0

I have the following code which produces a plot.

labels = sorted(set(df.index))
a = df.loc[df.user_id ==1234, 'count']
b = df.loc[df.user_id ==5678, 'count']
width = 0.5
x = np.arange(len(labels))  # the label locations


fig, ax = plt.subplots(figsize=(10,5))
rects1 = plt.plot_date(x, a, width, label='a')
rects2 = plt.plot_date(x, b, width, label='b', color = 'orange')

ax.set_xticks(x)
ax.set_xticklabels(labels, rotation=90)
ax.legend(['a','b'])
plt.ylabel('counts')

fig.tight_layout()
plt.gcf().autofmt_xdate()
plt.show()

The obvious problem is not being able to read the densely packed x labels. Without changing the plot, how can I label by week rather than day?

enter image description here

LeoGER
  • 355
  • 1
  • 8
  • 1
    Check this https://stackoverflow.com/a/61299822/7148638 – Rajat Mishra May 06 '20 at 11:23
  • 1
    I found the following solution https://stackoverflow.com/a/46343722/12934163 : ```n = 7 # Keeps every 7th label [l.set_visible(False) for (i,l) in enumerate(ax.xaxis.get_ticklabels()) if i % n != 0]``` – TiTo May 06 '20 at 11:25

1 Answers1

0
plt.xticks(range(0,len(label), 7), label[::7])

given a daily data this show ticks every 7 days

Marco Cerliani
  • 21,233
  • 3
  • 49
  • 54