如何使xticklabel只有数据列表的点?

时间:2018-02-04 16:35:16

标签: python matplotlib tkinter

我正在创建一个python应用程序来显示甘特图数据list。但是当我运行应用程序时,我得到了这个 - >

enter image description here

实际上,我需要xticklabels只应该是图表中同一终点的data列表中的点。类似的东西 - > (对不起的图片编辑感到抱歉)

enter image description here

这是我到目前为止所做的:

self.f = Figure(figsize=(10, 8), dpi=100)
self.ax = self.f.add_subplot(2,1,2)

ind = numpy.arange(1)  # the x locations for the groups
width = .95

data = [2,4,5,7,8,12,16]

patch_handles = []

left = 1

for i, d in enumerate(data):
    patch_handles.append(self.ax.barh(ind, d, align='center', left=left, yerr=0.4, color='y'))
    left += d

for j in range(len(patch_handles)):
    for i, patch in enumerate(patch_handles[j].get_children()):
        bl = patch.get_xy()
        x = 0.5 * patch.get_width() + bl[0]
        y = 0.5 * patch.get_height() + bl[1]
        self.ax.text(x, y, "dd", ha='center')

self.ax.set_yticks(ind)
self.ax.set_yticklabels("G")
self.ax.set_xticklabels(data)
self.ax.set_xlabel('Scores')

canvas = FigureCanvasTkAgg(self.f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)

编辑:更改@scroopy noopers的代码后的屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用set_xticks设置xticks位置(除了使用set_xticklabels设置刻度位置显示的值):

self.ax.set_yticks(ind)
self.ax.set_yticklabels("G")
self.ax.set_xticks(numpy.cumsum(data))  # Set positions correctly
self.ax.set_xticklabels(data)
self.ax.set_xlabel('Scores')

this post启发的答案。

编辑:正如OP在下面评论的那样,这导致了他们特定情节的一个一个错位。他们能够通过简单的调整来解决错位:

self.ax.set_xticks(numpy.cumsum(data) + 1)