1

Ok so I'm trying to create a scatter plot that contains 4 pieces of information per point, the x and y data, marker style and colour.

So I have a list of tuples containing this data like (string1,string2,float,int)

To give some context my data is a list of tuples like this:

new_data = [('Alice','A',0.1,100),('Bob','B',0.2,200)....]

There around 100 points but less than 100 names (Alice Bob etc) and less than 100 letters (A B etc).

My plan is to have each data point refer to the plot like this:

(marker,x_axis,y_axis,colour)

so I have managed to do nearly all of this (using some looping stuff), except creating a colourbar, which I can't get to work (and its starting to make me think I've gone about thisall wrong). I've tried some more complicated methods of getting the colorbar but I thought I should show the simplest one here.

cols = [x[3] for x in new_data]
#all the values i want to use for the colours

poss_cols = np.linspace(min(cols),max(cols),max(cols)-min(cols)+1) 
#not every number in the range exists in my data, so these are the 
possible ones

col_range = cm.rainbow(np.linspace(0,1,max(cols)-min(cols)+1))
#create the colourmap


col_dict = {k:v for k,v in zip(poss_cols,col_range)}
#this seemed the only sensible way to link the arbitrary int i have to a color array (i could easily be making this more complicated though)

names = list(set([x[0] for x in new_data]))
#all unique names (there are 9)
markers = ['.','2','v','^','<','>','s','*','x']
mark_dict = {k:v for k,v in zip(names,markers)}

plt.figure()
for name,mark in mark_dict.items():        
    x_data = [x[1] for x in new_data if x[0] == sub]
    y_data = [x[2] for x in new_data if x[0] == sub]
    colours = [x[3] for x in new_data if x[0] == sub]
    for x,y,c in zip(x_data,y_data,colours):
        plt.scatter(x,y,marker=mark,color=col_dict[c])
plt.colorbar(ax=plt.gca())

and this gives me this error:

TypeError: You must first set_array for mappable

which I don't fully understand. I've seen other examples where people assign the plt.scatter() to a variable and pass that to the colorbar but that doesn't seem to work for me (I'm guessing because that variable gets redefined with each iteration)

The other issue is the ticks on the colorbar should be in the range of the original values (lets say 100 to 300) and not 1 to 0, but I can probably figure that out on my own.

At this point I'm starting to think this isn't the best method for creating this plot, so any ideas are welcome!

bidby
  • 708
  • 1
  • 10
  • 24
  • I just gave a [new answer](https://stackoverflow.com/a/53228129/4124317) to the duplicate which might be most useful here, namely to only create a single scatter plot. – ImportanceOfBeingErnest Nov 09 '18 at 14:57

0 Answers0