使用Pyhton上的matplotlib库,我想用动态y变量绘制一些图形,即变量会根据绘图函数之前声明的另一个变量而变化
从导入的数据框中,我提取了不同的气体浓度(M**u浓度)和通量(M**u通量)
M33_conc = ec_top["M 33(ppbv)"]
M39_conc = ec_top["M 39(ncps)"]
M45_conc = ec_top["M 45(ppbv)"]
M59_conc = ec_top["M 59(ppbv)"]
M69_conc = ec_top["M 69(ppbv)"]
M71_conc = ec_top["M 71(ppbv)"]
M81_conc = ec_top["M 81(ppbv)"]
M137_conc = ec_top["M 137(ppbv)"]
M87_conc = ec_top["M 87(ppbv)"]
M47_conc = ec_top["M 47(ppbv)"]
M61_conc = ec_top["M 61(ppbv)"]
M33_flux = ec_top["Flux_M 33"]
M45_flux = ec_top["Flux_M 45"]
M59_flux = ec_top["Flux_M 59"]
M69_flux = ec_top["Flux_M 69"]
M71_flux = ec_top["Flux_M 71"]
M81_flux = ec_top["Flux_M 81"]
M137_flux = ec_top["Flux_M 137"]
M87_flux = ec_top["Flux_M 87"]
M47_flux = ec_top["Flux_M 47"]
M61_flux = ec_top["Flux_M 61"]
我希望能够绘制这些气体浓度/通量随时间的演变,只有一个函数允许我在绘制这些气体的浓度或通量之间进行选择
以下是我到目前为止所写的内容:
color_1 = 'black'
graph_type='conc'
fig, ((ax1, ax2, ax3), (ax5, ax7, ax8),(ax9,ax10,ax11)) = plt.subplots(3, 3, sharex=True, sharey=False)
fig.suptitle('Influence of wind direction of BVOCs concentration')
ax1.plot(wind_dir,'M33_'+graph_type,linestyle='',marker='.',color=color_1)
ax1.set_title('Methanol')
ax1.set(ylabel='Concentration [ppbv]')
ax2.plot(wind_dir,M39_conc,linestyle='',marker='.',color=color_1)
ax2.set_title('Water cluster')
ax2.set(ylabel='Concentration [ncps]')
ax3.plot(wind_dir,M45_conc,linestyle='',marker='.',color=color_1)
ax3.set_title('Acetaldehyde')
ax3.set(ylabel='Concentration [ppbv]')
# ax4.plot(wind_dir,M47_conc,linestyle='',marker='.',color='color_1')
# ax4.set_title('Unknown')
ax5.plot(wind_dir,M59_conc,linestyle='',marker='.',color=color_1)
ax5.set_title('Acetone')
ax5.set(ylabel='Concentration [ppbv]')
# ax6.plot(wind_dir,M61_conc,linestyle='',marker='.',color='color_1')
# ax6.set_title('Unknown')
ax7.plot(wind_dir,M69_conc,linestyle='',marker='.',color=color_1)
ax7.set_title('Isoprene')
ax7.set(ylabel='Concentration [ppbv]')
ax8.plot(wind_dir,M71_conc,linestyle='',marker='.',color=color_1)
ax8.set_title('Methyl vinyl, ketone and methacrolein')
ax8.set(ylabel='Concentration [ppbv]')
ax9.plot(wind_dir,M81_conc,linestyle='',marker='.',color=color_1)
ax9.set_title('Fragment of monoterpenes')
ax9.set(ylabel='Concentration [ppbv]',xlabel='Wind direction [°]')
ax10.plot(wind_dir,M87_conc,linestyle='',marker='.',color=color_1)
ax10.set_title('Methylbutenols')
ax10.set(ylabel='Concentration [ppbv]',xlabel='Wind direction [°]')
ax11.plot(wind_dir,M137_conc,linestyle='',marker='.',color=color_1)
ax11.set_title('Monoterpenes')
ax11.set(ylabel='Concentration [ppbv]',xlabel='Wind direction [°]')
plt.show()
当我尝试参数化要打印的数据时,我会写入,例如:
'M33_'+graph_type
我希望取“M33_conc”的值
有人能帮我做这个吗
提前谢谢
您已经提到想要绘制气体随时间的演化曲线,但是在您给出的代码示例中,您使用
wind_dir
作为x变量。在这个答案中,我忽略了这一点,而是使用时间作为x变量看看你的代码,我知道你想要创建两个不同的数字,由小倍数组成,一个是气体浓度,一个是气体通量。对于这种类型的绘图,我建议使用pandas或seaborn,以便可以一次绘制pandas数据框中包含的所有变量。这里我分享一个使用熊猫的例子
由于您希望绘制相同物质的不同测量值,因此我建议创建一个表格,列出与每种独特物质相关的变量和单位的名称(见下文)。我创建了一个使用代码来提取单位并在这里共享,但使用电子表格软件更容易做到这一点
使用这样的表可以更容易地创建一个绘图函数,该函数从}
ec_top
数据帧中选择要绘图的变量组。然后可以像这样使用pandas plotting函数:^{下面显示的大多数代码都是基于您的代码创建一些示例数据,使您能够准确地重新创建我在这里显示的内容,并使任何其他想尝试此功能的人都能够重新创建。因此,如果您想使用此解决方案,您可以跳过其中的大部分,只需按自己的方式创建substances表,然后调整plotting函数以适合您的首选项
创建样本数据集
创建包含变量名称和单位的物质表
这些物质按此处列出的顺序显示在图子批次中。此表中的信息用于创建子批次的标签和标题
基于pandas
创建绘图功能下面是创建绘图函数的一种方法,该函数允许您使用
graph_type
参数为绘图选择变量。它通过使用if/elif
语句从物质表中选择相关变量来工作。这个函数和ec_top[variables].plot(...)
函数是创建绘图所必需的全部,其余的都用于格式化图形。变量按variables
列表的顺序绘制。由于这里的宽度限制,我只画了两列子图(最大宽度为10英寸,以便在堆栈溢出时获得清晰的图像)文件:matplotlib date ticks
相关问题 更多 >
编程相关推荐