以下脚本创建一个Figure在辅助网格线后面带有蓝色直方图的实例,辅助网格线本身位于橙色累积直方图后面。
import matplotlib.pyplot as plt
import numpy as np
plt.style.use("seaborn-darkgrid")
np.random.seed(42)
foo = np.random.randn(1000)
fig, ax = plt.subplots()
ax.hist(foo, bins=50)
ax2 = ax.twinx()
ax2.hist(
foo, bins=50, density=True, cumulative=True, histtype="step", color="tab:orange"
)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种将网格线放在蓝色直方图后面的方法,并在matplotlib/matplotlib#7984中发现了相关问题。它说
您不能将一个轴的艺术家的绘图顺序与另一轴的艺术家的绘图顺序交错
这解释了为什么ax2.set_axisbelow(True)对 Primary 没有影响Axes。
我可以通过某种方式实现我的目标吗?欢迎使用解决方法(我想根据上面的引用没有规范的解决方案)。