如何在 Python 的 plt 中获取最近绘制的线条的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36699155/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:14:07  来源:igfitidea点击:

How to get color of most recent plotted line in Python's plt

pythonmatplotlib

提问by denvar

I plot a line without specifying the color (think: plt.plot(x,y)). Say the color comes out blue.

我绘制了一条没有指定颜色的线(想想:plt.plot(x,y))。假设颜色是蓝色的。

Question:How do I obtain this color from the plt object so that I can put it into a variable?

问题:如何从 plt 对象中获取此颜色,以便将其放入变量中?

Seems like this is close (and potentially the solution):

似乎这很接近(并且可能是解决方案):

p = plt.plot(x,y)
color = p[0].get_color()

Updated question:I am not sure I understand the "0" index: Does p[0] always access the most recent plotted line?

更新的问题:我不确定我是否理解“0”索引:p[0] 是否总是访问最近绘制的线?

回答by Hun

In your example, p is a list of Line2D object. In that example you have only one line object, p[0]. The following is an example plotting three lines. As more line is added, it is appended to the p.So if you want the color of the last plot, it will be a[-1].get_color().

在您的示例中, p 是 Line2D 对象的列表。在该示例中,您只有一个线对象 p[0]。以下是绘制三条线的示例。随着更多行的添加,它被附加到 p.So 如果你想要最后一个图的颜色,它将是a[-1].get_color().

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(10)
y = np.arange(10)
p = plt.plot(x,y, x,y*2, x,y*3) # make three line plots
type(p) # list
type(p[0]) # <class 'matplotlib.lines.Line2D'>
p[0].get_color() # 'b'
p[1].get_color() # 'g'
p[2].get_color() # 'r'

line plot

线图

回答by Jim Hunziker

For regular plt.plot, doing item.get_color()on each element of the list it returns will get you the colors of each line.

对于常规plt.plotitem.get_color()对它返回的列表的每个元素执行操作将使您获得每行的颜色。

But other plot functions, like plt.scatter, will return a Collection. For a Collection, you can call result.get_facecolor(). This will return an array of color values of the foreground colors of the elements. So if they're all the same color (as they are when you make a scatter plot with just X and Y values), result.get_facecolor()[0]will suffice.

但是其他绘图函数,如plt.scatter,将返回一个Collection。对于 a Collection,您可以调用result.get_facecolor(). 这将返回元素前景色的颜色值数组。因此,如果它们都是相同的颜色(就像您制作仅包含 X 和 Y 值的散点图时那样),result.get_facecolor()[0]就足够了。

回答by finefoot

If you cannot access or store the return value of the call to plt.plot, you should also be able to use plt.gca().lines[-1].get_color()to access the color of the last line which was added to the plot.

如果您无法访问或存储对 调用的返回值plt.plot,您还应该能够使用plt.gca().lines[-1].get_color()访问添加到绘图中的最后一行的颜色。

In the following example, I'm creating example data, run curve_fitand show both data and fitted curve in the same color.

在下面的示例中,我正在创建示例数据,运行curve_fit并以相同的颜色显示数据和拟合曲线。

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit

m = 5
n = 3
x = np.arange(m)
y = np.array([i * x + np.random.normal(0, 0.2, len(x)) for i in range(n)])

def f(x, a, b):
    return a * x + b

for y_i in y:
    popt, pcov = curve_fit(f, x, y_i)
    plt.plot(x, y_i, linestyle="", marker="x")
    plt.plot(x, f(x, *popt), color=plt.gca().lines[-1].get_color())
plt.show()

Figure 1

图1