简体   繁体   中英

How to set tick label2 in matplotlib

I want to make a plot with one set of numbers for the y axis on the left side, and a different set of numbers for the y axis on the right side. Unfortunately, when I try to set the labels myself, they don't show up in the final figure.

Here's what I've tried.

import matplotlib.pyplot as plt

fig, sub = plt.subplots(1, 1)
sub.set_yticks([5, 10, 15])
for tick, lab in zip(sub.yaxis.get_major_ticks(), [1, 2, 3]):
    tick.label2On = True
    tick.set_label('')
    tick.set_label1('{:3.1f}'.format(lab*3))
    tick.set_label2('{:3.1f}'.format(lab))
    print(tick.label1.get_text(), '\txxx\t', tick.label2.get_text())
# try to force an update of the figure
fig.canvas.draw()
plt.show()

Output

3.0     xxx  1.0
6.0     xxx  2.0
9.0     xxx  3.0

So the text is being updated in the tick objects, but when I call plt.show() , I only see 5, 10, and 15 (the original values) on both sides of the axes:

在此处输入图片说明

I'm going to try to hack something with twinx , but I'd rather know how to do this properly

fig, ax = plt.subplots(figsize=(3,1))
ax.axes.set_xticklabels([1,10])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM