ax.set_axisbelow(True)可以使用或plt.rc('axes', axisbelow=True)(其他堆栈问题)将网格线设置在填充区域的图后面。但使用时alpha<1网格线也会出现在前面。有没有办法仍然隐藏网格线或应用选择性alpha混合?我正在考虑一种基于对象的方法,其中指定对象 a 和 b 之间的 alpha。
答案应该也适用于fill_between。
重现问题的示例:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2022)
x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)
kwargs = dict(histtype='stepfilled', alpha=.3, density=True, bins=40)
fig, ax = plt.subplots(figsize=(9, 6))
ax.hist(x1, **kwargs)
ax.hist(x2, **kwargs)
ax.hist(x3, **kwargs)
ax.set_axisbelow(True)
ax.yaxis.grid(color='gray', linestyle='dashed')
ax.xaxis.grid(color='gray', linestyle='dashed')
Run Code Online (Sandbox Code Playgroud)