Python matplotlib 中更漂亮的默认绘图颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15814635/
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
Prettier default plot colors in matplotlib
提问by Randy Olson
The default colors used in matplotlib (example here: http://matplotlib.org/examples/pylab_examples/pie_demo.html) are kind of plain and ugly. I've also noticed that if you plot more than 5-6 different series in a single plot, matplotlib starts repeating colors.
matplotlib 中使用的默认颜色(例如:http: //matplotlib.org/examples/pylab_examples/pie_demo.html)有点简单和丑陋。我还注意到,如果在一个图中绘制超过 5-6 个不同的系列,matplotlib 会开始重复颜色。
I've seen some gorgeous graphs coming out of other visualization packages (in other languages, by default) that can have 5-6 different series covered by just one color in different shades. Does anyone have a good color set to use in matplotlib? And a way to make matplotlib use it by default?
我见过一些来自其他可视化包(默认情况下使用其他语言)的精美图表,它们可以有 5-6 个不同的系列,仅用一种颜色不同的色调覆盖。有没有人在 matplotlib 中设置好的颜色?以及让 matplotlib 默认使用它的方法?
采纳答案by gcalmettes
You can use Matplotlib's style sheets. It has been ported from the mpltools librarywhich has a stylemodule that redefine matplotlib rc parameters.
您可以使用Matplotlib 的样式表。它是从mpltools 库移植过来的,它有一个重新定义 matplotlib rc 参数的样式模块。
As an example, see the use of the ggplot styleand Matplotlib's manual.
例如,请参阅ggplot 样式的使用和Matplotlib 的手册。


回答by mgilson
You can setup a .matplotlibrcfile. A really heavily commented example is here. It looks to me like the option you want to change is axes.color_cycle. I don't have any advice on what to make it for a prettier interface -- That's a little too subjective Stack Overflow ;-) (and I'm happy with the defaults)
你可以设置一个.matplotlibrc文件。一个评论量很大的例子在这里。在我看来,您想要更改的选项是axes.color_cycle. 我对如何制作更漂亮的界面没有任何建议——这有点过于主观 Stack Overflow ;-)(我对默认值很满意)
回答by StuGrey
回答by Marcus P S
Have a look at prettyplotliba library — just pointed out to me recently by friends — that modifies matplotlib to be better aligned with the ideas of Edward Tufte, as well as some very carefully studied work by Cynthia Breweron color perception.
看看Prettyplotlib一个库——最近刚由朋友向我指出——它修改了 matplotlib 以更好地与Edward Tufte的想法保持一致,以及Cynthia Brewer对颜色感知的一些非常仔细研究的工作。
回答by DanHickstein
The Seabornpackage (based on Matplotlib) has nice default graph styles, and I've found that it's a nice way to create an appealing color-cycle.
该Seaborn包(按Matplotlib)具有良好的默认的图形样式,以及我发现,这是一个很好的方法来创建一个有吸引力的颜色周期。
They have a nice discussion of the colorpalettes here: https://stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html
他们在这里对调色板进行了很好的讨论:https://stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html
The following code demonstrates how you can pick a new color_cycle automatically for a simple line plot:
以下代码演示了如何为简单的线图自动选择新的 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()
If you want to just change the linecolors and not use the other seaborn pre-sets such as the gray background, just import seaborn with
如果您只想更改线条颜色而不使用其他 seaborn 预设,例如灰色背景,只需导入 seaborn 即可
import seaborn.apionly as sns
回答by wombatonfire
The question was asked 2 years ago, and today it's much easier to get better style for your plot. You don't even need external packages for that. As @asmaier mentioned in his comment, mpltools.style functionality has been integrated into Matplotlib 1.4, so you can switch styles with:
这个问题是 2 年前提出的,而今天,为您的情节获得更好的风格要容易得多。为此,您甚至不需要外部软件包。正如@asmaier 在他的评论中提到的,mpltools.style 功能已集成到 Matplotlib 1.4 中,因此您可以通过以下方式切换样式:
plt.style.use(style_name)
For example:
例如:
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()
You can list all available styles with:
您可以列出所有可用的样式:
print plt.style.available
In Matplotlib 1.5 several new styles have been added, including many styles from the Seaborn project:
在 Matplotlib 1.5 中添加了几种新样式,包括来自 Seaborn 项目的许多样式:
plt.style.use('seaborn-dark-palette')
回答by christopherlovell
For a richer set of colours that can be used with matplotlib, check out palettable, featuring the wonderful Wes Andersonpalletes.
如需更丰富的可搭配颜色matplotlib,请查看可调色板,其中包含精美的Wes Anderson调色板。
$ pip install palettable
$ python
>>> from palettable.colorbrewer.qualitative import Dark2_7

