gpt4 book ai didi

python - matplotlib xkcd和黑图背景

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:07 26 4
gpt4 key购买 nike

我正在尝试使用 matplotlibxkcd 包绘制黑色背景图。然而,xkcd 似乎在文本和线条周围添加了一种白色轮廓线。在白色背景上你看不到这个白框,但在黑色背景上它真的很烦人。

有没有人知道如何解决这个问题?也许可以选择如何将白色轮廓线更改为具有背景颜色?

最小的例子:

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 :

编辑:我在我的 jupyter notebook 中使用 Python 3.5.4 包:

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

请注意,当将上述代码作为脚本运行时,我以某种方式根本没有得到 xkcd 图。只有从 jupyter notebook 或命令行我才能得到描述的行为。

最佳答案

在你给出plt.xkcd()命令后,尝试执行

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

这解决了我的问题!

编辑:我根据建议的更改运行了您提供的代码。

输入:

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();

输出(抱歉,我必须链接到输出图像,因为 Stack 上的新手不允许在他们的答案中嵌入图像):

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

关于python - matplotlib xkcd和黑图背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50845646/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com