···
Mike,
If your locations were integers or floats rather than strings, you could just change the scatter call to the following:
ax.scatter(dates,IDs,c=
locations,marker=‘d’)
I
don’t know about a legend… I don’t know if that is possible with a scatter plot (?). Because scatter plots get their colors based off of a color map, you could generate a color bar for your data. You may need to
capture the collection object returned from the scatter plot function call, though. Here’s your code with these modifications:
Of course, you need to change your locations list to integers rather than strings.
fig = plt.figure()
ax = fig.add_subplot(111)
sc = ax.scatter(dates,IDs,c=locations,marker=‘d’)
ax.xaxis_date()
fig.autofmt_xdate()
plt.colorbar(sc)
plt.grid(True)
plt.show()
If
you really need a legend, then you could do a loop of plot commands for
each set of unique locations. Using some fancy Numpy masking makes the process easier…
import numpy as np
import matplotlib.pyplot as plt
IDs = np.array([47, 33, 47, 12, 50, 50, 27, 27, 16, 27])
locations = np.array([‘201’, ‘207’, ‘207’, ‘205’, ‘204’, ‘201’, ‘209’, ‘209’, \
'207','207'])
dates = np.array([ 733315.83240741, 733315.83521991, 733315.83681713,
733315.83788194, 733336.54554398, 733336.54731481,
733337.99842593, 733337.99943287, 733338.00070602,
733338.00252315])
fig = plt.figure()
ax = fig.add_subplot(111)
cs = [‘r’, ‘b’, ‘g’, ‘k’, ‘c’]
for n, i in enumerate(np.unique(locations)):
ax.plot(dates[locations==i],IDs[locations==i],'d', c=cs[n%len(cs)], label=i)
ax.xaxis_date()
fig.autofmt_xdate()
plt.legend(numpoints=1)
plt.grid(True)
plt.show()
Not sure if this is exactly what you wanted, but I hope it helps a little.
Ryan
On Mon, Oct 3, 2011 at 2:49 PM, Michael Castleton <fatuheeva@…9…> wrote:
Hello,
I am using Matplotlib 1.0.0 in Python 2.6.
I am trying to plot time series data of unique IDs and color the points
based on location. Each data point has a unique ID value, a date value, and
a location value.
The unique IDs and date values are plotting fine but I am unable to control
the color and subsequently the legend.
Here is a sample of the data.
IDs = [47, 33, 47, 12, 50, 50, 27, 27, 16, 27]
locations = [‘201’, ‘207’, ‘207’, ‘205’, ‘204’, ‘201’, ‘209’, ‘209’,
‘207’,‘207’]
dates = [ 733315.83240741, 733315.83521991, 733315.83681713,
733315.83788194, 733336.54554398, 733336.54731481,
733337.99842593, 733337.99943287, 733338.00070602,
733338.00252315]
This basic code works.
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(dates,IDs,marker=‘d’)
ax.xaxis_date()
fig.autofmt_xdate()
plt.grid(True)
plt.show()
I’ve been trying to figure out how to set color = locations with no success.
Any ideas out there?
Thanks,
Mike
–
View this message in context: http://old.nabble.com/color-problems-in-scatter-plot-tp32584727p32584727.html
Sent from the matplotlib - users mailing list archive at Nabble.com.
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users