简体   繁体   English

带有Colorbar和图例问题的Python散点图

[英]Python Scatter Plot with Colorbar and Legend Issues

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 谢谢

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. 我将每个legendHandle的颜色设置为各自的颜色图中的特定值。

在此处输入图片说明

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. 我还为图例中的每个散点图设置了一个点,而不是默认值3,并将图例的alpha设置为0.5,散点图的alpha设置为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)
...

在此处输入图片说明

I do not quite get why you do c=t ...but is this what you wanted? 我不太明白为什么要这样做c=t ...但这是您想要的吗?

在此处输入图片说明

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM