matplotlib / matplotlib/matplotlib

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

Open
#26,716 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: backends
Dominant language
Python
Stars
23.2k
Forks
8.5k
Avg merge
1d 6h
Merged PRs (30d)
66

Description

### 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

```python
# 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](https://github.com/matplotlib/matplotlib/assets/143531681/d885aa2b-9f65-4df3-afcb-485cae9b2714)

CASE 2:
![image](https://github.com/matplotlib/matplotlib/assets/143531681/b82b505e-4629-4ba0-b2a1-06cbf8012665)

### 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the two cases in a Jupyter notebook with the reported Matplotlib, Python, and inline backend versions. Start at matplotlib.style.context() and its interaction with module://matplotlib_inline.backend_inline; done means the second plot in case 1 remains inline, with a regression test covering the sequence.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter, python
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.