Python Scatter Plot with Colorbar and Legend Issues

三世轮回 提交于 2020-01-14 02:51:26

问题


I'm working with a pretty simple example. I create three scatter plots on the same set of axes, and each data set I plot has a different associated colormap. However, the legend does not look as I'd want it to; why is this?

import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0,10,100)
x = np.random.rand(100,3)
y = np.random.rand(100,3)

colmaps = ['Blues', 'Greys', 'Reds']
for i in range(3):
    plt.scatter(x[:,i], y[:,i], c=t, cmap=colmaps[i], label=i)

plt.legend()
plt.show()

This produces a figure like below:

I was hoping for the first label to be blue, the second to be grey, and the third to be red, so they're associated with the colormap, but it looks like that's not how it works. Is there a simple way to do this?

Thanks


回答1:


You can set the legend colors as such:

import numpy as np
import matplotlib.pyplot as plt

t = np.linspace(0,10,100)
x = np.random.rand(100,3)
y = np.random.rand(100,3)
colmaps = ['Blues', 'Greys', 'Reds']

for i in range(3):
    plt.scatter(x[:,i], y[:,i], c=t, cmap=colmaps[i], label=i)

plt.legend()
ax = plt.gca()
legend = ax.get_legend()
legend.legendHandles[0].set_color(plt.cm.Blues(.8))
legend.legendHandles[1].set_color(plt.cm.Greys(.8))
legend.legendHandles[2].set_color(plt.cm.Reds(.8))
plt.show()

I set the color of each legendHandle to a specific value in the respective colormap.

If you make the scatter dot's size larger you can see the color and associate individual dots with the legend easier. I also set just one dot per scatter plot in the legend, rather than the default 3, and set the legend's alpha to 0.5, and alpha of scatter plot to 0.7.

...
for i in range(3):
    plt.scatter(x[:,i], y[:,i], c=t, cmap=colmaps[i], label=i, s=200, alpha=0.7)
plt.legend(markerscale=0.7, scatterpoints=1)
ax = plt.gca()
legend = ax.get_legend()
legend.legendHandles[0].set_color(plt.cm.Blues(.8))
legend.legendHandles[1].set_color(plt.cm.Greys(.8))
legend.legendHandles[2].set_color(plt.cm.Reds(.8))
legend.get_frame().set_alpha(0.5)
...




回答2:


I do not quite get why you do c=t...but is this what you wanted?

Here is the code:

  1 import numpy as np 
  2 import matplotlib.pyplot as plt
  3                    
  4 colors = ['b', 'c', 'r']
  5 markers = ['x', 'o', '^']
  6 scatters = []      
  7                    
  8 x = np.random.rand(100,3)
  9 y = np.random.rand(100,3)
 10                    
 11 for i in range(3): 
 12     scatters.append(plt.scatter(x[:,i], y[:,i], color=colors[i], marker=markers[i], label=i))
 13                    
 14 plt.legend((scatters[0], scatters[1], scatters[2]),
 15             ('scatter 1', 'scatter 2', 'scatter 3'),
 16             scatterpoints=1,
 17             loc='upper right',
 18             ncol=3,                                                                                                 
 19             fontsize=8)
 20 plt.show()


来源:https://stackoverflow.com/questions/30677151/python-scatter-plot-with-colorbar-and-legend-issues

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