Skip to content

[Bug]: fig.set_dpi() and figure.dpi option in matplotlibrc file not working #24644

Open
@BearBearCodes

Description

@BearBearCodes

Bug summary

  • fig.set_dpi(150) only changes the dpi of plot shown in the Jupyter notebook but not in the saved figure
  • figure.dpi in the matplotlibrc file does not work at all (EDIT: bug occurs in matplotlib-inline 0.1.3 but this is fixed in matplotlib-inline 0.1.6)
  • plt.subplots(dpi=150) works as expected and changes the dpi of both the plot shown in the Jupyter notebook as well as in the saved figure

Continuation of discussion in #11227

Code for reproduction

import matplotlib as mpl
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
fig.set_dpi(150)
ax.plot([1, 2], [1, 2])
print(fig.get_dpi())
print(mpl.rcParams["figure.dpi"])
print(mpl.rcParams["savefig.dpi"])
fig.savefig("test.png", dpi="figure")
plt.show()

Actual outcome

Inside a Jupyter notebook running python 3.10.4 inside a conda environment (no matplotlibrc file):

150
72.0
figure

image
Saved plot is at 72.0 dpi.

Inside a standalone Python script running the same python version inside a conda environment (no matplotlibrc file):

150
100.0
figure

Saved plot is at 100.0 dpi.

Expected outcome

  • fig.set_dpi(150) would set the dpi to 150 for the figure shown in the Jupyter notebook + the figure shown in the matplotlib window that pops up when plt.show() is called as well as the figure saved.

  • figure.dpi in the matplotlibrc file would change the default dpi of figures created (but it does not). From my original comment in #11227:

    Moreover, I'm finding that on my machine, the figure.dpi rcParam is not respected after I have modified it in my matplotlibrc file (located at ~/.config/matplotlib/matplotlibrc). Other changes to options like axes.prop_cycle in my matplotlibrc file are being reflected in the plots shown in the Jupyter notebook as well as in those saved via fig.savefig(), but changes to the figure.dpi option are not reflected in the plots shown/saved

  • The default dpi should be 100.0 instead of 72.0 (only seems to happen inside Jupyter notebooks and not in standalone python scripts)

  • (similar to point #1) fig.savefig("test.png", dpi="figure") should reflect the changed dpi that was set with fig.set_dpi(150). Instead, it saves the figure at 72.0 dpi (when running the code in a Jupyter notebook) or 100.0 dpi (when running the code in a standalone Python script). Both the Jupyter notebook and Python script are running the same Python version in the same conda environment.

Additional information

Previous discussion in #11227

Operating system

WSL2 running Ubuntu 20.04.5 LTS

Matplotlib Version

3.5.2

Matplotlib Backend

Inside Jupyter notebook (same regardless whether or not matplitlibrc file exists): module://matplotlib_inline.backend_inline.

Inside Python script with no matplotlibrc file: QtAgg

Inside Python script with matplotlibrc file: TkAgg (since I set my backend to TkAgg in my matplotlibrc file)

Python version

3.10.4

Jupyter version

6.5.2

Installation

conda 22.9.0

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