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?
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.
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).
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.
# 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.
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
"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")
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With