首页 > 解决方案 > 将 Pandas 绘制成子图

问题描述

da 是我的数据框。我想把这个数字变成我将拥有的 2 个子图中的一个子图。当我为这个图添加 plt.subplots(2,1,2) 时,它最终将该图分成一个单独的图,并且子图是一个空图。

我怎样才能把这段代码变成一个子图?

-提前谢谢你,我是python的新手。 在此处输入图像描述

ax1 = da.plot(rot = 90, title ='Pre-Folsom Dam Spring Recession')
ax1.set_xlabel('Water Year Day')
ax1.axhline( y = float(fSP_Mag) , xmin=0, xmax=35,color ='r', linestyle='--',zorder=0,label= 'Magnitude')
ax1.axvline(x=float(fSP_Tim), color ='r',linestyle='--', label='Timing')
ax1.legend(framealpha=1, frameon=True)

标签: python-3.xpandasmatplotlib

解决方案


import pandas as pd
import matplotlib.pyplot as plt
data=pd.DataFrame({"col1":[1,2,3,4,5],"col2":[2,4,6,8,10]})
fig=plt.figure()
ax1=fig.add_subplot(2,1,1)
ax2=fig.add_subplot(2,1,2)
data["col1"].plot(ax=ax1)
data["col2"].plot(ax=ax2)

创建一个 plt.figure() 并将子图分配给 ax1 和 ax2。现在使用这些轴绘制数据框。

参考:-

  1. pyplot

推荐阅读