简体   繁体   English

如何在matplotlib中设置刻度标签2

[英]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. 我想绘制一个图,左侧的y轴有一组数字,右侧的y轴具有一组不同的数字。 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: 因此,文本正在tick对象中进行更新,但是当我调用plt.show() ,在轴的两边只看到5、10和15(原始值):

在此处输入图片说明

I'm going to try to hack something with twinx , but I'd rather know how to do this properly 我将尝试用twinx破解某些东西,但我宁愿知道如何正确执行此操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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