简体   繁体   English

matplotlib从plt.plot命令检索颜色

[英]matplotlib retrieve color from plt.plot command

How to obtain the color used by plt.plot after calling it? 调用后如何获取plt.plot使用的颜色? I dont want to specify the color in advance. 我不想提前指定颜色。

def plotStuff():

    lines = plt.plot(np.random.rand(500))
    color = lines.magic_thing_get_color
    plt.plot(np.random.rand(500),color = color,label = "_nolegend_" )

So calling plotStuff twice would use one color to plot 2 things the first time and a different color when calling it the second time. 因此,两次调用plotStuff会在第一次使用一种颜色绘制2件东西,而在第二次调用时使用另一种颜色。

The magic function you are searching is get_color() . 您正在搜索的魔术函数get_color() However, the plot command returns a list with the lines objects and thus, you have to call this function on the items and not the list itself. 但是,plot命令返回带有lines对象的列表,因此,您必须在项目上调用此函数,而不是列表本身。 Your function could look like 您的功能可能看起来像

import matplotlib.pyplot as plt
import numpy as np
def plotStuff():
    lines = plt.plot(np.random.rand(20))
    color = lines[0].get_color()
    plt.plot(np.random.rand(20),color = color,label = "_nolegend_" )
plotStuff()

creating a plot like 创建一个情节像
在此处输入图片说明
with both lines having the same color. 两条线的颜色相同。

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

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