Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the color of the last figure in matplotlib?

I'm plotting some data sets with linear fits. I want the linear fit to have the same color as the plotted data (error bars). How can I get that color?

like image 290
Yotam Avatar asked Nov 15 '12 10:11

Yotam


People also ask

What is the function to give color to plot in Matplotlib?

colors() This function is used to specify the color.

What does PLT show () do?

plt. show() starts an event loop, looks for all currently active figure objects, and opens one or more interactive windows that display your figure or figures. The plt. show() command does a lot under the hood, as it must interact with your system's interactive graphical backend.


1 Answers

You might try this:

x = np.arange(10)
y = np.arange(10)
err = np.ones(10)
ebar = plt.errorbar(x,y, yerr=err)
color = ebar[0].get_color()

ebar is a container of artist, so you might modify the index in the last line to match the artist you want to get color from.

You can also easily set the color of the errorbar, so you know exactly what color they are without checking it:

ebar = plt.errorbar(x,y, yerr=err, ecolor='y')
like image 91
btel Avatar answered Oct 18 '22 23:10

btel