2017-02-23 7 views
0

私はサイト上でいくつかの議論をしましたが、私が望むものを見つけることができません。 )サブラインの共通線を同じ行に設定

enter image description here

私は(set_xlabelを試してみました、テキスト()が、それはうまくいきませんでした最初のために、第二のために:私はこのような同じ行の2つのサブプロットに共通は、xlabelを設定したいです私は下の中心にテキストを置くことができません。

誰かがどのように知っていれば、コードを最適化したいと思います。

マイコード:

plt.subplot(121) 
plt.barh(range(1, len(y)+1), y) 
plt.yticks([i+1.5 for i in range(7)], range(7)) 
plt.xlim(0, xmax) 

plt.ylabel("Y label") 

plt.subplot(122) 
plt.barh(range(1, len(y2)+1), y2, color="r") 
plt.yticks([i+1.5 for i in range(7)], range(7)) 
plt.xlim(0, xmax) 
+2

[matplotlibのをサブプロットのための共通のは、xlabel/ylabelの]の可能な重複(http://stackoverflow.com/questions/16150819/common-xlabel-ylabel-for-matplotlib-subplots)は –

+0

はすでにチェックされ、すでに試してみましたしかし、動作しませんでした。 3x3のサブプロットの場合、xlabelを中央に配置するのは簡単ですが、1x2のサブプロットについてはわかりません。 – Biopy

+1

なぜ/どのようにそれが "うまくいきませんでしたか"を説明してください –

答えて

0

例えばから分かるようにthis questionの場合は、図の中央にテキストを設定するだけです。 (これは、図にいくつのサブプロットがあるかに依存しません)。

plt.gcf().text(0.5,0.04,"common xlabel for subplots on the same line", ha="center") 

完全な例は:

import matplotlib.pyplot as plt 
import numpy as np 

y = np.random.randint(100,833, size=(7,2)) 

plt.subplot(121) 
plt.barh(range(1, len(y)+1), y[:,0]) 
plt.yticks([i+1.5 for i in range(len(y))], range(len(y))) 
plt.xlim(0, 1000) 

plt.ylabel("Y label") 

plt.subplot(122) 
plt.barh(range(1, len(y)+1), y[:,1], color="r") 
plt.yticks([i+1.5 for i in range(len(y))], range(len(y))) 
plt.xlim(0, 1000) 

plt.gcf().text(0.5,0.04,"common xlabel for subplots on the same line", ha="center") 
plt.show() 


他のオプションは、フレームをオフにして、新しい軸を作成し、それにラベルを提供することで、例えば参照 this question

ax = plt.gcf().add_subplot(111, frameon=False) 
ax.tick_params(labelcolor='none', top='off', bottom='off', left='off', right='off') 
ax.set_xlabel('common xlabel for subplots on the same line', labelpad=8) 
+0

実際に私がtext()を使用したとき、テキストはグラフに重ねられ、x座標を変更しても、それは私が望むところに置かれませんでした。たぶんあなたが追加するgcf()のためです。 – Biopy

関連する問題