可变Matplotlib直方图Bin宽度

时间:2017-06-29 19:54:34

标签: python pandas matplotlib histogram

我正在制作一个包含3个子图的图形,并且一些直方图箱看起来是不同的尺寸,尽管它们都是相等的宽度。我的目标是创建一个宽度相等的直方图。

我正在绘制来自三个不同数据框df1,df2,df3的数据,每个数据框都有自己的轴。前两个数据帧(df1,df2)有12个值,而第三个(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。

第三个数据框的长度是否会导致此问题?

变量division是否指定了bin宽度?

1 个答案:

答案 0 :(得分:1)

我不知道为什么,但不知何故,当我调整x刻度(即ax2.xaxis.set_ticks)时,它改变了直方图条的外观。所以工作解决方案是:

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()

注意我已将ax2.xaxis.set_ticks(np.arange(-1, 1.5, 0.5))更改为ax2.xaxis.set_ticks(np.arange(-1, 2, 0.5))

enter image description here