Skip to content

[Bug]: Theme properties are not properly forwarded to xkcd plot #27791

Open
@cgadal

Description

@cgadal

Bug summary

Some theme properties are not properly forwarded to xkcd plot.

Also, the displayed figure is not identical to the one saved.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import patheffects

plt.style.use(["dark_background"])


def make_figure():
    fig = plt.figure()
    ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))
    ax.spines[["top", "right"]].set_visible(False)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_ylim([-30, 10])

    data = np.ones(100)
    data[70:] -= np.arange(30)

    ax.annotate(
        "THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED",
        xy=(70, 1),
        arrowprops=dict(arrowstyle="->"),
        xytext=(15, -10),
    )

    ax.plot(data)

    ax.set_xlabel("time")
    ax.set_ylabel("my overall health")
    fig.text(0.5, 0.05, '"Stove Ownership" from xkcd by Randall Munroe', ha="center")


with plt.xkcd():
    # This figure will be in XKCD-style
    make_figure()
    # ...

# This figure will be in regular style
make_figure()

plt.show()

Actual outcome

Shown figures

image

Saved figures

xkcd_fig
regular_fig

Expected outcome

By updating the relevant rcparams after the plt.xkcd() call, I can update the figure properly :

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import patheffects

plt.style.use(["dark_background"])


def make_figure(type):
    fig = plt.figure()
    ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))
    ax.spines[["top", "right"]].set_visible(False)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_ylim([-30, 10])

    data = np.ones(100)
    data[70:] -= np.arange(30)

    ax.annotate(
        "THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED",
        xy=(70, 1),
        arrowprops=dict(arrowstyle="->"),
        xytext=(15, -10),
    )

    ax.plot(data)

    ax.set_xlabel("time")
    ax.set_ylabel("my overall health")
    fig.text(0.5, 0.05, f"{type} type of figure", ha="center")


with plt.xkcd():
    # This figure will be in XKCD-style
    plt.rcParams["path.effects"] = [patheffects.withStroke(linewidth=0)]
    plt.rcParams["figure.facecolor"] = "black"
    plt.rcParams["axes.edgecolor"] = "white"
    make_figure("xkcd")
    # ...
plt.savefig("xkcd_fig.png")

# This figure will be in regular style
make_figure("regular")
plt.savefig("regular_fig.png")


plt.show()

shown figures

image

saved figures

regular_fig
xkcd_fig

EDIT: moving the plt.style.use("dark_background") result in a mix between the rcparams of the used style, and the ones imposed by xkcd:

with plt.xkcd():
    plt.style.use(["dark_background"])
    # This figure will be in XKCD-style
    make_figure("xkcd_black")
    # ...
plt.savefig("xkcd_fig.png")

xkcd_fig

Additional information

Based on this issue on stackoverflow.

Operating system

Ubuntu 22.04

Matplotlib Version

3.8.2

Matplotlib Backend

QTAgg

Python version

3.10.12

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions