[PYTHON] Use xticks () for pyplot and set_xticklabels () for Axes.

(matplotlib ver3.2.0)

If you want to change the scale character string of the graph drawn by matplotlib, use it properly as the title suggests. I've summarized it because it's a little quirky to use.

Basic syntax

#pyplot.xticks takes the coordinate value of the scale as the first argument and the replacement character string as the second argument.
plt.xticks([1, 2, 3, 4, 5], [ "A", "B", "C", "D", "E"])

#axes.set_xticklabels specifies only the replaced character string
axes.set_xticklabels(["A", "B", "C", "D", "E"])

Similarly, pyplot.yticks () is replaced by axes.set_yticklabels ().

Points to note about axes.set_xticklabels

In axes.set_xticklabels (), it is necessary to specify the entire character string for the scale in sequence, so if there are many scales, it is troublesome when you want to change only some of the scales.

For example, if you plot as follows, fine scales will be added, and it will be very troublesome to set all the character strings.

fig, ax = plt.subplots()
ax.plot([1,2,3,4,5] , [3,4,5,6,7])

Even if you specify as follows in this state, the specified characters will be placed at the positions of 0.5 to 2.5 as shown in the graph below.

ax.set_xticklabels(["A", "B", "C", "D", "E"])

If you do your best to set it up, you can write as follows.

ax.set_xticklabels(["", "A", "", "B", "", "C", "", "D", "", "E"])

The method that seems to be able to simplify the setting a little is summarized in the next section by trial and error.

How to simplify the setting of axes.set_xticklabels

Limit the scale notation in advance

Use axes.set_xticks () to limit the coordinates to the coordinates you want to replace with the character string, and then replace them.

ax.set_xticks([1,2,3,4,5]) 
ax.set_xticklabels(["A", "B", "C", "D", "E"])

Replace using axes.get_xticklabels ()

Since you can get the current scale with axes.get_xticklabels (), replace only the necessary parts with a new character string based on it.

Note that axes.get_xticklabels () is a Text object, so some ingenuity is required. You can access the tick string itself as follows:

for item in ax.get_xticklabels():
    print(item.get_text())

#Output result
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5

From the above, replace the scale string by writing as follows.

fig, ax = plt.subplots()
ax.plot([1,2,3,4,5] , [3,4,5,6,7])
fig.canvas.draw() #Call to determine coordinate values etc.
                  #Get without calling this_xticklabels()I can't get the character string even if I read
                  #By the way, plot.show()If that is the case, the plot will be fixed, so it is useless.

d = {1:"A", 2:"B", 3:"C", 4:"D", 5:"E"} #Define the coordinates and character string to be replaced in the dictionary

#Replace the position of the coordinates registered in the dictionary
#Leave the other positions blank
#(This is a little text. You need to rewrite it according to the context you want to do.)
l = []
for item in ax.get_xticklabels():
    k = float(item.get_text()) 
    if k in d:
        l.append(d[k])
    else:
        l.append("")
ax.set_xticklabels(l)

plt.show()

By the way, with this method, you can leave the minor scale lines without erasing them. Well, it's better to simplify the settings in axes.set_xticklabels above.

Recommended Posts

Use xticks () for pyplot and set_xticklabels () for Axes.
Japanese settings for matplotlib and Seaborn axes
Install tweepy with pip and use it for API 1.1
Use SQLAlchemy and multiprocessing