2017-05-18 4 views
3

によるPythonのパンダプロット二つBARH側に、 enter image description here

左側のチャート(伝説とプロット)はdf_2に由来し、そして右手サイドチャートはdf_1から派生しています。

しかし、2つのプロットを横に並べてy軸に共有することはできません。コードの意志は、2つの異なる「キャンバス」に2つのプロットを結果

df_1[target_cols].plot(kind='barh', x='LABEL', stacked=True, legend=False) 
df_2[target_cols].plot(kind='barh', x='LABEL', stacked=True).invert_xaxis() 
plt.show() 

は、ここにプロットする私の現在の方法です。

  1. どうすれば横軸をy軸で共有できますか?
  2. 左手側のグラフ(Y軸のラベルはdf_2から派生しています)を削除するにはどうすればよいですか?

ご意見をいただければ幸いです。ありがとう。

答えて

5

plt.subplots(sharey=True)を使用して共有サブプロットを作成できます。次に、データフレームを2つのサブプロットにプロットします。

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 

a = np.random.randint(5,15, size=10) 
b = np.random.randint(5,15, size=10) 

df = pd.DataFrame({"a":a}) 
df2 = pd.DataFrame({"b":b}) 

fig, (ax, ax2) = plt.subplots(ncols=2, sharey=True) 

ax.invert_xaxis() 
ax.yaxis.tick_right() 

df["a"].plot(kind='barh', x='LABEL', legend=False, ax=ax) 
df2["b"].plot(kind='barh', x='LABEL',ax=ax2) 
plt.show() 

enter image description here

+0

ありがとう!できます!今、私は 'subplot'の使い方を理解しています! – arnold

関連する問題