Sometimes, we want to format axis offset-values to whole numbers or specific number with Python matplotlib.
In this article, we’ll look at how to format axis offset-values to whole numbers or specific number with Python matplotlib.
How to format axis offset-values to whole numbers or specific number with Python matplotlib?
To format axis offset-values to whole numbers or specific number with Python matplotlib, we can custom the labels with functions.
For instance, we write
from pylab import *
x = linspace(55478, 55486, 100)
y = random(100) - 0.5
y = cumsum(y)
y -= y.min()
y *= 1e-8
plot(x,y)
locs, labels = xticks()
xticks(locs, map(lambda x: "%g" % x, locs))
locs,labels = yticks()
yticks(locs, map(lambda x: "%.1f" % x, locs*1e9))
ylabel('microseconds (1E-9)')
show()
to plot the values from the x and y arrays.
Then we call xticks with the locs list and we call map with the a function to return the values of the locs values formatted our way.
Likewise, we do the same with the y-axis labels by call yticks with locs and map with a function and locs*1e9 to format the labels for the y-axis labels.
Conclusion
To format axis offset-values to whole numbers or specific number with Python matplotlib, we can custom the labels with functions.