微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 如何使用matplotlib调整子图中的数字

我试图在子图中调整两个数字的大小.基本上,我想要一个温度分布为111且压力分布为211的子图.但是,我希望压力值小于温度值.这有可能没有相当复杂的gridspec lib吗?我有以下代码:

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

解决方法:

这就是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(...)

这样,您就不必创建任何无法真正使用的无关对象,并且始终明确指出要修改哪些轴.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐