Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One colorbar for multiple scatter plots

I'm plotting multiple figures of the same variable on one plot using matplotlib library. I'm not looking for a colorbar for subplots, which is the dominant search material. I plot multiple scatters, but the colorbar is only set to the values of the last scatter I plot.

Here is the part of the code:

plt.scatter(x1, y1, c=z1,cmap='viridis_r',marker='s')
plt.scatter(x2, y2, c=z2,cmap='viridis_r',marker='o')
plt.scatter(x3, y3, c=z3,cmap='viridis_r',marker='^')
plt.colorbar().set_label('Wind speed',rotation=270)
like image 585
Dr proctor Avatar asked Sep 08 '17 00:09

Dr proctor


People also ask

How do you plot multiple scatter plots?

Multiple scatter plots can be graphed on the same plot using different x and y-axis data calling the function Matplotlib. pyplot. scatter() multiple times.

What is the difference between plot () and scatter ()?

The difference between the two functions is: with pyplot. plot() any property you apply (color, shape, size of points) will be applied across all points whereas in pyplot. scatter() you have more control in each point's appearance.

What is a mappable Matplotlib?

A colorbar needs a "mappable" ( matplotlib. cm. ScalarMappable ) object (typically, an image) which indicates the colormap and the norm to be used. In order to create a colorbar without an attached image, one can instead use a ScalarMappable with no associated data.


2 Answers

It requires a bit of extra work:

  • You have to get the minimum and maximum of the cs (the colorbar values)
  • You have to clim each scatter plot

First the minimum and maximum:

zs = np.concatenate([z1, z2, z3], axis=0)
min_, max_ = zs.min(), zs.max()

Then the scatter plots with clim:

plt.scatter(x1, y1, c=z1,cmap='viridis_r',marker='s')
plt.clim(min_, max_)
plt.scatter(x2, y2, c=z2,cmap='viridis_r',marker='o')
plt.clim(min_, max_)
plt.scatter(x3, y3, c=z3,cmap='viridis_r',marker='^')
plt.clim(min_, max_)
plt.colorbar().set_label('Wind speed',rotation=270)

For a very simple dataset:

x1, x2, x3 = [1,2,3], [2,3,4], [3,4,5]
y1 = y2 = y3 = [1, 2, 3]
z1, z2, z3 = [1,2,3], [4,5,6], [7,8,9]

enter image description here

like image 192
MSeifert Avatar answered Oct 21 '22 00:10

MSeifert


scatter has a norm argument. Using the same norm for all scatters ensures that the colorbar produced by any of the plots (hence also the last) is the same for all scatter plots.

The norm can be a Normalize instance, to which minimum and maximum value are set and which produces a linear scaling in between. Of course you can also use any other norm provided in matplotlib.colors like PowerNorm, LogNorm, etc.

mini, maxi = 0, 2  # or use different method to determine the minimum and maximum to use
norm = plt.Normalize(mini, maxi)
plt.scatter(x1, y1, c=z1,cmap='viridis_r',marker='s', norm=norm)
plt.scatter(x2, y2, c=z2,cmap='viridis_r',marker='o', norm=norm)
plt.scatter(x3, y3, c=z3,cmap='viridis_r',marker='^', norm=norm)
plt.colorbar().set_label('Wind speed',rotation=270)
like image 26
ImportanceOfBeingErnest Avatar answered Oct 21 '22 02:10

ImportanceOfBeingErnest