将 Pandas 绘制成子图 [英] Plotting Pandas into subplots

查看:142
本文介绍了将 Pandas 绘制成子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

da is my dataframe. I want to make this figure into one subplot out of 2 that I will have. When I add plt.subplots(2,1,2) for this figure it ends up separating this figure into a separate figure and the subplot is an empty figure.

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

How can I make this code into a subplot?

-提前谢谢你,我是python的新手.

-Thank you in advance, I am a newbie in 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)

推荐答案

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.现在使用这些轴绘制数据框.

Create a plt.figure() and assign subplots to ax1 and ax2.Now plot the dataframe using these axes.

参考:-

  1. Pyplot

这篇关于将 Pandas 绘制成子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆