一次更改所有子图的线型

Change the linestyle of all subplots at once

我有一个包含许多子图的图形,我的目标是一次定义所有子图的 linestyle不是单独定义每个子图。这是一个 MWE:

import matplotlib.pyplot as plt
values = [2, 1, 3]
fig, axs = plt.subplots(2)
axs[0].plot(values)
axs[1].plot(values)

我尝试使用

循环轴
for ax in axs.flat:
    ax.set_linestyle('None')

但这引发了错误:“'AxesSubplot' 对象没有属性 'set_linestyle'”。我也试过了

plt.setp(axs, linestyle=...)

但这引发了错误:"Unknown property linestyle"。有什么想法吗?

我找到了一个解决方案:更改 Matplotlib 默认样式设置,但在 mpl.rc_context 内进行,这样其他图形不受影响。

import matplotlib as mpl

with mpl.rc_context():
    mpl.rcParams['lines.linestyle'] = 'None'
    values = [2, 1, 3]
    fig, axs = plt.subplots(2)
    axs[0].plot(values)
    axs[1].plot(values)