2

My data are :

import matplotlib.pyplot as plt
from datetime import *
import matplotlib

my_vectors =[['120819152800063',10,189, 8],
 ['120819152800063', 10,184, 8],
 ['120819152800063', 0,190, 43],
 ['120819152800067', 8,67, 10],
 ['120819152800067', 8,45, 10],
 ['120819152800073', 10,31, 8],
 ['120819152800073', 10,79, 8],
 ['120819152800073', 7,102, 25],
 ['120819152800075', 125,0, 13]]

I'm trying to plot vectors to represent the data, but i cheated(i represent a line which represent the body of the vector and 2 points for the beginning and the end), I just want to color the body of each 'vector'.

timeString = zip(*my_vectors)[0]
timeDatetime=[datetime.strptime(aTime, '%y%m%d%H%M%S%f') for aTime in timeString]
timeDate=matplotlib.dates.date2num(timeDatetime)
# Represent the data time
X = tuple([int(x) for x in timeString])
# Represent the data sender
Y = zip(*my_vectors)[1]
# represent the data type 
U = zip(*my_vectors)[2]
# represent the data receiver
V = zip(*my_vectors)[3]

# the 'body' of the vectors
plt.vlines(timeDate,Y,V,colors='r')
# the beginning of the vectors
plt.plot_date(timeDate,Y,'b.',xdate=True)
# the end of the vectors
plt.plot_date(timeDate,V,'y.')
plt.show()

To have a better read of my data, i need to change color for each data type but i don't know how many data type i'll have. How can i do this?

I have read this question but i don't really understand the answer :

Setting different color for each series in scatter plot on matplotlib

Community
  • 1
  • 1
Jguillot
  • 214
  • 3
  • 14

1 Answers1

1

You can access different colors by using a colormap. The module matplotlib.cm provides colormaps or you can create your own. This demo shows the available colormaps and their names. Colormaps contain 256 different colors. You can iterate through your data and assign a different color to each vector as is done in the example you linked to. If you have more than 256 data points you'll need to repeat colors or use a larger colormap but you might get into the limits of spectral resolution anyway.

Here's an example based on your question.

from matplotlib import pyplot as plt
import matplotlib.cm as cm
import numpy as np

# Some fake data
timeDate = np.arange(256)
Y = timeDate * 1.1 + 2
V = timeDate * 3 + 1

# Select the color map named rainbow
cmap = cm.get_cmap(name='rainbow')

# Plot each vector with a different color from the colormap. 
for ind, (t, y, v) in enumerate(zip(timeDate, Y, V)):
    plt.vlines(t,y,v ,color = cmap(ind))

plt.show()

plot using a color map on vlines

Alternately, you can access the RGBA values from the colormap directly:

plt.vlines(timeDate,Y,V,colors=cmap(np.arange(256)))
Molly
  • 13,240
  • 4
  • 44
  • 45