Skip to content

[Bug]: mpl.style.context() stops plotting inline in Jupyter #26716

Open
@iainAtIon

Description

@iainAtIon

Bug summary

Under certain circumstances, when using matplotlib.style.context() to temporarily alter the default style in a Jupyter notebook, it resets the backend to no plot inline. This occurs if the plot is the first one in the notebook, but not if another plot (without the context manager) has already been plotted.

Code for reproduction

# CASE 1:
# first cell - produces inline plot with desired grid
import matplotlibl.pyplot as pl

with plt.style.context({'axes.grid': True}):
    plt.plot(range(10))

# second cell - does not produce in line plot
plt.plot(range(10))

# CASE 2:
# first cell - produces inline plot
import matplotlibl.pyplot as plt
plt.plot(range(10))

# second cell - produces inline plot with desired grid
with plt.style.context({'axes.grid': True}):
    plt.plot(range(10))

# third cell - produces in line plot
plt.plot(range(10))

Actual outcome

CASE 1:
image

CASE 2:
image

Expected outcome

In case 1, the second plot should be inline

Additional information

No response

Operating system

Windows 10.0.19045 Build 19045

Matplotlib Version

3.7.2

Matplotlib Backend

'module://matplotlib_inline.backend_inline'

Python version

3.10.10

Jupyter version

6.4.12

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions