Seaborn散点图使用多种不同标记而不是圆点
我正在使用seaborn 0.13.2这个版本。
我想画一个只有点的散点图。但是当我尝试用sns.scatterplot(end_of_year_2018)
时,结果却出现了各种奇怪的符号,比如三角形、加号、星号等等(见下面的图片)。
所以我尝试这样画散点图:
end_of_year_2018 = yf.download ([ "ABBN.SW", "ADEN.SW", "CFR.SW", "SGSN.SW", "HOLN.SW", "NESN.SW", "NOVN.SW", "ROG.SW", "SREN.SW", "SCMN.SW", "UHR.SW", "UBSG.SW", "ZURN.SW" ], start = '2018-12-28', end = '2018-12-29')['Adj Close']
sns.scatterplot(end_of_year_2018, marker='o')
但这些符号还是出现在图上。
其他的功能都正常。
我已经尝试更新seaborn:pip install --upgrade seaborn
,还尝试重置seaborn:sns.set(rc=None)
。
图的其他部分都正常,我该怎么办呢?
1 个回答
0
- 请查看这个 回答,了解如何正确地将位置参数传递给seaborn绘图API。
- 传递给seaborn的数据框(dataframe)应该是整洁的(长格式),而不是宽格式。你可以使用
pandas.DataFrame.melt
来实现这一点,具体可以参考这个 问题的回答。- seaborn更喜欢使用“长格式”或“整洁格式”的数据。这意味着每个变量都是一列,每个观察值都是一行。这种格式更灵活,因为它更容易对数据进行子集处理和创建复杂的可视化。
- 一般使用场景:
- 折线图:用于连续数据,比如日期对应的数值
- 柱状图:用于分类数据,比如类别对应的数值
- 散点图:用于显示两个变量之间关系的双变量图,通常是在同一样本上测量的
- 在
python v3.12.0
,pandas v2.2.1
,matplotlib v3.8.1
,seaborn v0.13.2
中测试过。
import yfinance as yf
import seaborn as sns
import matplotlib.pyplot as plt
# download the data
end_of_year_2018 = yf.download(['ABBN.SW', 'ADEN.SW', 'CFR.SW', 'SGSN.SW', 'HOLN.SW', 'NESN.SW', 'NOVN.SW', 'ROG.SW', 'SREN.SW', 'SCMN.SW', 'UHR.SW', 'UBSG.SW', 'ZURN.SW'], start='2018-12-28', end='2023-12-29')['Adj Close']
# convert the data to a long form
end_of_year_2018_long = end_of_year_2018.melt(ignore_index=False).reset_index()
# plot
plt.figure(figsize=(10, 8))
ax = sns.scatterplot(data=end_of_year_2018_long, x='Date', y='value', hue='Ticker', marker='.')
sns.move_legend(ax, bbox_to_anchor=(1, 0.5), loc='center left', frameon=False)
- 你可以选择使用
sns.relplot
,这样就不需要单独设置图形大小和图例位置了。
g = sns.relplot(data=end_of_year_2018_long, x='Date', y='value', hue='Ticker', marker='.', height=7)
注意事项
- 如果要比较很多日期,应该使用折线图,而不是散点图。
g = sns.relplot(kind='line', data=end_of_year_2018_long, x='Date', y='value', hue='Ticker', aspect=3)
- 如果要比较单个日期的值,比如使用
start='2018-12-28', end='2018-12-29'
,那么应该使用柱状图,而不是散点图。
end_of_year_2018 = yf.download(['ABBN.SW', 'ADEN.SW', 'CFR.SW', 'SGSN.SW', 'HOLN.SW', 'NESN.SW', 'NOVN.SW', 'ROG.SW', 'SREN.SW', 'SCMN.SW', 'UHR.SW', 'UBSG.SW', 'ZURN.SW'], start='2018-12-28', end='2018-12-29')['Adj Close']
end_of_year_2018_long = end_of_year_2018.melt(ignore_index=False).reset_index()
g = sns.catplot(kind='bar', data=end_of_year_2018_long, x='Ticker', y='value', aspect=3)
_ = g.fig.suptitle('Adjusted Close for 2018-12-28')
end_of_year_2018.head()
Ticker ABBN.SW ADEN.SW CFR.SW HOLN.SW NESN.SW NOVN.SW ROG.SW SCMN.SW SGSN.SW SREN.SW UBSG.SW UHR.SW ZURN.SW
Date
2018-12-28 15.066603 34.481403 58.147148 32.570705 70.373329 55.331772 208.348633 381.062653 74.905602 64.147781 10.002420 254.402374 222.631409
2019-01-03 14.885272 32.702148 56.541176 31.991671 71.643211 55.305443 213.356201 389.256622 75.007278 64.133545 10.010596 245.528900 222.555435
2019-01-04 15.260020 34.211132 58.664013 33.704643 72.577995 55.845329 214.854187 389.500031 76.939247 65.130074 10.317168 254.047409 225.745667
2019-01-07 15.151222 34.714130 59.033199 33.350792 71.537399 54.712891 212.200623 389.743408 77.007027 64.930763 10.337606 254.047409 223.846725
2019-01-08 15.360760 35.795193 60.048466 33.817234 71.872498 55.753159 216.138184 386.336060 77.515434 65.001945 10.398920 260.081360 226.505249
end_of_year_2018_long.head()
Date Ticker value
0 2018-12-28 ABBN.SW 15.066603
1 2019-01-03 ABBN.SW 14.885272
2 2019-01-04 ABBN.SW 15.260020
3 2019-01-07 ABBN.SW 15.151222
4 2019-01-08 ABBN.SW 15.360760
end_of_year_2018
- 使用
start='2018-12-28', end='2018-12-29'
,这只是一天的数据。
Ticker ABBN.SW ADEN.SW CFR.SW HOLN.SW NESN.SW NOVN.SW ROG.SW SCMN.SW SGSN.SW SREN.SW UBSG.SW UHR.SW ZURN.SW
Date
2018-12-28 15.066599 34.4814 58.147148 32.570705 70.373329 55.331787 200.049286 381.062653 74.905609 64.147789 10.002419 254.402344 222.631409
ax = sns.scatterplot(data=end_of_year_2018, markers=['.']*len(end_of_year_2018.columns))