【发布时间】:2019-09-01 02:21:23
【问题描述】:
以下脚本创建了一个 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()
我正在寻找一种将网格线放在蓝色直方图后面的方法,并在matplotlib/matplotlib#7984 发现了一个相关问题。它说
您不能将一个 Axes 的艺术家的绘图顺序与另一个 Axes 的艺术家的绘图顺序交错
这就解释了为什么ax2.set_axisbelow(True) 对主Axes 没有影响。
我能以某种方式实现我的目标吗?欢迎使用变通方法(我想根据上面的引用没有规范的解决方案)。
【问题讨论】:
标签: python matplotlib