为什么matplotlib用这么多颜色为我的地块着色? [英] Why is matplotlib coloring my plot with so many colors?

查看:33
本文介绍了为什么matplotlib用这么多颜色为我的地块着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制多个对象,它们都由多条直线组成.为此,我使用了一个颜色列表,并为每个对象分配了一个颜色.所需的结果如下所示:
我使用命令 plt.plot 执行此操作,提供起点和终点以及颜色.

在试图得到这个结果时,我犯了一个错误,而不是像 'pink' 这样的名字,我提供了像 (0, 0.75, 0) 这样的值.
结果是:

为什么 matplotlib 会这样?我想了解为什么每个段都有自己的颜色,似乎是随机选择的,而我总是提供相同的错误输入.

当未传递正确的输入值时,我无法找到有关行为的文档.如果有文档,那么如果matplotlib每次实际上只是随机选择,则相关部分的引用将已经构成答案.

当我传递 False 而不是颜色时会发生同样的行为.

<小时>

MCVE :

 将matplotlib.pyplot导入为plt#黑色线plt.plot([-1,-1], [-1,1], 'k')# 线条颜色不好plt.plot([0,0],[-1,1],False)# 线条颜色相同plt.plot([1,1],[-1,1],False)#其他颜色不好的行plt.plot([2,2],[-1,1], (0,0.75,0))# 以相同的其他坏颜色行plt.plot([3,3],[-1,1], (0,0.75,0))plt.show()

<小时>

注意:
我不是在寻找如何通过传递恒定颜色来获得我想要的结果.我做到了.但是在去那里的路上,我遇到了这种行为,我想了解一下.
根据评论,pyplot 中似乎有一些已记录且众所周知的功能,它们仅使用颜色循环,并在未提供有效颜色时采用其中的下一种颜色.不过,我无法找到相关文档,所以我想要一些文档引用,或者这是未记录行为的答案,或者类似的内容.

还值得指出的是,当我使用无效的颜色参数时,图像中间的某些东西也会被绘制,但在使用正确的值时则不会.为什么?

解决方案

3.

与上述相同.唯一的不同是,您可以根据使用的颜色循环仪获得不同颜色的线条(请参见此答案的下面).

4.

  plt.plot([2,2],[-1,1],(0,0.75,0))plt.plot(x,y,y2)

同样,第三个参数不是有效的格式字符串.它再次被解释为另一行的 y 值.这次,与2.和3.相反, y2 包含多个坐标.因此,沿着点 (0,0), (1,0.75), (2,0) 绘制一条线.

<小时>

除第一种情况外,均未指定颜色.在这种情况下,根据文档

<块引用>

默认情况下,为每行分配一个由样式周期"指定的不同样式.fmt 和 line 属性参数仅在您希望显式偏离这些默认值时才需要.另外,您也可以使用'axes.prop_cycle'rcParam更改样式周期.

由于默认属性循环器仅包含颜色,因此您将在各处获得相同的线型,但颜色不同.

您可以打印默认的循环仪

print(plt.rcParams["axes.prop_cycle"])# cycler('color', ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b',# '#​​e377c2', '#7f7f7f', '#bcbd22', '#17becf'])

或者你可以设置它

  plt.rcParams ["axes.prop_cycle"] = plt.cycler("color",["red",#001eff",(0,0.75,0)])

matplotlib页面上提供了一个示例:使用循环仪的样式,以及<一个href ="https://matplotlib.org/gallery/color/color_cycle_default.html" rel ="nofollow noreferrer">默认属性周期中的颜色

cycler 也有自己的文档页面, https://matplotlib.org/cycler/,因为它可能同样用于matplotlib之外的其他事物.

回到问题的开头;为了使所有线条都具有相同的颜色,可以将颜色循环仪设置为单一颜色

plt.rcParams["axes.prop_cycle"] = plt.cycler("color", ["k"])

在这种情况下,轴上的所有线都将显示为黑色.

I am plotting multiple objects, both consisting of multiple straight lines. To do so, I use a list of colors and assign one to each object. The desired result looks like this:
I am doing this with the command plt.plot, providing a start and end point, and a color.

While trying to get to this result, I made a mistake, and instead of names like 'pink', I provided values like (0, 0.75, 0).
Which resulted in this:

Why does matplotlib behave this way? I would like to understand how it comes that each segment has its own color, seemingly chosen at random, when I have always provided the same incorrect input.

I was unable to find documentation of the behaviour when no correct input value is passed. If there is documentation, a quote of the relevant part would already constitute an answer if matplotlib actually just chooses at random every time.

The same behaviour happens when I pass False instead of a color.


MCVE:

import matplotlib.pyplot as plt

# Line in black
plt.plot([-1,-1], [-1,1], 'k')
# Line in bad color
plt.plot([0,0],[-1,1], False)
# Line in same bad color
plt.plot([1,1],[-1,1], False)
# Line in other bad color
plt.plot([2,2],[-1,1], (0,0.75,0))
# Line in same other bad color
plt.plot([3,3],[-1,1], (0,0.75,0))
plt.show()


Note:
I am not looking for how to get my desired result by passing a constant color. I managed that. But on the way there, I encountered this behaviour, which I'd like to understand.
Based on the comments, it seems likely that there is some documented and well-known functionality in pyplot that simply uses a color cycle and takes the next color in it whenever no valid color is provided. I am not able to find documentation on that though, so I would like either some documentation quote, or the answer that this is undocumented behaviour, or something along those lines.

It is also worth pointing out that something in the middle of the image was also drawn when I was using invalid color parameters, but not when using correct values. Why?

解决方案

The call signature of plot is completely crazy. This is because it has grown over time and is one of the most used functions, hence it's always kept backward compatible.

The documentation states the following ways to call this function.

plot([x], y, [fmt], data=None, **kwargs)
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

Arguments in square brackets are optional and the ellipses denote an arbitrary number of further arguments to be passed.

Going through the cases from above, and renaming the respective arguments:

1.

plt.plot([-1,-1], [-1,1], 'k')
plt.plot(   x   ,    y  , fmt)

As expected 'k' is the format to be used.

2.

plt.plot([0,0],[-1,1], False)
plt.plot(  x  ,   y  ,   y2 )

Here, False is no valid format string. Hence it is interpreted as data for a new line. Since there is no other following argument that can be interpreted, it cannot be x2, hence it must be y2. False is equivalent to 0. So you would get a single point at (0,0), which you don't see in the plot, because there is no marker specified. If you were to use

plt.plot([0,0],[-1,1], False, "o")

instead, you'd see the single point in orange.

3.

Same as above. The only difference is that you get the line in a different color according to the color cycler in use (see below in this answer).

4.

plt.plot([2,2],[-1,1], (0,0.75,0))
plt.plot(  x  ,   y  ,     y2    )

Again, the third argument is no valid format string. It is again interpreted as the y values of a further line. This time, in contrast to 2. and 3., y2 contains several coordinates. Therefore a line along the points (0,0), (1,0.75), (2,0) is drawn.


In all but the first case, no color is specified. In that case according to the docs

By default, each line is assigned a different style specified by a 'style cycle'. The fmt and line property parameters are only necessary if you want explicit deviations from these defaults. Alternatively, you can also change the style cycle using the 'axes.prop_cycle' rcParam.

Since the default property cycler only contains color, you will hence get the same linestyle everywhere, but in a different color.

You may print the default cycler

print(plt.rcParams["axes.prop_cycle"])

# cycler('color', ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b',
#                   '#e377c2', '#7f7f7f', '#bcbd22', '#17becf'])

Or you may set it

plt.rcParams["axes.prop_cycle"] = plt.cycler("color", ["red", "#001eff", (0, 0.75, 0)])

An example is available on the matplotlib page: Styling with cycler and also Colors in the default property cycle

cycler also has its own documentation page, https://matplotlib.org/cycler/ as it may equally be used for other things outside of matplotlib.

Coming back to the start of the question; in order to get all lines in the same color, it is possible to set the color cycler to a single color

plt.rcParams["axes.prop_cycle"] = plt.cycler("color", ["k"])

In this case, all lines in the axes will appear black.

这篇关于为什么matplotlib用这么多颜色为我的地块着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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