Python 中的 ggplot 样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22543208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:07:58  来源:igfitidea点击:

ggplot styles in Python

pythonmatplotlibpandasipython

提问by Josh

When I look at the plotting style in the Pandas documentation, the plots look different from the default one. It seems to mimic the ggplot "look and feel".

当我查看Pandas 文档中的绘图样式时,这些绘图看起来与默认绘图不同。它似乎模仿了 ggplot 的“外观和感觉”。

Same thing with the seaborn's package.

seaborn 的包裹相同

How can I load that style? (even if I am not using a notebook?)

我怎样才能加载那种风格?(即使我没有使用笔记本?)

采纳答案by joris

Update:If you have matplotlib >= 1.4, there is a new stylemodule which has a ggplotstyle by default. To activate this, use:

更新:如果您有 matplotlib >= 1.4,则style有一个ggplot默认具有样式的新模块。要激活它,请使用:

from matplotlib import pyplot as plt
plt.style.use('ggplot')

To see all the available styles, you can check plt.style.available.

要查看所有可用的样式,您可以查看plt.style.available



Similarly, for seaborn styling you can do:

同样,对于 seaborn 样式,您可以执行以下操作:

plt.style.use('seaborn-white')

or, you can use seaborn's own machinery to set up the styling:

或者,您可以使用seaborn自己的机器来设置样式:

import seaborn as sns
sns.set()

The set()function has more options to select a specific style (see docs). Note that seabornpreviously did the above automatically on import, but with the latest versions (>= 0.8) this is no longer the case.

set()函数有更多选项来选择特定样式(请参阅docs)。请注意,seaborn以前在导入时会自动执行上述操作,但在最新版本 (>= 0.8) 中,情况不再如此。



If you actually want a ggplot-like syntax in Python as well (and not only the styling), take a look at the plotninepackage, which is a grammar of graphics implementation in Python with a syntax very similar to R's ggplot2.

如果你真的想在 Python 中使用类似 ggplot 的语法(而不仅仅是样式),请查看plotnine包,它是 Python 中图形实现的语法,其语法与 R 的 ggplot2 非常相似。



Note: the old answer mentioned to do pd.options.display.mpl_style = 'default' . This was however deprecated in pandas in favor of matplotlib's styling using plt.style(..), and in the meantime this functionality is even removed from pandas.

注意:旧答案提到做pd.options.display.mpl_style = 'default' 。然而,这在 Pandas 中已被弃用,以支持使用 matplotlib 的样式plt.style(..),同时此功能甚至从 Pandas 中删除。

回答by Jan Katins

For the themes in python-ggplot, you can use them with other plots:

对于 python-ggplot 中的主题,您可以将它们与其他绘图一起使用:

from ggplot import theme_gray
theme = theme_gray()
with mpl.rc_context():
    mpl.rcParams.update(theme.get_rcParams())

    # plotting commands here

    for ax in plt.gcf().axes:
        theme.post_plot_callback(ax)

回答by AllanLRH

While I think that joris answeris a better solution since you're using Pandas, it should be mentioned that Matplotlib can be set to mimic ggplot by issuing the command matplotlib.style.use('ggplot').

虽然我认为joris 的答案是一个更好的解决方案,因为您使用的是 Pandas,但应该提到的是,可以通过发出命令将 Matplotlib 设置为模仿 ggplot matplotlib.style.use('ggplot')

See examples in the Matplotlib gallery.

请参阅Matplotlib 库中的示例

回答by Bodhi94

If you need to see available styles :

如果您需要查看可用样式:

import matplotlib.pyplot as plt

print(plt.style.available)

This will print available styles.

这将打印可用的样式。

And use this link to select the style you prefer

并使用此链接选择您喜欢的风格

https://tonysyu.github.io/raw_content/matplotlib-style-gallery/gallery.html

https://tonysyu.github.io/raw_content/matplotlib-style-gallery/gallery.html

回答by groceryheist

Jan Katins's answer is good, but the python-ggplot project seems to have become inactive. The plotnine project is more developed and supports an analogous, but superficially different, solution:

Jan Katins 的回答很好,但 python-ggplot 项目似乎已变得不活跃。plotnine 项目更加发达,支持类似但表面不同的解决方案:

from plotnine import theme_bw
import matplotlib as mpl
theme = theme_bw()

with mpl.rc_context():
    mpl.rcParams.update(theme.rcParams)