21

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?

8
  • What do you expect to see when providing color=[0.3,0.3,0.5]? When you provide a list as a color parameter, matplotlib understands you want the first event to be color[0], second to be color[1], .... Jul 28, 2017 at 1:24
  • I want each tick mark to have the color defined by [0.3,0.3,0.5], which is a dark navy blue that I find easier on the eyes than the default 'b'. Do I really have to make a list of same length of data where each element is [0.3,0.3,0.5]?
    – Bow
    Jul 28, 2017 at 1:29
  • No, you don't need that, try passing color = (0.3,0.3,0.5), this way, as a tuple, that should work. Jul 28, 2017 at 1:33
  • Are you sure? Did it work for you? Using a tuple still doesn't work for me :(
    – Bow
    Jul 28, 2017 at 1:36
  • 1
    Yes, thank you it works!
    – Bow
    Jul 28, 2017 at 2:43

2 Answers 2

19

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()

1
  • 3
    Heads up for anyone trying this color syntax for the .plot function. In that case, drop the list of the tuple, but just give a tuple of RGB values.
    – zwep
    Jun 8, 2020 at 13:32
1
# 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
1
  • 5
    I do believe that the max RGB quantity is, actually, 256, since it is a range between 0 and 255
    – olenscki
    Oct 27, 2020 at 17:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.