简体   繁体   English

如何在matplotlib中绘制具有不同边缘颜色的散点图?

[英]How to do a scatter plot with different edgecolor in matplotlib?

I wish to plot a scatter plot with no facecolors but with different edgecolors .我希望绘制一个没有 facecolors但有不同 edgecolors 的散点图 In other words, colorful circles.换句话说,彩色圆圈。

I thought I could easily found solution in matplotlib gallery but surprisingly I couldn't.我以为我可以很容易地在 matplotlib 库中找到解决方案,但令人惊讶的是我不能。 Parameter "edgecolors" does not accept array as values.参数“edgecolors”不接受数组作为值。 And manipulate parameter 'c' won't help with edgecolors but facecolors.并且操纵参数“c”对边缘颜色没有帮助,但对面颜色没有帮助。

Here is a link that I thought would be the solution: https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.scatter.html这是我认为是解决方案的链接: https : //matplotlib.org/api/_as_gen/matplotlib.axes.Axes.scatter.html 在此处输入图片说明

The demo picture at the end of the page seems to have colorful edgecolors!页面末尾的演示图片似乎有彩色边缘颜色! Then I download the script and run it:然后我下载脚本并运行它: 在此处输入图片说明

The edge is still black.边缘仍然是黑色的。

I am now doing my plot other ways.我现在正在以其他方式做我的阴谋。 (since this is not a big matter. This is only a question of interest.) I just wonder how did the demo get the colorful edges. (因为这不是什么大问题。这只是一个感兴趣的问题。)我只是想知道演示是如何获得彩色边缘的。

As others have said the way that the demo script got the colourful edges is that matplotlib 2.0 automatically sets the edge colour of the points in the scatter plot to the provided colour argument.正如其他人所说,演示脚本获得彩色边缘的方式是 matplotlib 2.0 自动将散点图中点的边缘颜色设置为提供的颜色参数。

If, however, you want to recreate the results of the demo script without updating to mpl 2.0 (though I recommend it) you only need to add an additional keyword parameter to the scatter line as follows但是,如果您想在不更新到 mpl 2.0 的情况下重新创建演示脚本的结果(尽管我推荐它),您只需要向散点行添加一个额外的关键字参数,如下所示

plt.scatter(x, y, s=area, c=colors, alpha=0.5, edgecolors=colors)

And for the sake of completeness you can implement the colourful circle idea that you originally had by faking it.为了完整起见,您可以通过伪造来实现您最初拥有的彩色圆圈想法。 You only need to change the internal colour to white, ie你只需要把内部颜色改成白色,即

plt.scatter(x, y, s=area, c='w', alpha=0.5, edgecolors=colors)

Additionally, you can't use a single float to define the colour for edgecolors as they are doing for the 'c' argument in the demo script so you either need to supply an array of length N that are all specified colours, eg ['r', 'g', 'b'], or replace the definition of the colours with此外,您不能使用单个浮点数来定义 edgecolors 的颜色,因为它们在演示脚本中为 'c' 参数所做的那样,因此您需要提供一个长度为N的数组,这些数组都是指定的颜色,例如 [' r', 'g', 'b'],或用替换颜色的定义

colors = np.random.random((N, p))

where p is either 3 or 4 depending if you want RGB or RGBA colours in the plot.其中p是 3 或 4,具体取决于您是否想要图中的 RGB 或 RGBA 颜色。 This should produce something like the following plot这应该产生如下图所示的内容

来自 scatter_demo.py 的输出,没有面部颜色和彩色边缘

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

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