简体   繁体   English

在 Python/Matplotlib 中重置绘图的默认字体/颜色

[英]Resetting default fonts/colours for plots in Python/Matplotlib

First time here and newbie so please bear with me.第一次来这里是新手,所以请多多包涵。

I'm following along a data viz tutorial with matplotlib.我正在使用 matplotlib 进行数据可视化教程。 I'm getting the same results (go me) but the font used (in the plot legend in particular) and the default co lours are different and no parameter has been set to change them.我得到了相同的结果(去我吧),但使用的字体(特别是在情节图例中)和默认颜色不同,并且没有设置参数来更改它们。 And of course the tutorial's stuff looks much nicer.当然,教程的内容看起来更好。 I don't know why this happens but I'm thinking that maybe I did change some of the stuff in unrelated notebooks and those choices stuck.我不知道为什么会发生这种情况,但我在想,也许我确实更改了无关笔记本中的一些内容,但这些选择被卡住了。 If so, how do I "reset" please?如果是这样,我该如何“重置”?

I am not allowed to attach screenshots yet.我还不能附上截图。 Basically, my plot lines are light blue and orange while his are the traditional discrete dark blue and green.基本上,我的情节线是浅蓝色和橙色,而他的则是传统的离散深蓝色和绿色。 My legend font looks like bad excel while his looks like LateX.我的图例字体看起来很糟糕,而他的字体看起来像 LateX。

Thank you for your suggestions.谢谢你的建议。

There's a number of ways you can do this and they vary depending on the specific use case but the one I use most often is有多种方法可以做到这一点,它们因特定用例而异,但我最常使用的方法是

from matplotlib import rcParams, rcParamsDefault
rcParams.update(rcParamsDefault)

Although it is likely the case that the tutorial is using a style other than the default so I wouldn't necessarily expect this to make your output look exactly like the tutorial.尽管本教程很可能使用了默认样式以外的样式,因此我不一定希望这会使您的输出看起来与本教程完全一样。 When using LaTex with matplotlib I use将 LaTex 与 matplotlib 一起使用时,我使用

pgf_with_latex = {
    "text.usetex": True,
    "pgf.rcfonts": False,
    "pgf.preamble": [
        r'\usepackage{color}'
    ]
}

rcParams.update(pgf_with_latex)

You can find more information here regarding customization and here regarding using LaTex with matplotlib.你可以找到更多的信息在这里关于定制,这里使用乳胶matplotlib有关。

To find all the built in matplotlib themes, use要查找所有内置的 matplotlib 主题,请使用

plt.style.available

I think the normal one is 'default'.我认为正常的是“默认”。 So add this before you make your plots:所以在你制作你的情节之前添加这个:

plt.style.use(['default'])

But you can make your plots look even better than the tutorial by using some of the other ones.但是您可以通过使用其他一些图使您的图看起来比教程更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM