Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib: Centering period labels under the period's data in a time-series plot

Tags:

matplotlib's axis-formatting options tend to fall flat when it comes to plotting and effectively labeling dense time-series data.

One problem is that tick labels are tied to ticks, so if you set axis ticks at an appropriate frequency, there are usually too many labels. This also means that if you are plotting, say, daily data over a period of several years, there is no good way to label the x-axis with each year in its natural position: centered under the year's data (i.e., under the x-axis position for July 2, or thereabouts).

The trick described in this example—set major ticks where you want them, then use invisible minor ticks to place the labels elsewhere—works, but it limits you to one visible set of axis ticks (since each axis is limited to one set of major and one set of minor ticks). You can't show, say, major ticks at the start of each year and minor ticks at the start of each month without giving up the ability to put year labels centered appropriately between the major (yearly) ticks, as you would find in publication-quality plots.

Is there a work-around that doesn't involve drawing everything fully manually?

like image 991
pash Avatar asked Jun 07 '11 02:06

pash


1 Answers

Have you looked at the tsplot capability in scikits.timeseries? It hasn't been maintained much recently, but it works pretty well. I'll be porting that code into pandas at some point in the relative near future.

like image 94
Wes McKinney Avatar answered Dec 26 '22 04:12

Wes McKinney