Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add unit to yaxis labels in MatPlotLib

I am trying to add mi or km (miles, kilometers) after the value on the yaxis of a matplotlib bar chart.

Right now I am just supplying matplotlib the values and it is making the yaxis labels automatically. I can't figure out how to append mi to the end of a value.

24 > 24 mi

There is an option for ax.set_7ticklabels(), but then I would need to set them statically.

like image 877
jhanifen Avatar asked Oct 13 '10 19:10

jhanifen


People also ask

How do I change the y axis values in matplotlib?

To specify the value of axes, create a list of characters. Use xticks and yticks method to specify the ticks on the axes with x and y ticks data points respectively. Plot the line using x and y, color=red, using plot() method. Make x and y margin 0.

What does PLT axis () do?

The plt. axis() method allows you to set the x and y limits with a single call, by passing a list which specifies [xmin, xmax, ymin, ymax] : In [11]: plt.


1 Answers

Are you wanting something like this?

import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter

x = range(10)
plt.plot(x)

plt.gca().xaxis.set_major_formatter(FormatStrFormatter('%d km'))

plt.show()

X-axis ticks labeled with units

like image 97
Joe Kington Avatar answered Oct 12 '22 21:10

Joe Kington