Skip to content

[Bug]: Setting backend in custom style sheet raises UserWarning #20800

Closed
@andthum

Description

@andthum

Bug summary

Importing a custom style sheet where the backend rcParam is set with plt.style.use("custom_style.mplstyle") raises the following warning and the backend remains Agg although it was set to PDF in custom_style.mplstyle.

UserWarning: Style includes a parameter, 'backend', that is not related to style.  Ignoring plt.style.use("custom_style.mplstyle")

This is quite strange, because the latest docs suggest to switch the backend via a (custom) style sheet (https://matplotlib.org/stable/tutorials/introductory/customizing.html, https://matplotlib.org/stable/tutorials/introductory/usage.html#selecting-a-backend) and backend is listed as valid rcParam (https://matplotlib.org/stable/api/matplotlib_configuration_api.html#matplotlib.RcParams).

Other rcParams (e.g. backend_fallback, interactive or savefig.directory) trigger a UserWarning, too.

This is related too Issue #5777 which describes the same problem for the default style sheet.

Code for reproduction

# Content of `custom_style.mplstyle`
backend: PDF


# Content of `plot.py`
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

x = np.arange(-np.pi, np.pi, 0.01)
y = np.sin(x)

plt.style.use("custom_style.mplstyle")
print(matplotlib.get_backend())
plt.plot(x, y)
plt.savefig("plot.pdf")

Actual outcome

The above mentioned UserWarning is raised, the backend remains Agg.

Expected outcome

The PDF backend should be loaded silently.

Operating system

Ubuntu 20.04.2

Matplotlib Version

3.4.2

Matplotlib Backend

Agg, PDF

Python version

3.8.10

Jupyter version

No response

Other libraries

Clean install of matplotlib in a fresh virtual environment:

python3 -m virtualenv env
source env/bin/activate
python3 -m pip install --upgrage pip setuptools wheel
python3 -m pip install --upgrade matplotlib

Installation

pip

Conda channel

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions