如何在单个图中为不同的图获得不同颜色的线条
我正在使用 matplotlib 来创建绘图。我必须用不同的颜色来标识每个图,这些颜色应该由 Python 自动生成。
您能给我一种在同一图中为不同的图添加不同颜色的方法吗?
I am using matplotlib
to create the plots. I have to identify each plot with a different color which should be automatically generated by Python.
Can you please give me a method to put different colors for different plots in the same figure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Matplotlib 默认执行此操作。
例如:
而且,正如您可能已经知道的,您可以轻松添加图例:
如果您想控制循环的颜色:
如果您不熟悉 matplotlib,本教程是一个很好的起点。
编辑:
首先,如果您想在一个图上绘制很多(> 5)个东西,可以:
否则,你将会得到一个非常混乱的情节!善待那些将要阅读你正在做的事情的人,不要试图将 15 种不同的东西塞到一个人物身上!
除此之外,许多人都存在不同程度的色盲,并且对于更多的人来说,区分许多细微不同的颜色是困难的,比你想象的要困难。
话虽如此,如果您确实想在一个轴上放置 20 条线,并具有 20 种相对不同的颜色,这里有一种方法:
Matplotlib does this by default.
E.g.:
And, as you may already know, you can easily add a legend:
If you want to control the colors that will be cycled through:
If you're unfamiliar with matplotlib, the tutorial is a good place to start.
Edit:
First off, if you have a lot (>5) of things you want to plot on one figure, either:
Otherwise, you're going to wind up with a very messy plot! Be nice to who ever is going to read whatever you're doing and don't try to cram 15 different things onto one figure!!
Beyond that, many people are colorblind to varying degrees, and distinguishing between numerous subtly different colors is difficult for more people than you may realize.
That having been said, if you really want to put 20 lines on one axis with 20 relatively distinct colors, here's one way to do it:
稍后设置它们
如果您不知道要绘制的图的数量,则可以在绘制它们后更改颜色,使用
.lines
直接从图中检索数字,我使用这个解决方案:一些随机数据
您需要的代码段:
结果如下:
Setting them later
If you don't know the number of the plots you are going to plot you can change the colours once you have plotted them retrieving the number directly from the plot using
.lines
, I use this solution:Some random data
The piece of code that you need:
The result is the following:
TL;DR不,它不能自动完成。是的,这是可能的。
OP 写道
但是... Matplotlib 自动为每条不同的曲线生成不同的颜色
那么为什么要提出 OP 请求呢?如果我们继续阅读,我们有
这是有道理的,因为每个图(Matplotlib 中的每个轴)都有自己的 color_cycle(或者更确切地说,在 2018 年,它的 prop_cycle)并且每个图(
轴
)以相同的顺序重复使用相同的颜色。如果这是原始问题的含义,一种可能性是为每个图明确命名不同的颜色。
如果绘图(经常发生)是在循环中生成的,我们必须有一个额外的循环变量来覆盖 Matplotlib 自动选择的颜色。
另一种可能性是实例化一个 Cycler 对象
请注意,
type(my_cycler)
是cycler.Cycler
但 <代码>类型(actual_cycler)是<代码>itertools.cycle。TL;DR No, it can't be done automatically. Yes, it is possible.
The OP wrote
But... Matplotlib automatically generates different colors for each different curve
So why the OP request? If we continue to read, we have
and it make sense, because each plot (each
axes
in Matplotlib's parlance) has its owncolor_cycle
(or rather, in 2018, itsprop_cycle
) and each plot (axes
) reuses the same colors in the same order.If this is the meaning of the original question, one possibility is to explicitly name a different color for each plot.
If the plots (as it often happens) are generated in a loop we must have an additional loop variable to override the color automatically chosen by Matplotlib.
Another possibility is to instantiate a cycler object
Note that
type(my_cycler)
iscycler.Cycler
buttype(actual_cycler)
isitertools.cycle
.我想对上一篇文章中给出的最后一个循环答案进行一个小小的改进(该文章是正确的,仍然应该被接受)。标记最后一个示例时所做的隐含假设是
plt.label(LIST)
将标签编号 X 放入LIST
中,其中的行对应于第 X 次plot< /code> 被调用。我以前也遇到过这种方法的问题。根据 matplotlibs 文档构建图例并自定义其标签的推荐方法( http://matplotlib.org/users/legend_guide.html# adjustment-the-order-of-legend-item) 是为了有一种温暖的感觉,标签与您认为的确切情节一致做:
**: Matplotlib 图例不工作
I would like to offer a minor improvement on the last loop answer given in the previous post (that post is correct and should still be accepted). The implicit assumption made when labeling the last example is that
plt.label(LIST)
puts label number X inLIST
with the line corresponding to the Xth timeplot
was called. I have run into problems with this approach before. The recommended way to build legends and customize their labels per matplotlibs documentation ( http://matplotlib.org/users/legend_guide.html#adjusting-the-order-of-legend-item) is to have a warm feeling that the labels go along with the exact plots you think they do:**: Matplotlib Legends not working
Matplot 用不同的颜色为您的绘图着色,但如果您想添加特定的颜色
Matplot colors your plot with different colors , but incase you wanna put specific colors
上面的代码可能会帮助您以随机方式添加不同颜色的 3D 线条。您的彩色线条也可以通过 label="... " 参数中提到的图例的帮助来引用。
The above code might help you to add 3D lines with different colours in a randomized fashion. Your colored lines can also be referenced with a help of a legend as mentioned in the label="... " parameter.
老实说,我最喜欢的方法非常简单:现在这不适用于任意大量的绘图,但它最多可以处理 1163 个绘图。这是通过使用所有 matplotlib 的命名颜色的映射,然后选择它们随机的。
Honestly, my favourite way to do this is pretty simple: Now this won't work for an arbitrarily large number of plots, but it will do you up to 1163. This is by using the map of all matplotlib's named colours and then selecting them at random.