我正在根据气候模型制作降水率的直方图,并且直方图功能起作用,但x轴让我感到非常轻微。我想在每个箱子之间直接打勾,比如2.55。然而,一些蜱被关闭,主要在左侧。有什么办法可以让它们正确对齐吗?
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
答案 0 :(得分:2)
plt.hist
选择bins
选项,它可以是整数(在脚本中),也可以是bin边缘列表。因此,您可以使用已定义为x
的bin边缘范围作为此bins
选项,以设置您感兴趣的精确bin边缘。
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