4

I'm trying to resize two figures within a subplot. Basically, I want a subplot with a temperature profile at 111 and a pressure profile at 211. However, I want the pressure figure to be smaller than the temperature figure. is this possible without the rather complicated gridspec lib? I have the following code:

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

# ------Pressure------#

df1 = pd.DataFrame.from_csv('Pressure1.csv',index_col = None)
df1['P'] = df1['P'].str.split('.').str[:-1].str.join(',').str.replace(',', '.').astype(np.float64)
a = df1['T']
a /= 2900 # Convert milliseconds to hours
initial_time = 0
a += initial_time - min(a)
b = df1['P']
b -=+1 #convert from volts to bar

# ------- Temperature----------#

df = pd.DataFrame.from_csv('Temperature1.csv',index_col = None)

w = df['Time']
w /= 3600000 # Convert milliseconds to hours
initial_time = 0
w += initial_time - min(w)

x = df['Input 1']
y = df['Input 2']
z = df['Input 3']

#----subplot----#

plt.subplot(1,1,1)
figsize(20,10)
plt.plot(w,x, label = "Top_sensor")
plt.plot(w,y, label = "Mid_sensor")
plt.plot(w,z, label = 'Bot_sensor')
plt.title('Temperature')
plt.xlabel('Time(Hours)')
plt.ylabel('Temperature (K)')
plt.ylim(70, 200)
plt.xlim(0, 12, 1)
plt.show()

plt.subplot(2,1,1)
figsize(20,3)
plt.plot(a,b)
plt.title('Pressure_Monitoring')
plt.xlabel('Time(Hours)')
plt.ylabel('Pressure (Bar)')
plt.xlim(0, 12, 1)
plt.show()

See how I try to change the figsize in each subplot. Is this the wrong way?

Okay, so i've managed to get the gridspec i want. But, how do i combine the two?

import matplotlib.gridspec as gridspec

f = plt.figure()

gs = gridspec.GridSpec(2, 1,width_ratios=[20,10], height_ratios=[10,5])

ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])

plt.show()
4
  • Try ax1 = plt.subplot(gs[0, :]) and ax2 = plt.subplot(gs[1, :]). Commented Feb 2, 2015 at 14:25
  • how do i use the ax1 and ax2 when using my own datasets? Commented Feb 2, 2015 at 14:29
  • plt.set_axes(ax1) and plt.set_axes(ax2)? Commented Feb 2, 2015 at 14:33
  • plt.axes(ax1) worked :) thanks. Commented Feb 2, 2015 at 14:39

2 Answers 2

4

This is why the pyplot interface is so bad. This is way more complicated than it needs to be.

Load and prepare your data. Then:

fig = plt.Figure(figsize=(20, 3))
gs = gridspec.GridSpec(2, 1, width_ratios=[20,10], height_ratios=[10,5])
ax1 = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1])

ax1.plot(...)
ax1.set_ylabel(...)
...

ax2.plot(...)
ax2.set_xlabel(...)

That way, you don't have to create any extraneous objects that don't really get used and it's always explicit as to which axes is being modified.

Sign up to request clarification or add additional context in comments.

1 Comment

A much nicer interface than pyplot indeed! However, when I tried to run the code above I got two errors. 1) plt.Figure should be lower case plt.figure 2) for a 2x1 grid, the width should be defined as a single value list since there is only one width to set, so width_ratios=[20].
2

Try:

ax1 = plt.subplot(gs[0, :])
ax2 = plt.subplot(gs[1, :])

and then set your axes like so:

plt.axes(ax1) 

3 Comments

wouldn't it just be better to actually plot against the ax1 and ax2? (i.e., ax.plot(...)
@PaulH yes, I would agree with you. I also agree with what you stated in your response that pyplot does not provide a very good interface which causes an overcomplicating of simple plots and inserts too much ambiguity on how to configure that same simple plot.
As @PaulH says, this is the case where the OO interface is much easier to use. While this isn't wrong I am tempted to down vote because it is bad practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.