0

How to obtain the color used by plt.plot after calling it? 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.

user1830663
  • 521
  • 3
  • 8
  • 16
  • instead of retrieving it, it might be easier to set the color cycle in advance. related: http://stackoverflow.com/q/24193174/2096752 – shx2 Dec 31 '14 at 05:48

1 Answers1

1

The magic function you are searching is 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. 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
enter image description here
with both lines having the same color.

Jakob
  • 19,815
  • 6
  • 75
  • 94