2017-10-18 5 views
-2

x軸のティックラベル(グラフ下のもの)が全体像から貴重なスペースを奪っています。テキストの回転を変更してサイズを縮小しようとしましたが、テキストラベルがかなり長いため、それはあまり役に立ちません。matplotlib図のxticklabels領域を減らす

xticklabel領域が占める領域を減らすためのより良いアプローチはありますか?たとえば、バーの中にこのテキストを表示できますか?ご協力ありがとうございました。グラフ設定の

私のコードは次のとおりです。

import matplotlib.pyplot as plt 
import matplotlib 
matplotlib.rcParams['font.sans-serif'] = "Century Gothic" 
matplotlib.rcParams['font.family'] = "Century Gothic" 

ax = df1.plot.bar(x = '', y = ['Events Today', 'Avg. Events Last 30 Days'], rot = 25, width=0.8 , linewidth=1, color=['midnightblue','darkorange']) 

for item in ([ax.xaxis.label, ax.yaxis.label] + 
     ax.get_xticklabels() + ax.get_yticklabels()): 
    item.set_fontsize(15) 

ax.legend(fontsize = 'x-large', loc='best') 
plt.tight_layout() 
ax.yaxis.grid(True, which='major', linestyle='-', linewidth=0.15) 
ax.set_facecolor('#f2f2f2') 
plt.show() 

enter image description here

+0
+0

?! – Gonzalo

+1

あなたは私に、または誰かに 'df1'自身を生産するためにこれに答える傾向があると頼んでいます。あなたがここで助けを求めているということを考えれば、あなたのことはあまりいいことではありません。 – ImportanceOfBeingErnest

答えて

1

私はunaesthetically長いxticklabelsで終わるとき、私は最初の、そして最も重要なことは、それらを短くしようとすることです。明白なように見えるかもしれませんが、省略形や異なる説明を使用することは、しばしば最も簡単で効果的な解決策であることを指摘する価値があります。

ロングネームと特定のフォントサイズに悩まされている場合は、代わりに水平のバープロットを作成することをお勧めします。私は一般的に長いラベルの水平プロットを好む。なぜなら、回転していないテキストを読むほうが簡単だからである(フォントサイズの1ステップをさらに減らすこともできるかもしれない)。ここで

は扱いにくいラベル付きグラフの例である:

import pandas as pd 
import seaborn as sns # to get example data easily 

iris = sns.load_dataset('iris') 
means = iris.groupby('species').mean() 
my_long_labels = ['looooooong_versicolor', 'looooooooog_setosa', 'looooooooong_virginica'] 
# Note the simpler approach of setting fontsize compared to your question 
ax = means.plot(kind='bar', y=['sepal_length', 'sepal_width'], fontsize=15, rot=25) 
ax.set_xlabel('') 
ax.set_xticklabels(my_long_labels) 

enter image description here

私は水平barplotにこれを変更します

ax = means.plot(kind='barh', y=['sepal_length', 'sepal_width'], fontsize=15) 
ax.set_ylabel('') 
ax.set_yticklabels(my_long_labels) 

enter image description here

あなたはラベルに改行を導入してさらに改善することができますこれはまた、垂直バーで動作します

ax = means.plot(kind='barh', y=['sepal_length', 'sepal_width'], fontsize=15, rot=0) 
ax.set_ylabel('') 
ax.set_yticklabels([label.replace('_', '\n') for label in my_long_labels]) 

enter image description here

ax = means.plot(kind='bar', y=['sepal_length', 'sepal_width'], fontsize=15, rot=0) 
ax.set_xlabel('') 
ax.set_xticklabels([label.replace('_', '\n') for label in my_long_labels]) 

enter image description here

最後に、あなたはまた、バー内のテキストを持つことができますが、これは困難な読みやすさVEの読む。 [MCVE]問題の程度

ax = means.plot(kind='barh', y=['sepal_length', 'sepal_width'], fontsize=15) 
ax.set_ylabel('') 
ax.set_yticklabels(my_long_labels, x=0.03, ha='left', va='bottom') 

enter image description here

+0

すごい!このような完全な答えをありがとう! – Gonzalo

関連する問題