import matplotlib.pyplot as plt x = range(1, 7) y = (220, 300, 300, 290, 320, 315) def test(axes): axes.bar(x, y) axes.set_xticks(x, [i+100 for i in x]) fig, (ax1, ax2) = plt.subplots(1, 2) test(ax1) test(ax2)
I am expecting the xlabs as 101, 102 ...
However, if i switch to use plt.xticks(x, [i+100 for i in x])
and rewrite the function explicitly, it works.
To fix the position of ticks, use set_xticks() function. To set string labels at x-axis tick labels, use set_xticklabels() function.
The Axes. set_xticks() function in axes module of matplotlib library is used to Set the x ticks with list of ticks.
Ticks are the markers denoting data points on axes. Matplotlib has so far - in all our previous examples - automatically taken over the task of spacing points on the axis. Matplotlib's default tick locators and formatters are designed to be generally sufficient in many common situations.
.set_xticks()
on the axes will set the locations and set_xticklabels()
will set the displayed text.
def test(axes): axes.bar(x,y) axes.set_xticks(x) axes.set_xticklabels([i+100 for i in x])
Another function that might be useful, if you don't want labels for every (or even any) tick is axes.tick_params
.
def test(axes): axes.tick_params(axis='x',which='minor',direction='out',bottom=True,length=5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With