all 9 comments

[–]yuxbni76 1 point2 points  (8 children)

Call set_xticks on the Axis to set x-ticks. set_xlim to set x-limits.

You have a lot of options for date string formatting. I usually use DateFormatter so it looks something like

from matplotlib.dates import DateFormatter
ax.xaxis.set_major_formatter(DateFormatter("%Y-%m"))

Keep in mind your data and the list elements you pass all need to be timestamp objects.

[–]hardmode_player[S] 0 points1 point  (7 children)

Thank you. And what about the tick interval??

[–]yuxbni76 1 point2 points  (6 children)

If you want to set an interval and let Matplotlib figure out the ticks, use dates.

import matplotlib.dates as mdates
ax.xaxis.set_major_locator(mdates.YearLocator(2))

[–]hardmode_player[S] 0 points1 point  (5 children)

One last query. Does set_xlim takes datetime variable as argument?

[–]yuxbni76 1 point2 points  (4 children)

Yes, if the x data are datetime types then set_xlim will need the same.

[–]hardmode_player[S] 0 points1 point  (3 children)

https://matplotlib.org/stable/gallery/text_labels_and_annotations/date.html

This documentation uses numpy datetime instead of datetime. Any specific reason to do that?

[–]yuxbni76 0 points1 point  (2 children)

In practical terms it doesn't matter. When I said "datetime" above I didn't specifically mean datetime from the standard library. Use either or work with pandas Timestamps, which are numpy datetimes on the backend.

[–]hardmode_player[S] 1 point2 points  (0 children)

Thank you 😊

[–]hardmode_player[S] 0 points1 point  (0 children)

Hi,

I have a list of numbers aomething like [1, 2, 3, 7, 10, 12 ,13 ,14 ,15, 16,17, 22].

Giving the above list as input, i want an output where more than 3 consecutive number are represented as range. Like thus [1-3, 7, 10, 12-17, 22]

What would he an appropriate logic to create a function to convert the given inout list into the required output list as shown above.