可变Matplotlib直方图图元宽度 [英] Variable Matplotlib Histogram Bin Width

查看:289
本文介绍了可变Matplotlib直方图图元宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个具有3个子图的图形,尽管它们的宽度相等,但某些直方图箱的大小似乎有所不同.我的目标是创建宽度相等的直方图.

I am making a figure with 3 subplots and some of the histogram bins are appearing to be different sizes, despite them all being equal width. My goal is to create a histogram with equal width bars.

我正在绘制来自三个不同数据帧df1,df2,df3的数据,并且每个都有自己的轴.前两个数据帧(df1,df2)具有12个值,而第三个(df3)具有21个值.一个最小的工作示例:

I am plotting data from three different data frames df1,df2,df3 and each gets its own axis. The first two data frames (df1,df2) have 12 values, while the third (df3) has 21 values. A minimal working example:

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

在上面的图中,第三个子图ax2具有一个直方图条,看起来像条宽为0.2.

In the above plot, the third subplot ax2 has a histogram bar that appears to have a bin width of 0.2.

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

Could the length of the third data frame be causing this issue?

变量division是否指示垃圾箱宽度?

Doesn't the variable division dictate the bin width?

推荐答案

我不知道为什么,但是当我调整x刻度(即ax2.xaxis.set_ticks)时,它以某种方式改变了直方图条的外观.因此,有效的解决方案是:

I dont know why, but somehow when I was adjusting the x ticks (i.e. ax2.xaxis.set_ticks) it altered the appearance of the histogram bars. So the working solution is:

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

这篇关于可变Matplotlib直方图图元宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆