如何将三个折线图合并为一个共享x轴但具有独立y轴的matplotlib?

时间:2018-11-07 23:57:11

标签: python pandas matplotlib

我能够使matplotlib中的三个图很好,但是不确定如何将它们合并为一个共享一个月数的x轴和单独的y轴的完整图。

这是我从三个不同的csv文件绘制的三个图形的代码:

import pandas as pd 
import matplotlib.pyplot as plt
#import numpy as np


df = pd.read_csv('1541544819_et.csv', skiprows=4)

ax = df.plot(color="blue",kind='line', legend = False,figsize=(15,6))

ax.set_ylabel("Evapotranspiration (inches)")

ax.set_xticks([0,65,125,185,245,305,365])

ax.set_xticklabels(['Jan','Mar','May','Jul','Sep','Nov','Jan'], fontsize = 'large')

plt.show()




df = pd.read_csv('1541544819_rainfall.csv', skiprows=4)

ax2 = df.plot(color="orange",kind='line', legend = False,figsize=(15,6))

ax2.set_ylabel("Rainfall (inches)")

ax2.set_xticks([0,65,125,185,245,305,365])

ax2.set_xticklabels(['Jan','Mar','May','Jul','Sep','Nov','Jan'], fontsize = 'large')

ax2.set_yticks([0,2,4,6,8,10])

ax2.set_yticklabels([0.0,0.5,1.0,1.5,2.0,2.5,3.0])

plt.show()





df = pd.read_csv('1541556002_water_level.csv', skiprows=4)

ax3 = df.plot(color="green",kind='line', legend = False,figsize=(15,6))

ax3.set_ylabel("Water Level (ft above NADV88)")

ax3.set_xticks([0,65,125,185,245,305,365])

ax3.set_xticklabels(['Jan','Mar','May','Jul','Sep','Nov','Jan'], fontsize = 'large')

ax3.set_yticks([0,2,4,6,8,10])

ax3.set_yticklabels([6.0,6.2,6.4,6.6,6.8,7.0])

plt.show()

这将导致以下折线图:

enter image description here

现在,我只需要将它们堆叠在一起就可以合并在一起。与月份共享x轴。希望获得所有帮助。

谢谢

1 个答案:

答案 0 :(得分:0)

您是否尝试过消除前两个语句以显示情节?

plt.show()

然后删除斧头,斧头2和斧头3/3。它们都应该指向同一个x轴吗?