2016-07-01 10 views
0

私は棒グラフをプロットするためにMatplotlibを使用しています。私のデータ値の大部分は-10から+30の範囲です。しかし、私は-300前後の2つのデータ値を持っています。最大長のMatplotlibプロット棒グラフ

私のデータをプロットすると、-300のデータ値バーは大きく見え、他のバーの洞察を隠します。 -10から+30の範囲のすべての小節をプロットする方法はありますか?-30で-300小節をクリップし、代わりに「-300」というラベルを書きますか?

答えて

3

ラベルを書くにはax.set_ylim()を使用し、ラベルを書くにはax.annotateを使用します(そして、好きな場合は、矢印)。例えば

import matplotlib.pyplot as plt 

fig, ax = plt.subplots(1) 

y = [-5, 10, 25, -10, 30, -300, 20, 30, -10, -300, 0, 4] 
x = range(len(y)) 

ax.bar(x, y, width=1, alpha=0.5) 

ymin, ymax = -15, 35 
ax.set_ylim(ymin, ymax) 

for xbar,ybar in zip(x,y): 

    if ybar < ymin: 

     ax.annotate(
       ybar, 
       xy=(xbar+0.5, -14), 
       xytext=(xbar+0.5, -8), 
       rotation=90, ha='center', va='center', 
       arrowprops=dict(arrowstyle="->")) 

plt.show() 

関連する問題