2013-12-18 22 views
8

我有下面的代碼來生成所示的圖。與pyplot中的三個子圖中的兩個共享一個yaxis標籤

mport matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 
import numpy as np 

One = range(1,10) 
Two = range(5, 14) 
l = len(One) 
fig = plt.figure(figsize=(10,6)) 
gs = gridspec.GridSpec(3, 1, height_ratios=[5, 3, 3]) 

ax0 = plt.subplot(gs[0]) 
ax0.bar(range(l), Two) 
plt.ylabel("Number of occurrence") 

ax1 = plt.subplot(gs[1], sharey=ax0) 
ax1.bar(range(l), Two) 

ax2 = plt.subplot(gs[2]) 
ax2.bar(range(l), One) 

plt.show() 

enter image description here

我想要的ylabel(「發生的號碼」)被第一和第二曲線之間共享,即,它應該發生在中心左的第一和第二情節。我怎麼做?

+0

英語糾錯:你的意思是 「OCCURENCES數」? – kd88

回答

0

不是一個非常複雜的解決方案,但會工作嗎?更換

plt.ylabel("Number of occurrence") 

plt.ylabel("Number of occurrence", verticalalignment = 'top') 

[編輯]

我的版本的64位蟒蛇(2.7.3)的,我需要做一個小的變化

plt.ylabel("Number of occurrence", horizontalalignment = 'right') 

這是我看起來的樣子,這不是你想要的嗎?

enter image description here

+0

這不會將標籤放在前兩個地塊的中心。 – DurgaDatta

+0

@DurgaDatta我已經添加了圖片在我的計算機上的樣子,是要求嗎? – Brad

+0

這會做我的工作。但是,我的機器中沒有這種圖像。你把那條線放在哪裏?它是否重要? – DurgaDatta

0

我認爲這個問題是更多,如果在正確的術語說,創建多個軸。我想向您提出我所問的問題以及我收到的答案,它爲您提供了爲該圖創建多軸的解決方案。

鏈接到Matplotlib類似的問題的解決方案: Using Multiple Axis

鏈接到其他相關的問題是:multiple axis in matplotlib with different scales

3

我能想到的最好的辦法是將文本添加到數字本身和位置它在中心處(0.5圖座標)像這樣

import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 
import numpy as np 

One = range(1,10) 
Two = range(5, 14) 
l = len(One) 
fig = plt.figure(figsize=(10,6)) 
gs = gridspec.GridSpec(3, 1, height_ratios=[5, 3, 3]) 

ax0 = plt.subplot(gs[0]) 
ax0.bar(range(l), Two) 

ax1 = plt.subplot(gs[1], sharey=ax0) 
ax1.bar(range(l), Two) 

ax2 = plt.subplot(gs[2]) 
ax2.bar(range(l), One) 

fig.text(0.075, 0.5, "Number of occurrence", rotation="vertical", va="center") 

plt.show() 
0

也可以手動調節的位置,使用的012 y參數:

import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 
import numpy as np 

One = range(1,10) 
Two = range(5, 14) 
l = len(One) 
fig = plt.figure(figsize=(10,6)) 
gs = gridspec.GridSpec(3, 1, height_ratios=[5, 3, 3]) 

ax0 = plt.subplot(gs[0]) 
ax0.bar(range(l), Two) 
plt.ylabel("Number of occurrence", y=-0.8)   ## ← ← ← HERE 

ax1 = plt.subplot(gs[1], sharey=ax0) 
ax1.bar(range(l), Two) 

ax2 = plt.subplot(gs[2]) 
ax2.bar(range(l), One) 

plt.show() 

enter image description here

相關問題