简体   繁体   English

将pandas DataFrame图插入matplotlib子图

[英]Insert a pandas DataFrame plot into a matplotlib subplot

I have created a plot with 4 subplots and each subplot will show a different type of analyses on some infrasound data. 我创建了一个包含4个子图的图,每个子图将在一些次声数据上显示不同类型的分析。 This the code I have used to create the subplots: 这是我用来创建子图的代码:

gs = gridspec.GridSpec(2, 2, width_ratios=[1,1], height_ratios=[1,1])

ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])
ax3 = plt.subplot(gs[2])
ax4 = plt.subplot(gs[3])

So far I have been able to input what I have wanted into the subplots, but I want to be able to input a pandas DataFrame plot into ax3 and I can't seem to do it. 到目前为止,我已经能够将我想要的内容输入到子图中,但我希望能够将一个pandas DataFrame图输入到ax3中,我似乎无法做到这一点。 I have already written the pandas program and was just going to insert it into the larger script so it was shown in the subplot. 我已经编写了pandas程序,只是将它插入到更大的脚本中,因此它显示在子图中。

This is the line of code that is used to plot the pandas DataFrame plot: 这是用于绘制pandas DataFrame图的代码行:

df.plot(subplots=True, sharey=True, ylim=(0,(y_max*1.5)))

When plotting using pandas.Dataframe.plot you can choose the Axes object you would like to plot to with the keyword argument ax as shown below: 使用pandas.Dataframe.plot绘图时,您可以使用关键字参数ax选择要绘制到的Axes对象,如下所示:

gs = gridspec.GridSpec(2, 2, width_ratios=[1,1], height_ratios=[1,1])

ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])
ax3 = plt.subplot(gs[2])
ax4 = plt.subplot(gs[3])

# ...some other code that defines df...

df.plot(ax=ax3)

This will add your data to the ax3 object. 这会将您的数据添加到ax3对象。 Note that this will plot all of your columns into that one subplot, if you want one particular column then you could do df['my_col_name'].plot(ax=ax3) . 请注意,这会将所有列绘制到该子图中,如果您需要一个特定列,则可以执行df['my_col_name'].plot(ax=ax3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM