首页 > 解决方案 > matplotlib 中的 zorder 与共享 x_axis 没有放在首位

问题描述

这是我的代码,输出图:

fig, ax1 = plt.subplots()


color = 'tab:red'
ax1.set_xlabel('Iterations')
ax1.set_ylabel('Non metric loss', color=color)
ax1.plot(net_loss_bigger, color=color, zorder=3)
ax1.tick_params(axis='y', labelcolor=color)

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

color = 'tab:blue'
ax2.set_ylabel('metric supervised loss', color=color)  # we already handled the x-label with ax1
ax2.plot(metric_super_loss_bigger, color=color, zorder=2)
ax2.tick_params(axis='y', labelcolor=color)

在此处输入图像描述
想法我做错了什么?
没有共享ax1.twinx()它按预期工作。解决方法?

标签: pythonmatplotlib

解决方案


添加

ax1.set_zorder(ax2.get_zorder()+ax3.get_zorder()+1)
ax1.patch.set_visible(False)

在此处输入图像描述

执行所需的功能


推荐阅读