matplotlib xkcd and black figure background

2024/10/8 22:12:17

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 pltplt.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:

  • matplotlib==2.2.2, backend='TkAgg'
  • jupyter==1.0.0
  • notebook==5.5.0

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.

Answer

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 patheffectsplt.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 (Sorry, I have to link to the output image, because a newbie on Stack is not allowed to embed images in their answer):

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

https://en.xdnf.cn/q/70089.html

Related Q&A

Python: Whats the difference between set.difference and set.difference_update?

s.difference(t) returns a new set with no elements in t.s.difference_update(t) returns an updated set with no elements in t.Whats the difference between these two set methods? Because the difference_u…

python telebot got unexpected response

I have been using my Telegram bot for sending me different notifications from my desktop computer using pythons telebot library. Everything was working properly for quite a long time, but one day it st…

How to set correct value for Django ROOT_URLCONF setting in different branches

Ive put site directory created by django-admin startproject under version control (Mercurial). Lets say, the site is called frobnicator.Now I want to make some serious refactoring, so I clone the site …

How do I improve scrapys download speed?

Im using scrapy to download pages from many different domains in parallel. I have hundreds of thousands of pages to download, so performance is important.Unfortunately, as Ive profiled scrapys speed, …

Convert numpy, list or float to string in python

Im writing a python function to append data to text file, as shown in the following,The problem is the variable, var, could be a 1D numpy array, a 1D list, or just a float number, I know how to convert…

Shared XMPP connection between Celery workers

My web app needs to be able to send XMPP messages (Facebook Chat), and I thought Celery might be a good solution for this. A task would consist of querying the database and sending the XMPP message to …

List of installed fonts OS X / C

Im trying to programatically get a list of installed fonts in C or Python. I need to be able to do this on OS X, does anyone know how?

How to detect changed and new items in an RSS feed?

Using feedparser or some other Python library to download and parse RSS feeds; how can I reliably detect new items and modified items?So far I have seen new items in feeds with publication dates earli…

python SharedMemory persistence between processes

Is there any way to make SharedMemory object created in Python persist between processes? If the following code is invoked in interactive python session: >>> from multiprocessing import share…

What is the difference between syntax error and runtime error?

For example:def tofloat(i): return flt(i)def addnums(numlist):total = 0for i in numlist:total += tofloat(i)return totalnums = [1 ,2 ,3] addnums(nums)The flt is supposed to be float, but Im confused whe…