2017-01-18 6 views
2

共有Y軸を持つ2つのサブプロットからなるFigureを作ろうとしていますが、いくつかの 'ticks'はありません。例:Matplotlib - サブプロット間で軸を共有するときにいくつかのティックがありません

import matplotlib.pyplot as plt 
import pandas as pd 

df_a = pd.DataFrame({"Foo" : pd.Series(['A','B','C']), "Bar" : pd.Series([1,2,3])}) 
df_b = pd.DataFrame({"Foo" : pd.Series(['B','C','D']), "Bar" : pd.Series([4,5,6])}) 

fig, axes = plt.subplots(nrows=1, ncols=2, sharex= True, sharey=True) 

df_a.plot.barh("Foo", "Bar", ax=axes[0], legend=False, title="df_a") 
df_b.plot.barh("Foo", "Bar", ax=axes[1], legend=False, title="df_b") 

は(ラベルが混同されているダニ)以下のグラフを生成します。

enter image description here

私が見て期待していた何がこのような何か(Rを用いて製造されたもの)であります

enter image description here

私はここで何が欠けていますか?

答えて

1

あなたはとても一つの可能​​な解決策がconcatで、同じインデックスを必要とする:

df = pd.concat([df_a.set_index('Foo'), df_b.set_index('Foo')], axis=1) 
df.columns = ['a','b'] 
print (df) 
    a b 
A 1.0 NaN 
B 2.0 4.0 
C 3.0 5.0 
D NaN 6.0 

df.a.plot.barh(ax=axes[0], legend=False, title="df_a") 
df.b.plot.barh(ax=axes[1], legend=False, title="df_b") 

別の解決策は、indexesunionによってset_indexreindex次のとおりです。

df_a = df_a.set_index('Foo') 
df_b = df_b.set_index('Foo') 
df_a = df_a.reindex(df_a.index.union(df_b.index)) 
df_b = df_b.reindex(df_a.index.union(df_b.index)) 
print (df_a) 
    Bar 
Foo  
A 1.0 
B 2.0 
C 3.0 
D NaN 

print (df_b) 
    Bar 
Foo  
A NaN 
B 4.0 
C 5.0 
D 6.0 



df_a.plot.barh(ax=axes[0], legend=False, title="df_a") 
df_b.plot.barh(ax=axes[1], legend=False, title="df_b") 
+0

'pd.concat'トリックは次のように動作します魅力的で、残りのワークフローにうまく収まります。ありがとう! – dratewka

関連する問題