Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: Add strings as custom x-ticks but also keep existing (numeric) tick labels? Alternatives to matplotlib.pyplot.annotate?

Tags:

I am trying to produce a graph and I am having some issues annotating it.

My graph has a log scale on the x-axis, showing time. What I want to be able to do is keep the existing (but not predictable) numeric tick labels at 100 units, 1000 units, 10000 units, etc but also add custom tick labels to the x-axis that make it clear where more "human readable" time intervals occur---for instance I want to be able to label 'one week', 'one month', '6 months', etc.

I can use matplotlib.pyplot.annotate() to mark the points but it doesn't really do what I want. I don't really want text and arrows on top of my graph, I just want to add a few extra custom tick marks. Any ideas?

like image 360
FakeDIY Avatar asked May 16 '12 09:05

FakeDIY


People also ask

How do you change X ticks in MatPlotLib?

To change the default x ticks, use set_xticks() function in Matplotlib.

How do I add x-axis labels in MatPlotLib?

Use the xlabel() method in matplotlib to add a label to the plot's x-axis.


1 Answers

If you really want to add extra ticks, you can get the existing ones using axis.xaxis.get_majorticklocs(), add whatever you want to add, and then set the ticks using axis.xaxis.set_ticks(<your updated array>).

An alternative would be to add vertical lines using axvline. The advantage is that you don't have to worry about inserting your custom tick into the existing array, but you'll have to annotate the lines manually.

Yet another alternative would be to add a linked axis with your custom ticks.

like image 73
ev-br Avatar answered Sep 22 '22 20:09

ev-br