Joe
Joe

Reputation: 1768

Change the linestyle of all subplots at once

I have a figure with many subplots and my goal is to define the linestyle of all subplots at once, not for each subplot individually. Here is a MWE:

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

I tried looping through the axes with

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

but this threw the error: "'AxesSubplot' object has no attribute 'set_linestyle'". I also tried

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

but this threw the error: "Unknown property linestyle". Any ideas?

Upvotes: 0

Views: 1018

Answers (1)

Joe
Joe

Reputation: 1768

I found a solution: change the Matplotlib default style settings, but do it inside a mpl.rc_context such that other figures are not affected.

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)

Upvotes: 1

Related Questions