2017-06-02 5 views
4

Jupyterノートブックで指定されたサイズ、タイトル、および凡例の位置を持つ積み上げ横棒グラフを作成しようとしています。私は他のスタックオーバーフローソリューションを使用すると、私はいくつかのグラフが1つの代わりに印刷されます。ここには簡単な例があります:Jupiterノートブックで複数のグラフを表示するPythonの横積み棒グラフ

import pandas as pd 
import matplotlib.pyplot as plt 
a = [3,5,4,2,1] 
b = [3,4,5,2,1] 
c = [3,5,4,6,1] 
df = pd.DataFrame({'a' : a,'b' : b, 'c' : c}) 
df.plot.barh(stacked=True); 

fig, ax = plt.subplots() 
fig.set_size_inches(6,6) 

ax.set_title("My ax title") 
#plt.title("My plt title") # This seems to be identical to ax.set_title 
# Which is prefered? 
ax.legend(loc='upper left') 

plt.show() 

このコードは私に次の2つのプロットを与えます。プロットは私が探しているものですが、私のサイズと伝説の場所は無視され、タイトルは私が望まない2番目のグラフに置かれました。

enter image description hereenter image description here

注:私はそれが動作するようになったので、私はパンダからplot.barhを使用していますが、私はmatplotlibのから直接それを行うには同じように幸せになります。

答えて

4

plotの戻りオブジェクトを変数axに割り当てることができます。それからあなたが望むことをやりなさい。

a = [3,5,4,2,1] 
b = [3,4,5,2,1] 
c = [3,5,4,6,1] 
df = pd.DataFrame({'a' : a,'b' : b, 'c' : c}) 
ax = df.plot.barh(stacked=True); 

ax.figure.set_size_inches(6,6) 

ax.set_title("My ax title") 
ax.legend(loc='upper left') 

enter image description here


または代わりに、あなたはplot呼び出しであなたの作成したaxを参照している可能性があります。

a = [3,5,4,2,1] 
b = [3,4,5,2,1] 
c = [3,5,4,6,1] 
df = pd.DataFrame({'a' : a,'b' : b, 'c' : c}) 

fig, ax = plt.subplots() 
fig.set_size_inches(6,6) 

df.plot.barh(stacked=True, ax=ax); 

ax.set_title("My ax title") 
ax.legend(loc='upper left') 

enter image description here

2

あなたがもう一度あなたが

fig, ax = plt.subplots() 

はその取り払う使用して、空のプロットを作成している

df.plot.barh(stacked=True) 

を使用してプロットを作成しているし、それが第二の空のプロットの世話をします

プロット内にパラメータを渡すことができます

df.plot.barh(stacked=True,title = "My ax title", figsize = (6,6)) 
+1

これは完全に正確です。 – piRSquared

関連する問題