Python 如何使用matplotlib调整子地块内地物的大小

Python 如何使用matplotlib调整子地块内地物的大小,python,matplotlib,pandas,subplot,Python,Matplotlib,Pandas,Subplot,我正在尝试调整子地块中两个图形的大小。基本上,我想要一个子地块,温度分布在111,压力分布在211。但是,我希望压力图小于温度图。如果没有相当复杂的gridspec库,这可能吗?我有以下代码: import pandas as pd import matplotlib.pyplot as plt import numpy as np # ------Pressure------# df1 = pd.DataFrame.from_csv('Pressure1.csv',index_col =

我正在尝试调整子地块中两个图形的大小。基本上,我想要一个子地块,温度分布在111,压力分布在211。但是,我希望压力图小于温度图。如果没有相当复杂的gridspec库,这可能吗?我有以下代码:

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()
查看我如何尝试更改每个子批次中的figsize。这条路错了吗

好的,我已经设法得到了我想要的gridspec。但是,如何将两者结合起来呢

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()
尝试:

然后按如下方式设置轴:

plt.axes(ax1) 

这就是为什么
pyplot
界面如此糟糕的原因。这比需要的要复杂得多

加载并准备数据。然后:

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

这样,您就不必创建任何未真正使用的无关对象,并且它总是明确显示修改了哪些轴。

尝试
ax1=plt.subplot(gs[0,:])
ax2=plt.subplot(gs[1,:])
。使用自己的数据集时如何使用ax1和ax2?
plt.set\u轴(ax1)
plt.set_轴(ax2)
?plt.axs(ax1)工作:)谢谢。实际绘制ax1和ax2不是更好吗?(即,ax
.plot(…)
@PaulH是的,我同意你的看法。我也同意你在回复中所说的,
pyplot
没有提供一个非常好的接口,它会导致简单绘图的过度复杂,并在如何配置相同的简单绘图上插入太多的模糊性。正如@PaulH所说,这就是OO接口非常容易的情况r使用。虽然这并没有错,但我很想否决投票,因为这是一个不好的做法。确实是一个比pyplot好得多的界面!然而,当我试图运行上面的代码时,我出现了两个错误。1)
plt。对于2x1网格,Figure
应该是小写的
plt。Figure
2),宽度应定义为一个单值列表,因为只有一个宽度可设置,因此
width\u ratio=[20]
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(...)