matplotlib 中的命名颜色 [英] Named colors in matplotlib

查看:40
本文介绍了matplotlib 中的命名颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

matplotlib 中有哪些命名颜色可用于绘图?我可以在 matplotlib 文档中找到一个列表,声称这些是唯一的名称:

b:蓝色g:绿色r:红色c:青色m:洋红色y:黄色k:黑色w:白色

但是,我发现也可以使用这些颜色,至少在这种情况下:

scatter(X,Y, color='red')分散(X,Y,颜色='橙色​​')分散(X,Y,颜色='深绿色')

但这些不在上面的列表中.有谁知道可用的命名颜色的详尽列表?

解决方案

我经常忘记我想使用的颜色的名称,并不断回到这个问题 =)

以前的答案很好,但我发现从发布的图像中大致了解可用颜色有点困难.我更喜欢将颜色与相似的颜色分组,所以我稍微调整了

我真的没有从 matplotlib 示例中改变太多,但这里是完整的代码.

将 matplotlib.pyplot 导入为 plt从 matplotlib 导入颜色为 mcolors颜色 = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)# 按色调、饱和度、值和名称对颜色进行排序.by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name)名称,颜色在colors.items())sorted_names = [hsv 的名称,by_hsv 中的名称]n = len(sorted_names)ncols = 4nrows = n//ncols图, ax = plt.subplots(figsize=(12, 10))# 获取高度和宽度X, Y = fig.get_dpi() * fig.get_size_inches()h = Y/(nrows + 1)w = X/ncols对于我,枚举中的名称(sorted_names):行 = i % nrowscol = i//nrowsy = Y - (row * h) - hxi_line = w * (col + 0.05)xf_line = w * (col + 0.25)xi_text = w * (col + 0.3)ax.text(xi_text, y, name, fontsize=(h * 0.8),水平对齐='左',垂直对齐='中心')ax.hlines(y + h * 0.1, xi_line, xf_line,颜色=颜色[名称],线宽=(h * 0.8))ax.set_xlim(0, X)ax.set_ylim(0, Y)ax.set_axis_off()fig.subplots_adjust(left=0, right=1,顶部=1,底部=0,hspace=0, wspace=0)plt.show()

<小时>

其他命名颜色

于 2017 年 10 月 25 日更新.我将我以前的更新合并到此部分.

xkcd

如果您想在使用 matplotlib 绘图时使用其他命名颜色,您可以使用

表格

默认 Tableau 颜色可通过tab:"前缀在 matplotlib 中使用:

plt.plot([1,2], lw=4, c='tab:green')

有十种不同的颜色:

HTML

您还可以通过他们的 HTML 十六进制代码绘制颜色:

plt.plot([1,2], lw=4, c='#8f9805')

这更类似于指定和 RGB 元组而不是命名颜色(除了十六进制代码作为字符串传递的事实),并且我不会包括您可以选择的 1600 万种颜色的图像...

<小时>

有关详细信息,请参阅matplotlib 颜色文档和指定可用的源文件颜色,_color_data.py.

<小时>

What named colors are available in matplotlib for use in plots? I can find a list on the matplotlib documentation that claims that these are the only names:

b: blue
g: green
r: red
c: cyan
m: magenta
y: yellow
k: black
w: white

However, I've found that these colors can also be used, at least in this context:

scatter(X,Y, color='red')
scatter(X,Y, color='orange')
scatter(X,Y, color='darkgreen')

but these are not on the above list. Does anyone know an exhaustive list of the named colors that are available?

解决方案

I constantly forget the names of the colors I want to use and keep coming back to this question =)

The previous answers are great, but I find it a bit difficult to get an overview of the available colors from the posted image. I prefer the colors to be grouped with similar colors, so I slightly tweaked the matplotlib answer that was mentioned in a comment above to get a color list sorted in columns. The order is not identical to how I would sort by eye, but I think it gives a good overview.

I updated the image and code to reflect that 'rebeccapurple' has been added and the three sage colors have been moved under the 'xkcd:' prefix since I posted this answer originally.

I really didn't change much from the matplotlib example, but here is the code for completeness.

import matplotlib.pyplot as plt
from matplotlib import colors as mcolors


colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)

# Sort colors by hue, saturation, value and name.
by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name)
                for name, color in colors.items())
sorted_names = [name for hsv, name in by_hsv]

n = len(sorted_names)
ncols = 4
nrows = n // ncols

fig, ax = plt.subplots(figsize=(12, 10))

# Get height and width
X, Y = fig.get_dpi() * fig.get_size_inches()
h = Y / (nrows + 1)
w = X / ncols

for i, name in enumerate(sorted_names):
    row = i % nrows
    col = i // nrows
    y = Y - (row * h) - h

    xi_line = w * (col + 0.05)
    xf_line = w * (col + 0.25)
    xi_text = w * (col + 0.3)

    ax.text(xi_text, y, name, fontsize=(h * 0.8),
            horizontalalignment='left',
            verticalalignment='center')

    ax.hlines(y + h * 0.1, xi_line, xf_line,
              color=colors[name], linewidth=(h * 0.8))

ax.set_xlim(0, X)
ax.set_ylim(0, Y)
ax.set_axis_off()

fig.subplots_adjust(left=0, right=1,
                    top=1, bottom=0,
                    hspace=0, wspace=0)
plt.show()


Additional named colors

Updated 2017-10-25. I merged my previous updates into this section.

xkcd

If you would like to use additional named colors when plotting with matplotlib, you can use the xkcd crowdsourced color names, via the 'xkcd:' prefix:

plt.plot([1,2], lw=4, c='xkcd:baby poop green')

Now you have access to a plethora of named colors!

Tableau

The default Tableau colors are available in matplotlib via the 'tab:' prefix:

plt.plot([1,2], lw=4, c='tab:green')

There are ten distinct colors:

HTML

You can also plot colors by their HTML hex code:

plt.plot([1,2], lw=4, c='#8f9805')

This is more similar to specifying and RGB tuple rather than a named color (apart from the fact that the hex code is passed as a string), and I will not include an image of the 16 million colors you can choose from...


For more details, please refer to the matplotlib colors documentation and the source file specifying the available colors, _color_data.py.


这篇关于matplotlib 中的命名颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆