您提供的代码似乎产生了预期的结果。
这使我认为您的控制台或matplotlib版本存在问题-也许您可以提供有关如何运行代码的更多信息。
我建议移动
ax.set_xlabel之前
twinx,例如:
ax_2 = fig.add_subplot(222, sharex=None, sharey=None)ax_22 = ax_2.twinx()ax_2.set_xlabel("AX2 X Lablel")ax_2.plot([1, 3, 5, 7, 9])# Becomes...ax_2 = fig.add_subplot(222, sharex=None, sharey=None)ax_2.set_xlabel("AX2 X Lablel")ax_2.plot([1, 3, 5, 7, 9])ax_22 = ax_2.twinx()编辑
我建议改用gridspec。请参见以下工作示例:
import matplotlib.pyplot as pltimport matplotlib.gridspec as gspecimport numpy as npfig = plt.figure()gs = gspec.GridSpec(2, 2)gs.update(hspace=0.7, wspace=0.7)ax1 = plt.subplot(gs[0, 0])ax2 = plt.subplot(gs[1, 0])ax3 = plt.subplot(gs[0, 1])ax4 = plt.subplot(gs[1, 1])x1 = np.linspace(1,10,10)ax1.plot(x1, x1**2)ax1.set_xlabel('ax1 x')ax1_2 = ax1.twinx()ax1_2.plot(x1, x1**3)ax1_2.set_ylabel('ax1_2 y')ax1.set_ylabel('ax1 y')# To save time I left the other cells blank, but it should work fine.plt.show()上面产生了这个:



