Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对齐直方图条-Python_Python_Alignment_Histogram - Fatal编程技术网

对齐直方图条-Python

对齐直方图条-Python,python,alignment,histogram,Python,Alignment,Histogram,我需要将柱状图的条形图居中 x = array y = [0,1,2,3,4,5,6,7,8,9,10] num_bins = len(array) n, bins, patches = plt.hist(x, num_bins, facecolor='green', alpha=0.5) barWidth=20 x.bar(x, y, width=barWidth, align='center') plt.show() 我需要的是,它看起来像 我几乎什么都试过了,但还是不行。 谢谢大家的

我需要将柱状图的条形图居中

x = array
y = [0,1,2,3,4,5,6,7,8,9,10]

num_bins = len(array)
n, bins, patches = plt.hist(x, num_bins, facecolor='green', alpha=0.5)
barWidth=20
x.bar(x, y, width=barWidth, align='center')

plt.show()
我需要的是,它看起来像

我几乎什么都试过了,但还是不行。
谢谢大家的任务,我认为最好用NumPy计算直方图,用bat函数绘图。请参考以下代码,了解如何使用bin_边

import matplotlib.pyplot as plt
import numpy as np

num_samples = 100
num_bins = 10
lb, ub = 0, 10 # lower bound, upper bound

# create samples
y = np.random.random(num_samples) * ub

# caluculate histogram
hist, bin_edges = np.histogram(y, num_bins, range=(lb, ub))
width = (bin_edges[1] - bin_edges[0])

# plot histogram
plt.bar(bin_edges[:-1], hist, align='center', 
        width=width, edgecolor='k', facecolor='green', alpha=0.5)
plt.xticks(range(num_bins))
plt.xlim([lb-width/2, ub-width/2])

plt.show()