【问题标题】:Prettier default plot colors in matplotlibmatplotlib 中更漂亮的默认绘图颜色
【发布时间】:2013-03-26 17:30:46
【问题描述】:

matplotlib 中使用的默认颜色(此处的示例:http://matplotlib.org/examples/pylab_examples/pie_demo.html)有点普通和丑陋。我还注意到,如果您在一个图中绘制超过 5-6 个不同的系列,matplotlib 会开始重复颜色。

我已经看到了一些来自其他可视化包(默认为其他语言)的华丽图表,它们可以有 5-6 个不同的系列,仅由一种颜色和不同的阴影覆盖。有没有人可以在 matplotlib 中使用好的颜色集?以及让matplotlib默认使用它的方法?

【问题讨论】:

  • .. 该图像中的颜色对我来说看起来不错。您可以查看 this question 了解如何更改默认颜色循环,但我不确定“是否有人有好的颜色设置”是我们可以回答的问题。
  • 网络上有 1000 个网站可以帮助您选择好的配色方案,但它们不会专门针对图表。

标签: python matplotlib


【解决方案1】:

您可以使用Matplotlib's style sheets。它是从mpltools library 移植过来的,style 模块可以重新定义 matplotlib rc 参数。

例如,请参阅ggplot styleMatplotlib's manual 的使用。

【讨论】:

【解决方案2】:

这个问题是在 2 年前提出的,而今天为你的情节获得更好的风格要容易得多。您甚至不需要外部软件包。正如@asmaier 在他的评论中提到的,mpltools.style 功能已集成到 Matplotlib 1.4 中,因此您可以使用以下方式切换样式:

plt.style.use(style_name)

例如:

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('ggplot')

num_lines = 6

ax = plt.subplot(111)

for i in range(num_lines):
    x = np.linspace(0,20,200)
    ax.plot(x,np.sin(x)+i)

plt.show()

您可以列出所有可用的样式:

print plt.style.available

在 Matplotlib 1.5 中添加了几个新样式,包括来自 Seaborn 项目的许多样式:

plt.style.use('seaborn-dark-palette')

【讨论】:

  • 样式的演示在matplotlib.org/gallery/style_sheets/style_sheets_reference.html,如果您尝试一些,您需要plt.style.use('default') 在应用新样式之前重置样式,因为样式是可组合的.
  • 我得到了:ax.set_color_cycle(sns.color_palette("coolwarm_r",num_lines)) AttributeError: 'AxesSubplot' 对象没有属性 'set_color_cycle'
  • set_color_cycle 现在已被弃用。您应该改用:ax.set_prop_cycle('color', sns.color_palette("coolwarm_r", num_lines))
【解决方案3】:

看看prettyplotlib 一个库——最近朋友们向我指出——它修改了matplotlib 以更好地与Edward Tufte 的思想保持一致,以及Cynthia Brewer 的一些非常仔细研究的工作颜色感知。

【讨论】:

  • 请注意,根据github页面,prettyplotlib不再维护,维护者建议使用seaborn(seaborn.pydata.org/index.html)包,该包也支持prettyplotlib配色方案。
【解决方案4】:

Seaborn 包(基于 Matplotlib)具有很好的默认图形样式,我发现这是创建吸引人的颜色循环的好方法。

他们在这里对调色板进行了很好的讨论: https://stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html

以下代码演示了如何为简单的线图自动选择新的 color_cycle:

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns   

num_lines = 6

ax = plt.subplot(111)
ax.set_color_cycle(sns.color_palette("coolwarm_r",num_lines))

for i in range(num_lines):
    x = np.linspace(0,20,200)
    ax.plot(x,np.sin(x)+i)

plt.show()

如果您只想更改线条颜色而不使用其他 seaborn 预设,例如灰色背景,只需 import seaborn with

import seaborn.apionly as sns

【讨论】:

    【解决方案5】:

    您可以设置.matplotlibrc 文件。一个评论非常多的例子是here。在我看来,您要更改的选项是axes.color_cycle。我对如何制作更漂亮的界面没有任何建议——这有点太主观了 Stack Overflow ;-)(我对默认设置很满意)

    【讨论】:

      【解决方案6】:

      您可以使用 matplotlib 的colormap 功能。

      this 问题就是一个很好的例子。您可以显示您的颜色图选项using this script

      【讨论】:

        【解决方案7】:

        如需更多可与matplotlib 一起使用的颜色,请查看palettable,其中包含精彩的Wes Anderson 调色板。

        $ pip install palettable
        $ python
        >>> from palettable.colorbrewer.qualitative import Dark2_7
        

        【讨论】:

          【解决方案8】:

          我的作品使用ggplot风格,您可以在此链接中查看其他风格列表matplotlib.org

          用法:

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

          【讨论】:

            【解决方案9】:

            前段时间,我在matplotx 中创建了 dufte,这是 matplotlib 的简约风格。安装

            pip install matplotx
            

            并与

            一起使用
            plt.style.use(matplotx.styles.dufte)
            

            【讨论】:

              猜你喜欢
              • 2014-10-04
              • 2019-03-29
              • 2020-01-15
              • 2018-03-27
              • 2020-04-08
              • 1970-01-01
              • 1970-01-01
              • 2015-10-02
              • 2018-08-20
              相关资源
              最近更新 更多