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.

1
  • instead of retrieving it, it might be easier to set the color cycle in advance. related: stackoverflow.com/q/24193174/2096752 Commented Dec 31, 2014 at 5:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.