Can't change xticks and allign them with with x-labels

Viewed 27

I'm creating plot with several levels in x-axis. My problem is that I can't find a way to alling them with dots on the plot

def add_line(ax, xpos, ypos):
line = plt.Line2D([xpos, xpos], [ypos + .05, ypos],
                  transform=ax.transAxes, color='gray')
line.set_clip_on(False)
ax.add_line(line)


def label_len(my_index, level):
    labels = my_index.get_level_values(level)
    return [(k, sum(1 for ii in g)) for k, g in groupby(labels)]


def label_group_bar_table(ax, df):
    ypos = -.05
    scale = 1. / df.index.size
    for level in range(df.index.nlevels)[::-1]:
        pos = 0
        for label, rpos in label_len(df.index, level):
            lxpos = (pos + .5 * rpos) * scale
            ax.text(lxpos, ypos, label, ha='center', transform=ax.transAxes, fontsize='x-small',
                    fontstretch='extra-condensed', in_layout=True)
            add_line(ax, pos * scale, ypos)
            pos += rpos
        add_line(ax, pos * scale, ypos)
        ypos -= .05

leng = 0
scale = 1. / df_test.index.size
pos = 0
for label, rpos in label_len(df_test.index, 1):
    single = (pos + .5 * rpos) * scale
    leng += (pos + .5 * rpos) * scale
singlelen = leng / df_test.index.size

x = np.arange(0, leng, single)
ax = df_test.plot(marker='o', markersize=5, linestyle='none', fillstyle='full')
plt.xticks(x) # ticks change

ax.set_xticklabels('')
ax.set_xlabel('')
label_group_bar_table(ax, df_test)
plt.tight_layout()
plt.show()

I'm using this code to generate a plot. My goal is to have the same distance between dots and labels on first level of x-axis labels. This is how it looks right now and as You can see they start ,,later" than x-axis and have less space between them. Why xticks don't change what I did wrong ? Thanks in advance

0 Answers
Related