簡體   English   中英

如何使用matplotlib調整子圖中的數字大小

[英]how to resize figures within a subplot using 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(...)

這樣,您就不必創建任何無法真正使用的無關對象,並且始終明確指出要修改哪些軸。

嘗試:

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

然后像這樣設置你的軸:

plt.axes(ax1) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM