2017-06-29 12 views
0

私は3つのサブプロットで図を作っており、ヒストグラムビンのいくつかは、すべて同じ幅であるにもかかわらず、異なるサイズで表示されています。私の目標は、等しい幅の棒でヒストグラムを作成することです。可変Matplotlibヒストグラムビン幅

私は3つの異なるデータフレームdf1,df2,df3からデータをプロットしており、それぞれが独自の軸を取得しています。最初の2つのデータフレーム(df1,df2)は12個の値を持ち、3番目のデータフレーム(df3)は21個の値を持っています。最小限の実施例:上記プロットで

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

#Data 
df1 = pd.DataFrame(data={'Delta_Thick': {0: -0.10257269427388138,1: -0.39092250646203491,2:-0.23459561055233191,3: 0.68753181981137268,4: -0.86443211703287937,5: -0.46963178960649432,6: 0.14070311160589327,7: 0.1885440568340489,8: 0.64210565529921859,9: -0.81346415594104837,10: 0.68175896505459788,11: 0.33673654536030828}}) 
df2 = pd.DataFrame(data={'Delta_Thick':{0: -0.38775619399296002,1: -0.32367407025583783,2: -0.56055783338428344,3: 0.23824247437746471,4: -0.64925233000340721,5: -0.44120245730257612,6: 0.027222094241818928,7: -0.091069018106476163,8: 0.0066066466889458386,9: -0.60477189852646174,10: 0.12878952794346843,11: -0.0077463979905486591}}) 
df3 = pd.DataFrame(data={'Delta_Thick':{0: 0.28518349971907864,1: -0.06724843620619711,2: 0.32596222283195153,3: 0.44928934543390797,4: 0.20911991461399143,5: -0.036989014816141919,6: -0.21517978702947216, 7: -0.028429332303918198,8: 0.037553921139760305,9: 0.98813506475654656,10: 0.51938760439670373,11: 0.11348101736407434,12: 0.79676269452200232,13: 0.27961307494052506,14: -0.55282685608381399,15: 0.63549900861027275,16: -0.20869225741458663,17: 0.55296943711112945,18: 0.34448294335085694,19: 0.18268186220418725,20: 0.36422880308671302}}) 

fig, (ax,ax1,ax2) = plt.subplots(ncols=3) 

bins=[round(x,1)for x in np.linspace(-1,1,21)] 
counts, division = np.histogram(df1.loc[:,'Delta_Thick'],bins=bins) 
df1.loc[:,'Delta_Thick'].hist(ax=ax, bins=division,color='green',label='Thing',hatch='//') 
ax.xaxis.set_ticks(np.arange(-1, 1.5, 0.5)) 
ax.yaxis.set_ticks(np.arange(0, 5, 1)) 
ax.set_title('A. 1990-2016') 
ax.set_ylabel('Number of Sites') 
ax.legend(fontsize='x-small',loc=2) 

#Deficit 
bins=[round(x,1)for x in np.linspace(-1,0.6,16)] 
counts, division = np.histogram(df2.loc[:,'Delta_Thick'],bins=bins) 
df2.loc[:,'Delta_Thick'].hist(ax=ax1, bins=division,color='green',hatch='//') 
ax1.xaxis.set_ticks(np.arange(-1, 0.75, 0.5)) 
ax1.yaxis.set_ticks(np.arange(0, 5, 1)) 
ax1.set_title('B. 1990-2003') 
ax1.set_xlabel('X axis label') 

#Enrich 
bins=[round(x,1)for x in np.linspace(-1,0.6,16)] 
counts, division = np.histogram(df3.loc[:,'Delta_Thick'],bins=bins) 
df3.loc[:,'Delta_Thick'].hist(ax=ax2, bins=division,color='green',hatch='//') 
ax2.xaxis.set_ticks(np.arange(-1, 1.5, 0.5)) 
ax2.yaxis.set_ticks(np.arange(0, 5, 1)) 
ax2.set_title('C. 2003-2016') 
plt.tight_layout() 
plt.show() 

output figure

、第三のサブプロットax2 0.2のビン幅を有するように見えるヒストグラムバーを有しています。

3番目のデータフレームの長さでこの問題が発生する可能性がありますか?

変数divisionはビン幅を指定していませんか?

+0

私はそれは垂直線のいずれかが表示されないことだけだ、ビンがすべての罰金だと思います。 Python3、matplotlib 2.0.2でQt5Aggバックエンドでコードを実行しましたが、ビンを分ける縦線は表示されません。 Figureを「png」として保存すると、Qt5Aggバックエンドと非常によく似たものが得られます。 PDFとして保存すると、ビンの中には孵化していないものもあります。私はそれがmatplotlibのバグか数字を表示するものであれば分かりません。 –

+0

@FrancescoMontesanoテストのためにありがとう – dubbbdan

+0

あなたは大歓迎です。 –

答えて

1

理由はわかりませんが、xティック(つまり、ax2.xaxis.set_ticks)を調整していたときに、ヒストグラムバーの外観が変更されました。だから、実用的なソリューションは、次のとおりです。私はax2.xaxis.set_ticks(np.arange(-1, 2, 0.5))ax2.xaxis.set_ticks(np.arange(-1, 1.5, 0.5))を変更

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

#Data 
df1 = pd.DataFrame(data={'Delta_Thick': {0: -0.10257269427388138,1: -0.39092250646203491,2:-0.23459561055233191,3: 0.68753181981137268,4: -0.86443211703287937,5: -0.46963178960649432,6: 0.14070311160589327,7: 0.1885440568340489,8: 0.64210565529921859,9: -0.81346415594104837,10: 0.68175896505459788,11: 0.33673654536030828}}) 
df2 = pd.DataFrame(data={'Delta_Thick':{0: -0.38775619399296002,1: -0.32367407025583783,2: -0.56055783338428344,3: 0.23824247437746471,4: -0.64925233000340721,5: -0.44120245730257612,6: 0.027222094241818928,7: -0.091069018106476163,8: 0.0066066466889458386,9: -0.60477189852646174,10: 0.12878952794346843,11: -0.0077463979905486591}}) 
df3 = pd.DataFrame(data={'Delta_Thick':{0: 0.28518349971907864,1: -0.06724843620619711,2: 0.32596222283195153,3: 0.44928934543390797,4: 0.20911991461399143,5: -0.036989014816141919,6: -0.21517978702947216, 7: -0.028429332303918198,8: 0.037553921139760305,9: 0.98813506475654656,10: 0.51938760439670373,11: 0.11348101736407434,12: 0.79676269452200232,13: 0.27961307494052506,14: -0.55282685608381399,15: 0.63549900861027275,16: -0.20869225741458663,17: 0.55296943711112945,18: 0.34448294335085694,19: 0.18268186220418725,20: 0.36422880308671302}}) 

fig, (ax,ax1,ax2) = plt.subplots(ncols=3) 

bins=[round(x,1)for x in np.linspace(-1,1,21)] 
counts, division = np.histogram(df1.loc[:,'Delta_Thick'],bins=bins) 
df1.loc[:,'Delta_Thick'].hist(ax=ax, bins=division,color='green',hatch='//') 
ax.xaxis.set_ticks(np.arange(-1, 1.5, 0.5)) 
ax.yaxis.set_ticks(np.arange(0, 5, 1)) 
ax.set_title('A. 1990-2016') 
ax.set_ylabel('Number of Sites') 
ax.legend(fontsize='x-small',loc=2) 

#Deficit 
bins=[round(x,1)for x in np.linspace(-1,1,21)] 
counts, division = np.histogram(df2.loc[:,'Delta_Thick'],bins=bins) 
#ax1.hist(df2.loc[:,'Delta_Thick'],bins=counts.size) 
df2.loc[:,'Delta_Thick'].hist(ax=ax1, bins=division,color='green',hatch='//') 
ax1.xaxis.set_ticks(np.arange(-1, 1.5, 0.5)) 
ax1.yaxis.set_ticks(np.arange(0, 5, 1)) 
ax1.set_title('B. 1990-2003') 
ax1.set_xlabel('X axis label') 

#Enrich 

bins=[round(x,1)for x in np.linspace(-1,1,21)] 
counts, division = np.histogram(df3.loc[:,'Delta_Thick'],bins=bins) 
df3.loc[:,'Delta_Thick'].hist(ax=ax2, bins=division,color='green',hatch='//') 
ax2.xaxis.set_ticks(np.arange(-1, 2, 0.5)) 
ax2.yaxis.set_ticks(np.arange(0, 10, 1)) 
ax2.set_title('C. 2003-2016') 


plt.tight_layout() 
plt.show() 

注意してください。

enter image description here

+0

私は問題の一部は、あるバーの境界が近くのものの後ろにあることが時々あると思います。また、境界線の幅を広げることもできます。これが機能するかどうかはわかりませんが、試してみる価値はあります。 –

関連する問題