Zaus
Zaus

Reputation: 1351

matplotlib xkcd and black figure background

I am trying to make a plot using matplotlib's xkcd package while having a black background. However, xkcd seems to add a sort of white contour line around text and lines. On a white background you can't see this white frame, but on a black background it is really annoying.

Does anyone have an idea how to fix this? Maybe an option how to change the white contour lines to have the background color instead?

minimal example:

import numpy as np
import matplotlib.pyplot as plt

plt.style.use(['dark_background'])
plt.xkcd()
plt.rcParams['figure.facecolor'] = 'black'

x = np.linspace(-5, 5)

plt.figure()
plt.plot(x, x**2)
plt.text(0, 10, "Text", color='r', fontsize=20)
plt.xlabel("$x$"   )
plt.ylabel("$f(x)$")
plt.show();

minimal example of xkcd with black background:

Edit: I am using Python 3.5.4 in my jupyter notebook with packages:

Note, when running the above code as a script I somehow don't get a xkcd plot at all. Only from jupyter notebook or command line I get the described behaviour.

Upvotes: 8

Views: 944

Answers (1)

StubBurn
StubBurn

Reputation: 126

After you give the plt.xkcd() command, try executing

from matplotlib import patheffects
plt.rcParams['path.effects'] = [patheffects.withStroke(linewidth=0)]

This solved the problem for me!

Edit: I ran the code you gave with the suggested changes.

Input:

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

plt.style.use(['dark_background'])
plt.xkcd()
plt.rcParams['path.effects'] = [patheffects.withStroke(linewidth=0)]

plt.rcParams['figure.facecolor'] = 'black'

x = np.linspace(-5, 5)

plt.figure()
plt.plot(x, x**2)
plt.text(0, 10, "Text", color='r', fontsize=20)
plt.xlabel("$x$"   )
plt.ylabel("$f(x)$")
plt.show();

Output

You might want to change your axis colors to white, though, if you want the background to be black

Upvotes: 7

Related Questions