Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass rgb color values to python's matplotlib eventplot?

I'm simply trying to plot some tick marks with a specific color using matplotlib's eventplot. I'm running Python 3 in Jupyter notebook with %matplotlib inline.

Here's an example code:

import numpy as np
import matplotlib.pyplot as plt    
spikes = 100*np.random.random(100)
plt.eventplot(spikes, orientation='horizontal', linelengths=0.9, color=[0.3,0.3,0.5])

It outputs the following error:

ValueError: colors and positions are unequal sized sequences

The error occurs presumably because I am not providing a list of colors of the same length as the data (but I wan't them to all just be the same color!). It also gives an error when I use a color string like 'crimson' or 'orchid'. But it works when I use a simple one-letter string like 'r'.

Am I really restricted to just using the extremely limited set of one-letter color strings 'r','b','g','k','m','y', etc... or making a long color list when using this eventplot?

like image 492
Bow Avatar asked Jul 28 '17 00:07

Bow


People also ask

How do you assign 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.

How do you assign a color to a plot in Python?

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.

Does MatPlotLib use RGB?

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].


2 Answers

According to the docs:

you can pass an (r, g, b) or (r, g, b, a) tuple, where each of r, g, b and a are in the range [0,1].

import numpy as np
import matplotlib.pyplot as plt

spikes = 100*np.random.random(100)
plt.eventplot(spikes, orientation='horizontal', linelengths=0.9, color = [(0.3,0.3,0.5)])

plt.show()

like image 75
Vinícius Figueiredo Avatar answered Sep 20 '22 15:09

Vinícius Figueiredo


# for a list of three plotted variables

df = data[['x', 'y', 'z']]
color_theme=[(52/235, 235/235, 86/235), (52/235, 70/235, 235/235), 
(165/235, 52/235, 235/235)]           
df.plot(color=color_theme)

# where numerators are the R,G,B numbers
# python only uses RGB in numbers <1
# denominator is obviously the max RGB quantity on a normal RGB scale
like image 33
some_texan Avatar answered Sep 19 '22 15:09

some_texan