I'm new to Python and I'm working in iPython notebook.
I want to plot the data in subplots using secondary axes. I can set the ylim but it affects only the secondary y-axis (on the right side). I can't find a way to control the ylim on the primary y-axis (on the left side). I'd like the primary y-axis to have a limit of [0,40], and the secondary y-axis to have a limit of [0,100].
#import matplotlib.pylab as plt
import numpy as np
import pandas as pd
import csv
import string
%pylab inline
%matplotlib inline
# READ IN FIRST CSV
espr = pd.read_csv('out.csv')
df_e = pd.DataFrame(espr)
#READ IN SECOND CSV
heat = pd.read_csv('heat.csv',encoding='ISO-8859-1',low_memory=False)
heat = heat[2:]
df_h = pd.DataFrame(heat,dtype='float64')
df_h = df_h.reset_index(drop=True)
#READ IN THIRD CSV
temp = pd.read_csv('temp.csv',encoding='ISO-8859-1',low_memory=False)
temp = temp[2:]
df_t = pd.DataFrame(temp,dtype='float64')
df_t = df_t.reset_index(drop=True)
# CONCATENATE DATAFRAMES
df_e = df_e[[' building:time step (-)', ' building:zone 01:air point:temperature (oC)', ' building:zone 05:air point:temperature (oC)']]
df_h = df_h[['TIME', 'Tdb_amb']]
df_t = df_t[['Tavg_diurn', 'Tavg_seas']]
sim = pd.concat([df_h, df_t, df_e],axis=1)
# SEPARATE DATA FOR PLOTTING
mar = sim[14160:21600]
This is where I'm running into a wall, I've read about setting twinx() to set properties for different axes but whenever I try to implement it, it tells me it doesn't know what twinx is.
fig, axes = plt.subplots(nrows=2, ncols=1)
fig.set_figheight(25)
fig.set_figwidth(20)
mar[' building:zone 01:air point:temperature (oC)'].plot(ax=axes[0])
mar[' building:zone 05:air point:temperature (oC)'].plot(ax=axes[0])
mar['Tavg_diurn'].plot(secondary_y=True, ax=axes[0])
mar['Tavg_seas'].plot(secondary_y=True, ax=axes[0])
ylim([0,100])
fig.tight_layout()
Thanks in advance for any help.
ylim() to get the ylim of the plot on an axis. @dashesy You use set_xlim and set_ylim . plt has many fewer options than working directly with the axes object. In fact, almost every function in plt is a very thin wrapper that first calls ax = plt.
To set the lower axis limit, we use xlim() and ylim() function and pass the left and bottom parameter respectively. To plot the graph, use plot() function. To set the labels at the axes, use xlabel() and ylabel() function.
Well if axes
is an array of the two axes then you could do:
axes[0].set_ylim(0, 40)
axes[1].set_ylim(0, 100)
or using pyplot's twinx()
you could try:
axes[0] = ax2.twinx()
axes[0].set_ylim(0, 40)
ax2.set_ylim(0, 100)
The key is this: axes[0].right_ax.set_ylim(0,100)
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
%matplotlib inline
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(20, 10), sharex=True)
columns = [' building:zone 01:air point:temperature (oC)', ' building:zone 05:air point:temperature (oC)',
'Tavg_diurn', 'Tavg_seas']
mar = pd.DataFrame(np.random.randn(1000, 4), index=pd.date_range('1/1/2010', periods=1000), columns=columns)
mar = mar.cumsum()
mar.plot(ax=axes[0], secondary_y=['Tavg_diurn', 'Tavg_seas'])
axes[0].set_ylim(0,40)
axes[0].right_ax.set_ylim(0,100)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With