Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: how to set line color to orange, and specify line markers?

I have a situation where I have many lines that I am plotting in pyplot.

They are grouped by color, and within each color, I plot according to plot style--so the circles, dashes, etc.

My plot styling is:

plt.plot(x,y1,'b')
plt.plot(x,y2,'bs')
plt.plot(x,y3,'b--')

And then I repeat for various colors. However, I am running into trouble with orange. When I plot with orange, I get an error because pyplot wants to plot with circles instead of the color orange! Here is an example:

plt.plot(x,z1,'o')
plt.plot(x,z2,'os')
plt.plot(x,z3,'o--')

This fails because 'os' is being parsed as two formatting instructions, rather than a color and the format: squares.

How do I work around this in order to plot the orange lines?

like image 290
Chris Avatar asked Feb 01 '17 15:02

Chris


People also ask

How do I assign a line color in matplotlib?

The usual way to set the line color in matplotlib is to specify it in the plot command. This can either be done by a string after the data, e.g. "r-" for a red line, or by explicitely stating the color argument.

How do I change the marker color in matplotlib?

All of the line properties can be controlled by keyword arguments. For example, you can set the color, marker, linestyle, and markercolor with: plot(x, y, color='green', linestyle='dashed', marker='o', markerfacecolor='blue', markersize=12).

How do I specify RGB in matplotlib?

Matplotlib recognizes the following formats to specify a color. RGB or RGBA (red, green, blue, alpha) tuple of float values in a closed interval [0, 1]. Case-insensitive hex RGB or RGBA string. Case-insensitive RGB or RGBA string equivalent hex shorthand of duplicated characters.

How do you specify a RGB color in python?

# In Python, colors can just be stored as 3-Tuples of (Red, Green, Blue). red = (255,0,0) green = (0,255,0) blue = (0,0,255) # Many libraries work with these. # You can also, of course, define your own functions to work with them.


3 Answers

That is because the character 'o' is not a pre-defined single-letter color code. You will instead need to use either the RGB value or the string 'orange' as your color specification.

plt.plot(x, z3, '--', color='orange')           % String colorspec
plt.plot(x, z3, '--', color='#FFA500')          % Hex colorspec
plt.plot(x, z3, '--', color=[1.0, 0.5, 0.25])   % RGB colorspec
like image 190
Suever Avatar answered Sep 29 '22 16:09

Suever


"o" is not one of the available color codes.

An alternative to plt.plot(x,z3,'o--') is, for example,

plt.plot(x, z3, '--', color="orange")
like image 22
Warren Weckesser Avatar answered Sep 29 '22 15:09

Warren Weckesser


Use HTML colour codes to specify the colour of your plot. Something along the lines of ->

plt.plot(x,y,'o',color='#F39C12')

This plots x,y with orange circles. orange circles look pretty with sinusiods

Edit: You can nitpick your color at http://htmlcolorcodes.com/

like image 45
akshay chawla Avatar answered Sep 29 '22 16:09

akshay chawla