简体   繁体   English

在 Python 中绘制 colors 的字典

[英]Plotting dictionary of colors in Python

I'm trying to display a dictionary of custom colors in Python using the example in the matplotlib docs我正在尝试使用matplotlib 文档中的示例在 Python 中显示自定义 colors 的字典

import numpy as np
import matplotlib.pyplot as plt

th = np.linspace(0, 2*np.pi, 512)

fig, ax1 = plt.subplots(1, 1, figsize=(6, 3))


def color_demo(ax, colors, title):
    ax.set_title(title)
    for j, c in enumerate(colors):
        v_offset = -(j / len(colors))
        ax.plot(th, .1*np.sin(th) + v_offset, color=c)
        ax.annotate("'C{}'".format(j), (0, v_offset),
                    xytext=(-1.5, 0),
                    ha='right',
                    va='center',
                    color=c,
                    textcoords='offset points',
                    family='monospace')

        ax.annotate("{!r}".format(c), (2*np.pi, v_offset),
                    xytext=(1.5, 0),
                    ha='left',
                    va='center',
                    color=c,
                    textcoords='offset points',
                    family='monospace')
    ax.axis('off')

mycolors = {
    'br': ((84/255), (0/255), (0/255)),
    'bl': ((15/255), (123/255), (175/255)),
    'yl': ((255/255), (186/255), (45/255)),
    'rd': ((237/255), (23/255), (69/255)),
    'gy': ((210/255), (210/255), (210/255))
    }

color_demo(ax1, mycolors, 'mycolors')

fig.subplots_adjust(**{'bottom': 0.0, 'left': 0.059,
                       'right': 0.869, 'top': 0.895})

But I get the error ValueError: 'br1' is not a valid value for color .但我收到错误ValueError: 'br1' is not a valid value for color

Here's an experiment:这是一个实验:

>>> mycolors = {
...     'br': ((84/255.0), (0/255.0), (0/255.0)),
...     'bl': ((15/255.0), (123/255.0), (175/255.0)),
...     'yl': ((255/255.0), (186/255.0), (45/255.0)),
...     'rd': ((237/255.0), (23/255.0), (69/255.0)),
...     'gy': ((210/255.0), (210/255.0), (210/255.0))
...     }
>>>
>>> for i, c in enumerate(mycolors):
...  print(i, c)
... 
0 br
1 bl
2 yl
3 rd
4 gy
>>> 

Is that what you expected this code to do?这是你期望这段代码做的吗? Iterating over a dictionary like this actually iterates over its keys .像这样遍历字典实际上是遍历它的

The error I got while running your code says:我在运行您的代码时遇到的错误说:

ValueError: 'br' is not a valid value for color

It tried to use the first key ( 'br' ) as a color, which Matplotlib tried to interpret as the name of the color, but it turned out that "'br' is not a valid value for color".它试图使用第一个键( 'br' )作为颜色,Matplotlib 试图将其解释为颜色的名称,但结果证明“'br' 不是颜色的有效值”。

You should iterate over the values of the dictionary:您应该遍历字典的

for j, c in enumerate(colors.values()):
    ...

Resulting plot:结果 plot:

阴谋

br , bl , etc... are not colors by default. br , bl等...默认不是 colors 。 I think what you want is:我想你想要的是:

def color_demo(ax, colors, title):
    ax.set_title(title)
    for j, c in enumerate(colors):
        v_offset = -(j / len(colors))
        ax.plot(th, .1*np.sin(th) + v_offset, color=colors[c])
        ax.annotate("'C{}'".format(j), (0, v_offset),
                    xytext=(-1.5, 0),
                    ha='right',
                    va='center',
                    color=c,
                    textcoords='offset points',
                    family='monospace')

        ax.annotate("{!r}".format(c), (2*np.pi, v_offset),
                    xytext=(1.5, 0),
                    ha='left',
                    va='center',
                    color=colors[c],
                    textcoords='offset points',
                    family='monospace')
    ax.axis('off')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM