2

I Have two sets of data that I would like to compare. Each set of data has x and y values along with some z values for each x,y point. The distribution of z values between the two data sets may overlap each other, but generally will have portions that do not overlap. In this case [1,4] and [2,6]. I would like for the color scheme to take this into account so I can visualize these differences when comparing the two sets. I would eventually like to use a colorbar added to this figure as well. Some example code:

# Fake Values
vals1 = np.array([[1,1,1],[2,2,4]])
vals2 = np.array([[1,1,2],[2,2,6]])

fig, ax = plt.subplots(1,2, constrained_layout=True)
g1 = ax[0].scatter(x=vals1[:,0], y=vals1[:,1], c=vals1[:,2], cmap='RdBu')
g2 = ax[1].scatter(x=vals2[:,0], y=vals2[:,1], c=vals2[:,2], cmap='RdBu')
fig.colorbar(g2)

This gives me the following:

Scatterplot with bad color scheme

As you can see the z (c?) values are not standardized between the subplots. Any help would be greatly appreciated.

StephB
  • 35
  • 6

1 Answers1

2

You can set the vmin/vmax of both plots as the data's global min/max.

Either set the vmin/vmax params individually:

vmin = np.vstack([vals1,vals2]).min()
vmax = np.vstack([vals1,vals2]).max()

fig, ax = plt.subplots(1,2, constrained_layout=True)
g1 = ax[0].scatter(x=vals1[:,0], y=vals1[:,1], c=vals1[:,2], vmin=vmin, vmax=vmax, cmap='RdBu')
g2 = ax[1].scatter(x=vals2[:,0], y=vals2[:,1], c=vals2[:,2], vmin=vmin, vmax=vmax, cmap='RdBu')
fig.colorbar(g2)

Or create a matplotlib.colors.Normalize() instance and use it for the norm param:

norm = mcolors.Normalize(
    vmin=np.vstack([vals1,vals2]).min(),
    vmax=np.vstack([vals1,vals2]).max(),
)

fig, ax = plt.subplots(1,2, constrained_layout=True)
g1 = ax[0].scatter(x=vals1[:,0], y=vals1[:,1], c=vals1[:,2], norm=norm, cmap='RdBu')
g2 = ax[1].scatter(x=vals2[:,0], y=vals2[:,1], c=vals2[:,2], norm=norm, cmap='RdBu')
fig.colorbar(g2)

synced vmin/vmax

tdy
  • 36,675
  • 19
  • 86
  • 83
  • 2
    You can also create the `Norm` outside of the call to scatter and pass it to both of them. – tacaswell Apr 24 '21 at 02:31
  • 1
    Hi @tdy and thanks for your reply! It looks like there was typo in my original code which was recapitualted in your reply. If you change the c value in the g2 line to: c=vals2[:,2] it will be correct! – StephB Apr 24 '21 at 17:14