matplotlib Plot multiple scatter plots, each colored by different thrid variable

↘锁芯ラ 提交于 2019-12-11 12:23:25

问题


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 the second and third.

Any help would be appreciated

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, cmap=jet)
plt.scatter(a, b, s=100, c=c, cmap=jet)
plt.scatter(d, e, s=100, c=f, cmap=jet)

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

回答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()



回答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:



来源:https://stackoverflow.com/questions/29201356/matplotlib-plot-multiple-scatter-plots-each-colored-by-different-thrid-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!