将容器与plt.hist中的xticks对齐 [英] Aligning bins to xticks in plt.hist

查看:386
本文介绍了将容器与plt.hist中的xticks对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据气候模型制作降水率的直方图,并且直方图函数起作用了,但是x轴让我有些不适.我希望每个垃圾箱之间都直接有一个刻度,例如2.55.但是,某些刻度线已关闭,主要在左侧.我有什么办法可以正确对齐它们?

I'm making a histogram of precipitation rate means from climate models and the histogram function worked, but the x-axis irks me ever so slightly. I want there to be a tick directly in between each bin like for 2.55. However, some of the ticks are off, mainly on the left side. Is there any way I can align them properly?

x = np.arange(0.006,0.0345,0.0015)
print (x)

#Make historical (1979-2015) histogram
plt.figure(figsize=(11,7))
plt.hist(histmeans, 19, color='#808080')

#labels & axes
#plt.locator_params(nbins=19, axis='x')
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
plt.title('Precip. Flux Anomaly (1979-2015 means, CanESM2 Hist)',fontsize=20)
plt.xlabel('Precip. Flux Mean (mm/day)',fontsize=15)
plt.ylabel('Number of Members',fontsize=15)
plt.xticks(x)
plt.xlim(0.006,0.0345)

print (np.min(histmeans))
print (np.max(histmeans))

输出:

[ 0.006   0.0075  0.009   0.0105  0.012   0.0135  0.015   0.0165  0.018
  0.0195  0.021   0.0225  0.024   0.0255  0.027   0.0285  0.03    0.0315
  0.033   0.0345]

0.00612598903444

0.0344927479091

推荐答案

plt.hist采用选项bins,该选项可以是整数(如脚本中的整数)或bin边列表.因此,您可以使用已定义为x的bin边缘范围作为此bins选项,以设置您感兴趣的确切bin边缘.

plt.hist takes the option bins, which can either be an integer (as you have in your script), or a list of bin edges. So, you can use the range of bin edges you have defined as x as this bins option, to set the exact bin edges you are interested in.

x = np.arange(0.006,0.0345,0.0015)
plt.hist(histmeans, bins = x, color='#808080')

此处为完整脚本:

import matplotlib.pyplot as plt
import numpy as np

# random data in your range
hmin,hmax = 0.00612598903444, 0.0344927479091
histmeans = hmin + np.random.rand(50)*(hmax-hmin)

x = np.arange(0.006,0.0345,0.0015)
print (x)

#Make historical (1979-2015) histogram
plt.figure(figsize=(11,7))
n,bins,edges = plt.hist(histmeans, x, color='#808080',edgecolor='k')

#Check bins
print bins

#labels & axes
#plt.locator_params(nbins=19, axis='x')
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
plt.title('Precip. Flux Anomaly (1979-2015 means, CanESM2 Hist)',fontsize=20)
plt.xlabel('Precip. Flux Mean (mm/day)',fontsize=15)
plt.ylabel('Number of Members',fontsize=15)
plt.xticks(x)
plt.xlim(0.006,0.0345)

print (np.min(histmeans))
print (np.max(histmeans))


plt.show()

这是输出:

[ 0.006   0.0075  0.009   0.0105  0.012   0.0135  0.015   0.0165  0.018
  0.0195  0.021   0.0225  0.024   0.0255  0.027   0.0285  0.03    0.0315
  0.033   0.0345]
[ 0.006   0.0075  0.009   0.0105  0.012   0.0135  0.015   0.0165  0.018
  0.0195  0.021   0.0225  0.024   0.0255  0.027   0.0285  0.03    0.0315
  0.033   0.0345]
0.00661096260281
0.0341882193394

这篇关于将容器与plt.hist中的xticks对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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