Home > Back-end >  Same color range for multiple scatterplots
Same color range for multiple scatterplots

Time:10-25

When doing multiple scatterplots, I want the colors of the new plotted points to be in line with the same colorbar. I have my base plot (colors are stored in y array):

im = ax.scatter(embs[:,0],
           embs[:,1],
           edgecolor='k',
           linewidth=0.2,
           c=y
           )

enter image description here

Now I want to highlight a specific point, by plotting a red contour (I'm highlighting the yellow point top right):

ax.scatter(embs[5715][0],
            embs[5715][1],
            edgecolor='red',
            linewidth=0.3,
            c=y[5715]
            )

The problem is that its color now won't agree with the original one:

enter image description here

CodePudding user response:

One easy way to fix this is to pass the norm of the object returned by your first scatter call as the norm of the second call.

This ensures that the range of data used for the first scatter plot is respected for the second one

e.g.

import matplotlib.pyplot as plt
import numpy as np

x, y = np.random.normal(size=(2, 1000))
c = x ** 2   y ** 2

cb = plt.scatter(x, y, c=c, ec="k")
plt.scatter(x[777], y[777], c=c[777], ec="r", norm=cb.norm)

plt.colorbar(cb)

plt.show()

which will give e.g.

enter image description here

  • Related