Skip to content Skip to sidebar Skip to footer

Matplotlib Plot Multiple Scatter Plots, Each Colored By Different Thrid Variable

I'm trying to plot multiple pairs of data on a single scatter plot, each colored by a different third variable array. The coloring seems to work for the first plot, then fails for

Solution 1:

I have removed the plt.clim(0,5) line and added minimal and maximal values for all plots and that seems to work.

import matplotlib.pyplot as plt

jet=plt.get_cmap('jet')

x = [1,2,3,4]
y = [1,2,3,4]
z = [1,1,1,1]

a = [2,3,4,5]
b = [1,2,3,4]
c = [2,2,2,2]

d = [3,4,5,6]
e = [1,2,3,4]
f = [3,3,3,3]

plt.scatter(x, y, s=100, c=z, vmin=1, vmax=5, cmap=jet)
plt.scatter(a, b, s=100, c=c, vmin=1, vmax=5, cmap=jet)
plt.scatter(d, e, s=100, c=f, vmin=1, vmax=5, cmap=jet)

plt.colorbar()
plt.show()

Solution 2:

The problem is that your colormap is being renormalized for each of your plot commands. Also. As a matter of style, jet is basically never the right colormap to use. So try this:

import matplotlib.pyplot as plt

jet=plt.get_cmap('coolwarm')

x = [1,2,3,4]
y = [1,2,3,4]
z = [1,1,1,1]

a = [2,3,4,5]
b = [1,2,3,4]
c = [2,2,2,2]

d = [3,4,5,6]
e = [1,2,3,4]
f = [3,3,3,3]

plt.scatter(x, y, s=100, c=z, cmap=jet, vmin=0, vmax=4)
plt.scatter(a, b, s=100, c=c, cmap=jet, vmin=0, vmax=4)
plt.scatter(d, e, s=100, c=f, cmap=jet, vmin=0, vmax=4)

plt.clim(0,5)
plt.colorbar()
plt.show()

Makes a nice plot: enter image description here

Post a Comment for "Matplotlib Plot Multiple Scatter Plots, Each Colored By Different Thrid Variable"