matplotlib条形图中的条形之间没有空格

时间:2020-02-26 22:33:34

标签: python matplotlib

我希望生成的图形看起来像直方图,条形之间完全没有空格。我正在使用matplotlib的axes.bar()。我仔细阅读了文档,但在那里没有尝试(设置pad = 0,设置xticks)无济于事。我希望每个条的宽度为0.1,因为那是刻度之间的间隔大小,因此我想完全填满图表。

这是我的代码

colors = self.data[index].colors # a list of the color each bar should have
depth = self.data[index].depth # the depth in meters, increases by set interval (0.1)
data = np.ones(len(cluster_labels))
axis.bar(x=depth, height=data, color=colors, width=0.1, align='edge')
axis.tick_params(axis='x', pad=0)
axis.get_xaxis().set_visible(True)
axis.get_yaxis().set_visible(True)
axis.set_xlim(xmin=self.ruler_pix_to_m(idx_begin), xmax=self.ruler_pix_to_m(idx_end))
axis.set_ylim(ymin=0, ymax=1)
axis.set_axis_on()

我得到的图形是: enter image description here

我想要的是一个图形,其中我只能看到从刻度2.2到刻度16.78的条形,如下所示:

enter image description here

如何指定条形之间完全没有空格?如果matplotlib条形图不起作用,是否还有另一种方法可以获取所需的图类型?任何帮助将不胜感激!

更新:我摆脱了辅助轴并更改了宽度= 1,现在这里的图形如下:

enter image description here

但是,我不希望在图形的末尾有空格,但是尝试使用.set_xlim(xmin = x_start,xmax = x_end)会导致以下结果:

enter image description here

如您所见,x轴完全弄乱了im所使用的x限制,即条形图x轴中的第一个和最后一个数据点,因此我不确定是什么导致范围变小。

1 个答案:

答案 0 :(得分:0)

如果要使用matplotlib创建直方图,则可能应该使用plt.hist(),如果将参数rwidth指定为1.0,则结果图之间的竖线之间将没有垂直间隙。

相关问题