0

I try to use matplotlib to plot a dependency on dates (I use dates as X-values). As a result I get a plot with a huge number of x-ticks (dates) and these dates are overlapped such that one cannot recognize what date are there.

I read that the problem can appear of dates are not really date-objects but strings representing the dates (for example '2015-02-12'). However, I have really date-objects and it still does not work.

Here is my code:

plt.figure()
plt.xlim([date_1, date_2])
plt.xticks(rotation=90)
plt.plot(df['col1'].tolist(), df['col2'].tolist(), marker = 'o')
plt.savefig(fname)

1 Answer 1

2

I remember solving this problem by telling python to plot not all the datetimes but only every n-th datetime from the huge list.

In my code I solved this in the following command:

ax.axes.get_xaxis().set_ticks(datetimes[::2])

Notice that it's the [::2] that tells python to take every second element.

You should try different values and look what works best for you either it's 2, 50 or 200.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.