pandas 打印良好的 matplotlib 颜色选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13968520/
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
Color selection for matplotlib that prints well
提问by vkontori
I am using pandas and matplotlib to generate bar-graphs with lots of bars.
我正在使用 pandas 和 matplotlib 生成带有很多条形的条形图。
I know how to cycle through a list of selected colors (How to give a pandas/matplotlib bar graph custom colors). The question is what colors to select so that my graph prints nicely on a paper (it is for a research paper). What I am most interested in is sufficient contrast between the columns and a selection of colors that looks pleasant. I would like to have multiple colors instead of gray-scale or single-hue colorschemes.
我知道如何循环选择颜色列表(如何提供Pandas/matplotlib 条形图自定义颜色)。问题是选择什么颜色,以便我的图表可以很好地打印在纸上(用于研究论文)。我最感兴趣的是柱子之间的足够对比度和看起来令人愉快的颜色选择。我想要多种颜色,而不是灰度或单色调色方案。
Are there any predetermined schemes to select from that people use?
是否有任何预定的方案可供人们选择?
回答by endolith
So your requirements are "lots of colors" and "no two colors should map to the same grayscale value when printed", right? The second criteria should be met by any "sequential" colormaps (which increase or decrease monotically in luminance). I think out of all the choices in matplotlib, you are left with cubehelix(already mentioned), gnuplot, and gnuplot2:
所以你的要求是“很多颜色”和“打印时没有两种颜色应该映射到相同的灰度值”,对吗?任何“顺序”颜色图(亮度单调增加或减少)都应满足第二个标准。我认为在 matplotlib 中的所有选择中,您只剩下cubehelix(已经提到)gnuplot、 和gnuplot2:
The white line is the luminance of each color, so you can see that each color will map to a different grayscale value when printed. The black line is hue, showing they cycle through a variety of colors.
白线是每种颜色的亮度,因此您可以看到每种颜色在打印时都会映射到不同的灰度值。黑线是色调,显示它们在各种颜色中循环。
Note that cubehelix is actually a function (from matplotlib._cm import cubehelix), and you can adjust the parameters of the helix to produce more widely-varying colors, as shown here. In other words, cubehelix is not a colormap, it's a family of colormaps. Here are 2 variations:
请注意,cubehelix 实际上是一个函数 ( from matplotlib._cm import cubehelix),您可以调整螺旋的参数以产生更广泛变化的颜色,如下所示。换句话说,cubehelix 不是颜色图,而是颜色图系列。这里有 2 个变体:




For less wildly-varying colors (more pleasant for many things, but maybe not for your bar graphs), maybe try the ColorBrewer 3-color maps, YlOrRd, PuBuGn, YlGnBu:
对于变化不大的颜色(对许多事物来说更令人愉快,但对于您的条形图可能不是这样),也许可以尝试使用 ColorBrewer 3 色图、YlOrRd、PuBuGn、YlGnBu:
https://www.flickr.com/photos/omegatron/7298887952/
https://www.flickr.com/photos/omegatron/7298887952/
I wouldn't recommend using only this color to identify bar graphs, though. You should always use text labels as the primary identifier. Also note that some of these produce white bars that completely blend in with the background, since they are intended for heatmaps, not chart colors:
不过,我不建议只使用这种颜色来识别条形图。您应该始终使用文本标签作为主要标识符。另请注意,其中一些会产生与背景完全融合的白条,因为它们用于热图,而不是图表颜色:
from matplotlib import pyplot as plt
import pandas, numpy as np # I find np.random.randint to be better
# Make the data
x = [{i:np.random.randint(1,5)} for i in range(10)]
df = pandas.DataFrame(x)
# Make a list by cycling through the colors you care about
# to match the length of your data.
cmap = plt.get_cmap('cubehelix')
indices = np.linspace(0, cmap.N, len(x))
my_colors = [cmap(int(i)) for i in indices]
# Specify this list of colors as the `color` option to `plot`.
df.plot(kind='bar', stacked=True, color=my_colors)
And these are the new guys:
这些是新人:
回答by tacaswell
In 1.5 matplotlib will ship with 4 new rationally designed color maps:
在 1.5 matplotlib 中将提供 4 个新的合理设计的颜色图:
'viridis'(will be default color map in 2.0)'magma''plasma''inferno'.
'viridis'(将是 2.0 中的默认颜色图)'magma''plasma''inferno'.
The process of designing these color maps is presented in https://youtu.be/xAoljeRJ3lU.
https://youtu.be/xAoljeRJ3lU 中介绍了设计这些颜色图的过程。
The tool developed for this process can be installed by pip install viscm
可以通过以下方式安装为此过程开发的工具 pip install viscm
I would suggest the cubehelixcolor map. It is designedto have correct luminosity ordering in both color and gray-scale
我会建议cubehelix颜色图。它被设计为在颜色和灰度中具有正确的光度排序
回答by tiago
I am not aware of predetermined schemes. I usually use a few colours for publication plots. I mostly take two things into consideration when choosing colours:
我不知道预定的方案。我通常为出版图使用几种颜色。在选择颜色时,我主要考虑两件事:
- Colour-blindness: thispage on wikipedia has lots of good info about choosing colours that are distinguishable to most color-blind people. If you notice on the "tips for editors" section, once you take the guidelines into account there are only a few sets of colours available. (A good rule of thumb is to never mix red and green!) You can also use the linked colour-blind simulators to see if your plot would be well visible.
- Luminance: most of the journals in my field will publish in B&W by default. Even though most people read the papers online, I still like to make sure that the plots can be understood when printed in grayscale. So I take care to use colours that have different luminances. To test, a good way is to just desaturate the image produced, and you'll have a good idea of how it looks when printed in grayscale. In many cases (particularly line or scatter plots), I also use other things than colour to distinguish between sets (eg. line styles, different markers).
- 色盲:维基百科上的这个页面有很多关于选择大多数色盲人可以区分的颜色的好信息。如果您注意到“编辑提示”部分,一旦您考虑到这些指南,就只有几组颜色可用。(一个好的经验法则是永远不要混合红色和绿色!)您还可以使用链接的色盲模拟器来查看您的绘图是否清晰可见。
- 亮度:我所在领域的大多数期刊默认都会以黑白形式发表。尽管大多数人在线阅读论文,但我仍然希望确保以灰度打印时可以理解这些图。所以我小心使用具有不同亮度的颜色。要进行测试,一个好方法是对生成的图像进行去饱和处理,这样您就可以很好地了解以灰度打印时的外观。在许多情况下(特别是线图或散点图),我还使用颜色以外的其他东西来区分集合(例如线型、不同的标记)。
If no colours are specified in matplotlib plots, it has a default set of colours that it cycles through. This answerhas a good explanation on how to change that default set of colours. You can customise that to your preferred set of colours, so the plots would use them in turn.
如果在 matplotlib 图中没有指定颜色,则它有一组默认的颜色可以循环使用。这个答案很好地解释了如何更改默认颜色集。您可以将其自定义为您喜欢的一组颜色,以便绘图依次使用它们。

