Home > other >  How to plot multiple scatter plots with a global colourbar in python?
How to plot multiple scatter plots with a global colourbar in python?

Time:03-12

I have a dataset from which I've calculated a value which I've used in a colourbar for a plot. See below:

enter image description here

I'm trying to figure out how to combine it so that another parameter is plotted over it (see below:) enter image description here

I want the colours to be added to the different shapes for each of the classes.

Some of the code used is below:

plt.figure(figsize=[12,10])
plt.scatter(x_offset1, y_offset1, s=2, c=index, cmap='jet', marker='.')
plt.scatter(x_offset2, y_offset2, s=2, c=index, cmap='jet', marker='^')
plt.scatter(x_offset3, y_offset3, s=2, c=index, cmap='jet', marker='v')
plt.scatter(x_offset4, y_offset4, s=2, c=index, cmap='jet', marker='x')

plt.colorbar()

This gives the error:

ValueError: 'c' argument has 289 elements, which is inconsistent with 'x' and 'y' with size 3.

I know that the 289 elements is the colour data for the whole dataset and the size 3 is the size of the first class of plots but I'm not sure how to plot the colour values of just the 3 points only.

CodePudding user response:

What you are looking for is a slice of the index list. Slices work like this:

slice = pizza[:2]

And go in the format of list[start:stop:step]. So, if your colors go from the first item in the list to the third item, then you would do:

plt.scatter(x_offset4, y_offset4, s=2, c=index[1:3], cmap='jet', marker='x')

Assuming index contains ["a", "b", "c", "d"] (just for the sake of an example), index[1:3] will return ["a", "b", "c", "d"]. This operation does not modify the original list, so index will still be ["a", "b", "c", "d"] after index[1:3].

  • Related