我在一个图中有两组矩形补丁。我想把它们分开命名。底部为"Layer-1“,上部为类似的"Layer-2”。我想设置Y轴的坐标,但它不起作用。此外,我无法在标签中添加"Layer-2“文本。请帮帮忙。我尝试使用下面提到的代码,但它不起作用。
plt.ylabel("LAYER-1", loc='bottom')
yaxis.labellocation(bottom)

发布于 2020-08-05 18:29:40
一种解决方案是创建第二个轴,即所谓的双轴,它共享相同的x轴。那么就可以单独给它们贴上标签。此外,您可以通过axis.yaxis.set_label_coords(-0.1,0.75)调整标签的位置
这里有一个示例,您可以根据自己的需求进行调整。结果可以在这里找到:https://i.stack.imgur.com/1o2xl.png
%matplotlib notebook
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
plt.rcParams['figure.dpi'] = 100
import matplotlib.pyplot as plt
x = np.arange(0, 10, 0.1)
y1 = 0.05 * x**2
y2 = -1 *y1
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(x, y1, 'g-')
ax2.plot(x, y2, 'b-')
# common x axis
ax1.set_xlabel('X data')
# First y axis label
ax1.set_ylabel('LAYER-1', color='g')
# Second y [enter image description here][1]axis label
ax2.set_ylabel('LAYER-2', color='b')
# Adjust the label location
ax1.yaxis.set_label_coords(-0.075, 0.25)
ax2.yaxis.set_label_coords(-0.1, 0.75)
plt.show()https://stackoverflow.com/questions/63262503
复制相似问题