我正试图建立一个阴谋,在那里我可以根据空气中的化学物质检查鸟类的死亡率。我所面临的挑战是如何将情节相互叠加
下面是我写的代码。基本上给定了6种化学物质,我有六个独立的轴,在每个轴上,我以周数为x轴,绘制出所述化学物质与鸟类死亡率的关系
即使图形的来源来自一个数据帧(X轴没有区别),Seaborn也会分别绘制这两个图形
我尝试使用bird_chem_df.plot()
和vanilla matplotlib来查看这是否仅限于Seaborn,但没有运气
很想知道到底是什么原因导致X轴不一致,尽管数据来自同一来源,并且它们共享相同的自变量
如果你想玩数据=https://drive.google.com/file/d/1KKkSoy3xQno_vE_-LSoviClmqC3UDcW6/view?usp=sharing
# Import the CSV in the link above
# bird_chem_df = pd.read_csv('...')
# Using `target_chemicals_chart` as our array, we will loop through each
# and plot it against bird mortality
target_chemicals_chart = ['Toluene', 'o-Xylene', 'm,p-Xylene','Ethylbenzene', 'Benzene', 'PM2.5']
target_chemicals_display = {
'Toluene':[0,2.5],
'o-Xylene':[0,2.5],
'm,p-Xylene':[0,2.5],
'Benzene':[0,2.5],
'Ethylbenzene':[0,8],
'PM2.5':[0,25]
}
chemical_index = 0
# Setup the plot
fig, axs = plt.subplots(2,3,figsize=(12,7))
fig.subplots_adjust(left=7, bottom=7, right=9, top=9, wspace=0.2, hspace=0.5)
# Plot each axis
for row in axs:
for col in row:
target_chemical = target_chemicals_chart[chemical_index]
col.set_title(f"{target_chemical} vs. Bird Mortality")
sb.barplot(ax=col, x="week_number", y=target_chemical, data=bird_chem_df, ci=None, color='lightsteelblue')
col.set_ylim(target_chemicals_display[target_chemical])
bird = col.twinx()
sb.lineplot(ax=bird, x="week_number", y="total_bird_deaths", data=bird_chem_df, color='red')
col.set_xlabel("Weeks since Spill")
col.set_ylabel("Average Result for Chemical - ug/m3")
# Increase font size due to fig configurations
for item in ([col.title, col.xaxis.label, col.yaxis.label] +
col.get_xticklabels() + bird.get_yticklabels() + col.get_yticklabels()):
item.set_fontsize(20)
chemical_index += 1
axs
转换为1-D,以便于访问和迭代李>'week_number'
,与线形图相同,因此两个图之间的记号位置数量相同。因此,不是使用x='week_number'
打印,而是通过指定x=ax.get_xticks()
将线图打印到与条形图相同的勾号位置x=bird_chem_df.index
(因为索引是从0开始的RangeIndex
),然后将xtick标签更改为bird_chem_df.week_number
python 3.8.11
、pandas 1.3.2
、matplotlib 3.4.3
、seaborn 0.11.2
相关问题 更多 >
编程相关推荐