Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPython notebook - set ylim on subplot secondary y-axis

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.

like image 750
user3508369 Avatar asked Jan 22 '15 18:01

user3508369


People also ask

How do you set YLIM in subplots?

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.

How do I change YLIM to XLIM in Python?

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.


2 Answers

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)
like image 167
alacy Avatar answered Oct 22 '22 08:10

alacy


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)

A matplotlib figure resulting form the above code

like image 35
Andrew Sturges Avatar answered Oct 22 '22 08:10

Andrew Sturges