[英]Setting position of twin axes not possible
我有這個代碼:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.