绘制x和y辅助轴时缺少一个辅助标签 [英] One secondary label missing when plotting x and y secondary axis
问题描述
我来自这个问题 Matplotlib:两个x轴和两个y轴,我学会了如何在同一图上绘制两个x
和y
轴.
I'm coming from this question Matplotlib: two x axis and two y axis where I learned how to plot two x
and y
axis on the same plot.
这里是MWE
:
import matplotlib.pyplot as plt
import numpy as np
# Generate random data.
x1 = np.random.randn(50)
y1 = np.linspace(0, 1, 50)
x2 = np.random.randn(20)+15.
y2 = np.linspace(10, 20, 20)
# Plot both curves.
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xlabel('x_1')
ax1.set_ylabel('y_1')
plt.plot(x1, y1, c='r')
ax2 = ax1.twinx().twiny()
ax2.set_xlabel('x_2')
ax2.set_ylabel('y_2')
plt.ylim(min(y2), max(y2))
ax2.plot(x2, y2, c='b')
plt.show()
这是输出:
右侧y
轴和顶部x
轴对应于蓝色曲线.
The right y
axis and the top x
axis correspond to the blue curve.
如您所见,即使定义了第二个y
标签,也将丢失它.我尝试了许多不同的方法,但无法显示出来.我在做错什么吗?
As you can see, the second y
label is missing even though it is defined. I've tried a number of different approaches but I can't get it to show. Am I doing something wrong?
添加:
显然这行有问题:
ax2 = ax1.twinx().twiny()
如果我这样反转它:
ax2 = ax1.twiny().twinx()
然后是第二个x
标签,将不会显示.
then it's the second x
label that will not show.
推荐答案
基本上,正在发生的事情是创建了第三个坐标轴对象,您当前未保留对其的引用. ax2
的可见y轴实际上属于此第三轴对象.
Basically, what's happening is that there's a third axes object created that you're not currently retaining a reference to. ax2
's visible y-axis actually belongs to this third axes object.
您有两种选择.
- 保留对隐藏"轴对象的引用,并设置其y标签.
- 不要使用
twinx
和twiny
,而是在与第一个相同的位置创建轴.
- Retain a reference to the "hidden" axes object and set its y-label.
- Don't use
twinx
andtwiny
and instead create an axes in the same position as the first.
第二个选项稍显冗长,但是具有第二个图的y轴限制将自动缩放的优点.您无需像当前一样手动设置它们.
The second option is a touch more verbose, but has the advantage that the y-axis limits on the second plot will autoscale as you'd expect. You won't need to manually set them as you're currently doing.
无论如何,这是第一种选择的示例:
At any rate, here's an example of the first option:
import matplotlib.pyplot as plt
import numpy as np
# Generate random data.
x1 = np.random.randn(50)
y1 = np.linspace(0, 1, 50)
x2 = np.random.randn(20)+15.
y2 = np.linspace(10, 20, 20)
# Plot both curves.
fig, ax1 = plt.subplots()
ax1.set(xlabel='x_1', ylabel='y_1')
ax1.plot(x1, y1, c='r')
tempax = ax1.twinx()
ax2 = tempax.twiny()
ax2.plot(x2, y2, c='b')
ax2.set(xlabel='x_2', ylim=[min(y2), max(y2)])
tempax.set_ylabel('y_2', rotation=-90)
plt.show()
...这是第二个选项的示例:
...And here's an example of the second option:
import matplotlib.pyplot as plt
import numpy as np
def twinboth(ax):
# Alternately, we could do `newax = ax._make_twin_axes(frameon=False)`
newax = ax.figure.add_subplot(ax.get_subplotspec(), frameon=False)
newax.xaxis.set(label_position='top')
newax.yaxis.set(label_position='right', offset_position='right')
newax.yaxis.get_label().set_rotation(-90) # Optional...
newax.yaxis.tick_right()
newax.xaxis.tick_top()
return newax
# Generate random data.
x1 = np.random.randn(50)
y1 = np.linspace(0, 1, 50)
x2 = np.random.randn(20)+15.
y2 = np.linspace(10, 20, 20)
# Plot both curves.
fig, ax1 = plt.subplots()
ax1.set(xlabel='x_1', ylabel='y_1')
ax1.plot(x1, y1, c='r')
ax2 = twinboth(ax1)
ax2.set(xlabel='x_2', ylabel='y_2')
ax2.plot(x2, y2, c='b')
plt.show()
两者都产生相同的输出:
Both produce identical output:
这篇关于绘制x和y辅助轴时缺少一个辅助标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!