When you are plotting a time series graph, it can be critical to get the formatting of your dates (often on the X axis) please correct. One complexity is that you may want to different date formats based on the story you’re trying to tell. For example, does it matter whether you have the day of the week? Or just the month? Even down to the minute or separate? Each of these implies a different level of detail about the data, and having fine-grained control over the display is critical.
Getting Matplotlib to format dates
Unfortunately, MPL does not give great instructions for how to deal with the situation. Fortunately, it turned out to be fairly simple once you have grasped the philosophy behind it. Be easiest approach is to leverage the date functionality of pandas pandas, The most useful part of which is periods, which are the objects that do not try to be maximally precise. If you’re using a conventional timestamp, the internal representation is always a specific down to the millisecond if you believe that you have a set of data points that represent separate days, what you actually have is a bunch of very specific points in time at exactly midnight each 24 hours. However, if you’re using D. datatype, you explicitly sets whether you are working in minutes, hours, days, months, or any other unit that you prefer. That gives MPL explicit instructions about how it should display your dates.
Using Pandas for date time formatting
An alternative to this “upstream approach” is to simply set the string representation of the daytime object explicitly. This is very similar to The approach that we used to add a “thousands separator” to the labels along the X axis in this article []. Using the excellent string formatting function , You can decide whether you want the day of the week represented, rather months should be represented as numbers or is abbreviated or four words, and whether full year representations or just the last two numbers should be used. This is precise beyond what you can do with the . Representation discussed above. If you’re making a Pollis graph, it is highly recommended.