Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.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 在matplotlib中,是否有一个参数可以在指定位置稍微调整条?_Python_Matplotlib - Fatal编程技术网

Python 在matplotlib中,是否有一个参数可以在指定位置稍微调整条?

Python 在matplotlib中,是否有一个参数可以在指定位置稍微调整条?,python,matplotlib,Python,Matplotlib,我正在使用plt.bar函数绘制直方图 arr = [1, 1, 2, 2, 2, 5, 5, 3] hist, bin_edges = np.histogram(arr, bins = range(7)) plt.bar(bin_edges[:-1], hist) plt.xlim(min(bin_edges), max(bin_edges)) plt.show() 每个条都位于勾号(1,2,…,5)的正中间,该勾号不清楚地指示边缘,包括左侧,不包括右侧,除了最后一条边缘 参数宽度用于设置

我正在使用plt.bar函数绘制直方图

arr = [1, 1, 2, 2, 2, 5, 5, 3]
hist, bin_edges = np.histogram(arr, bins = range(7))
plt.bar(bin_edges[:-1], hist)
plt.xlim(min(bin_edges), max(bin_edges))
plt.show()

每个条都位于勾号(1,2,…,5)的正中间,该勾号不清楚地指示边缘,包括左侧,不包括右侧,除了最后一条边缘

参数宽度用于设置钢筋的宽度,而不是位置


在matplotlib中是否有一个参数可以将每个条稍微放在指定的位置(如0.5)?

您有三个选项。你可以选择你认为最合适、最容易理解的

选项1:向左(-0.5)或向右(+0.5)移动条中心(x位置)0.5,然后将x记号设置为0.5、1.5、2.5,依此类推

arr = [1, 1, 2, 2, 2, 5, 5, 3]
hist, bin_edges = np.histogram(arr, bins = range(7))
plt.bar(bin_edges[:-1]-0.5, hist)
plt.xlim(min(bin_edges), max(bin_edges))
plt.ticks(bin_edges[:-1]-0.5)
plt.show()

选项2:使用
align=edge
,默认情况下,该选项将与记号的右侧对齐,并假定一些默认厚度(如下图中的0.8)

选项3:使用定义宽度的
align=edge
(正宽度将向右对齐,负宽度将向左对齐)


您有三种选择。你可以选择你认为最合适、最容易理解的

选项1:向左(-0.5)或向右(+0.5)移动条中心(x位置)0.5,然后将x记号设置为0.5、1.5、2.5,依此类推

arr = [1, 1, 2, 2, 2, 5, 5, 3]
hist, bin_edges = np.histogram(arr, bins = range(7))
plt.bar(bin_edges[:-1]-0.5, hist)
plt.xlim(min(bin_edges), max(bin_edges))
plt.ticks(bin_edges[:-1]-0.5)
plt.show()

选项2:使用
align=edge
,默认情况下,该选项将与记号的右侧对齐,并假定一些默认厚度(如下图中的0.8)

选项3:使用定义宽度的
align=edge
(正宽度将向右对齐,负宽度将向左对齐)

plt.bar(bin_edges[:-1], hist, align='edge', width=-0.5)