简体   繁体   English

在 matplotlib 中旋转第二个 y 轴刻度标签

[英]Rotating 2nd y-axis tick labels in matplotlib

I need to rotate the 2nd y-axis ticklabel and add a label for this axis as well in the figure below我需要旋转第二个 y 轴刻度标签并在下图中为此轴添加标签

import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import matplotlib.transforms as mtransforms

fig, ax1 = plt.subplots(constrained_layout=True)
x = [61,62,62,59,62,59,62,63,61,60,103,104,109,105,109,105,109,111,110,107]
y = [62,62,62,62,60,60,62,62,62,63,106,107,106,106,105,105,105,106,107,108]
ax1.plot(x,y,'b.')
x2 = [2.2,3.4,4.3,5.1,5.5,5.7]
y2 = [2.3,2.8,3.2,3.9,4.5,5.9]
ax2 = ax1.twinx().twiny()
ax2.tick_params(axis="y",labelrotation=90,direction='out',length=6, width=2, colors='r',grid_color='r', grid_alpha=0.5) #called tick_params before the plot and didn't work
ax2.plot(x2,y2,'r.')
ax2.set_xlim(0,10)
ax2.set_ylim(0,10)
ax2.set_yticklabels(['Label1', 'Label2', 'Label3'], rotation=90) #y ticklabels is not rotating
ax2.set_xlabel('abc', rotation=0, fontsize=20, labelpad=20)
ax2.set_ylabel('abc', rotation=0, fontsize=20, labelpad=20)   #y label is not wroking

plt.yticks(rotation=90)

line = mlines.Line2D([0, 1], [0, 1], color='red')
transform = ax2.transAxes
line.set_transform(transform)
ax2.add_line(line)
plt.show()

This code produced the figure below这段代码产生了下图

这段代码产生了下图

The problem is ax2.set_yticklabels and ax2.set_ylabel don't work.问题是 ax2.set_yticklabels 和 ax2.set_ylabel 不起作用。

I want to add a label to 2nd y-axis and rotate the tick label for that axis.我想在第二个 y 轴上添加一个标签并旋转该轴的刻度标签。 Also, how to control the position of the tick mark at these axes, I want it to be at the same position of tick marks of 1st y-axis and 1st x-axis.另外,如何控制刻度线在这些轴上的位置,我希望它与第 1 个 y 轴和第 1 个 x 轴的刻度线位置相同。 So Label1 will shift up and 0 will shift right所以 Label1 会上移而 0 会右移

Thanks谢谢

When you are instancing ax2 = ax1.twinx().twiny() , you can no longer modify the y axis.当您实例化ax2 = ax1.twinx().twiny() ,您不能再修改 y 轴。 Instead, create two axes and modify accordingly.相反,创建两个轴并进行相应修改。 Modified code and the result is below.修改代码,结果如下。

import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import matplotlib.transforms as mtransforms

fig, ax1 = plt.subplots(constrained_layout=True)
x = [61,62,62,59,62,59,62,63,61,60,103,104,109,105,109,105,109,111,110,107]
y = [62,62,62,62,60,60,62,62,62,63,106,107,106,106,105,105,105,106,107,108]
ax1.plot(x,y,'b.')
x2 = [2.2,3.4,4.3,5.1,5.5,5.7]
y2 = [2.3,2.8,3.2,3.9,4.5,5.9]
ax2 = ax1.twinx()  # ax2 handles y
ax3 = ax2.twiny()  # ax3 handles x
ax3.plot(x2,y2,'r.')
ax3.set_xlim(0,10)
ax2.set_ylim(0,10)
ax3.set_xlabel('abc', rotation=0, fontsize=20, labelpad=20)
ax2.set_ylabel('abc', rotation=0, fontsize=20, labelpad=20)   
ax2.tick_params(axis="y",labelrotation=90,direction='out',length=6, width=2, colors='r',grid_color='r', grid_alpha=0.5)
ax2.set_yticklabels(['Label1', 'Label2', 'Label3'], rotation=-90)     
plt.yticks(rotation=90)
line = mlines.Line2D([0, 1], [0, 1], color='red')
transform = ax2.transAxes
line.set_transform(transform)
ax2.add_line(line)
plt.show()

The resulting graph has all the y label/tick modifications.结果图具有所有 y 标签/刻度修改。

在此处输入图片说明

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

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