我有这个代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
a = np.array([1,2,3])
b = a
ax1.plot(a,b)
ax2 = ax1.twinx()
ax2.set_position(matplotlib.transforms.Bbox([[0.125, 0.125], [0.9, 0.2]]))
c = np.array([4,5,6])
d = c
ax2.plot(c,d)
plt.show()
当我用 Python 2 运行它时,结果是:
问题是当我尝试使用 Python 3 使用相同的代码时,我得到了这张图片:
如何使用 Python 3 获得相同的结果?
最佳答案
这是一个错误,现在已经修复(因此它与 python 版本无关,而是与正在使用的 matplotlib 版本有关)。您可以使用 inset_axes 而不是通常的子图。后者可能看起来像这样:
import numpy as np
from matplotlib.transforms import Bbox
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111, label="first")
ax2 = fig.add_subplot(111, label="second")
ax2.set_position(Bbox([[0.125, 0.125], [0.9, 0.2]]))
ax1.get_shared_x_axes().join(ax1, ax2)
ax2.yaxis.tick_right()
ax2.tick_params(bottom=False, labelbottom=False)
ax2.set_facecolor("none")
a = np.array([1,2,3])
ax1.plot(a,a)
c = np.array([4,5,6])
ax2.plot(c,c)
plt.show()
关于python - 不能设置双轴位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56743959/