简体   繁体   English

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

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

I plot a line without specifying the color (think: plt.plot(x,y)).我绘制了一条没有指定颜色的线(想想:plt.plot(x,y))。 Say the color comes out blue.假设颜色是蓝色的。

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] 是否总是访问最近绘制的线?

In your example, p is a list of Line2D object.在您的示例中, p 是 Line2D 对象的列表。 In that example you have only one line object, p[0].在该示例中,您只有一个线对象 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.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'

线图

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_fit and 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()

图 1

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.plot ,在它返回的列表的每个元素上执行item.get_color()将获得每行的颜色。

But other plot functions, like plt.scatter , will return a Collection .但是其他绘图函数,如plt.scatter ,将返回一个Collection For a Collection , you can call result.get_facecolor() .对于Collection ,您可以调用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.因此,如果它们都是相同的颜色(就像您制作仅包含 X 和 Y 值的散点图时一样), result.get_facecolor()[0]就足够了。

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

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