Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why set_xticks doesn't set the labels of ticks?

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) 

enter image description here

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.

like image 408
colinfang Avatar asked Feb 20 '14 14:02

colinfang


People also ask

How do you change a tick label in Python?

To fix the position of ticks, use set_xticks() function. To set string labels at x-axis tick labels, use set_xticklabels() function.

What does ax Set_xticks do?

The Axes. set_xticks() function in axes module of matplotlib library is used to Set the x ticks with list of ticks.

What are ticks Matplotlib?

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.


2 Answers

.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]) 

enter image description here

like image 146
Hooked Avatar answered Oct 14 '22 21:10

Hooked


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) 

enter image description here

like image 39
groceryheist Avatar answered Oct 14 '22 21:10

groceryheist