How to merge three line plots into one that share one x-axis but have separate y-axes with matplotlib?

I am able to make the three graphs in matplotlib fine but not sure how to go about merging them into one whole graph sharing a single x-axis of months and a separate y-axes.

Here is the code I have for the three graphs that draw from three distinct csv files:

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()

And that results in the following line plots:

enter image description here

Now I just need to merge them together stacked on top of each other. With the shared x-axis of months. Would appreciate any and all help.

Thank you

1 answer

  • answered 2018-11-08 00:04 Ryan Jarvis

    Have you tried eliminating the first two Statements to show show the plot?

    plt.show()
    

    Then remove 2/3 of the ax, ax2, and ax3. They should all refer to the same x-axis?