2015-02-02 2 views
4

서브 Plot 내에서 두 개의 그림 크기를 조정하려고합니다. 기본적으로, 나는 111의 온도 프로파일과 211의 압력 프로파일을 가진 서브 플롯을 원합니다. 그러나 압력 수치를 온도 수치보다 작게하고 싶습니다. 다소 복잡한 gridspec lib없이 이것이 가능합니까? 다음 코드를 가지고 :matplotlib를 사용하여 서브 플로트 내에서 그림 크기를 조정하는 방법

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

각 서브 플로트에서 무화과 크기를 어떻게 변경하려고하는지보십시오. 이것이 잘못된 방법입니까?

좋아요, 그래서 저는 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() 
+0

'ax1 = plt.subplot (gs [0, :])'및'ax2 = plt.subplot (gs [1, :])'을 시도하십시오. –

+0

내 데이터 세트를 사용할 때 ax1과 ax2를 어떻게 사용합니까? – Joey

+0

'plt.set_axes (ax1)'과'plt.set_axes (ax2)'? –

답변

4

이다. 이것은 필요한 것보다 훨씬 복잡합니다.

데이터를로드하고 준비하십시오. 그런 다음 :

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

그런 식으로, 당신은 정말 사용하지 않는 외부 개체를 만들 필요가 없습니다 및 축되는하는 것은 수정되는대로 항상 명시 적입니다.

2

시도 :

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

하고 그렇게처럼 축 설정은 다음 pyplot 인터페이스가 그렇게 나쁜 이유

plt.axes(ax1) 
+3

실제로 'ax1'과'ax2'에 대해 실제로 그려 보는 것이 낫지 않습니까? (예 : ax'.plot (...)' –

+0

@PaulH 네, 동의 할 것입니다. 당신의 답변에'pyplot '이 매우 좋은 인터페이스를 제공하지 않는다는 것에 동의합니다. 간단한 플롯을 만들고 동일한 간단한 플롯을 구성하는 방법에 너무 많은 모호성을 삽입합니다. –

+0

@PaulH이 말했듯이, OO 인터페이스가 훨씬 더 사용하기 쉬운 경우입니다. 이것은 틀림없이 _ 유감입니다. 나쁜 습관입니다. – tacaswell

관련 문제