Question

I want to plot weather data over the span of several days every half hour, but I only want to label the days at the start as a string in the format 'mm/dd/yy'. I want to leave the rest unmarked.

I would also want to control where such markings are placed along the x axis, and control the range of the axis.

I also want to plot multiple sets of measurements taken over different intervals on the same figure. Therefore being able to set the axis and plot the measurements for a given day would be best.

Any suggestions on how to approach this with matplotlib?

Was it helpful?

Solution

It depends somewhat on whether you're using the procedural (MATLAB-like) or object-oriented interface. In either case, say you wanted to label 1, 2.5, and 4 with a, b, and c. With the procdeual interface, you could call:

plt.xticks([1, 2.5, 4], ["a", "b", "c"])

In the latter case, you would do:

ax = plt.gca()
ax.set_xticks([1, 2.5, 4])
ax.set_xticklabels(["a", "b", "c"])

or, in one step:

ax = plt.gca()
ax.set(xticks=[1, 2.5, 4], xticklabels=["a", "b", "c"])

OTHER TIPS

You can use a DayLocator as in: plt.gca().xaxis.set_major_locator(dt.DayLocator())

And DateFormatter as in: plt.gca().xaxis.set_major_formatter(dt.DateFormatter("%d/%m/%Y"))

Note: import matplotlib.dates as dt

Matplotlib xticks are your friend. Will allow you to set where the ticks appear.

As for date formatting, make sure you're using dateutil objects, and you'll be able to handle the formatting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top