如何在Matplot散点图中为每个气泡设置set_gid()? [英] How to set_gid() for each bubble in matplot scatter chart?
问题描述
我用下面的代码画了一个散点图:
I draw a scatter chart by below codes:
import matplotlib.pyplot as plt
x = [2,4,6]
y = [1,3,7]
r = [650,890,320]
clr = ['r','b','g']
bubble_id = ['C0','C1','C2']
H0 = plt.scatter(x,y,s=r,c=clr)
然后我想将 'set_gid()'
分别设为 'C0', 'C1' ,'C2'
三个气泡.怎么做 ?由于 H0
是单个对象
,我不知道如何分解 H0 并找到三个泡泡儿子"的H0.感谢您的提示.
Then I want to 'set_gid()'
to the three bubbles as 'C0', 'C1' ,'C2'
respectively. How to do that ? As H0
is a single object <matplotlib.collections.PathCollection object at 0x0ADA6D30>
, I don't know how to break down H0 and find the three 'bubble son' of H0 . Thanks for your tips.
推荐答案
所以,我知道这可能不是最有效的解决方案,但是循环呢?
So, I know that might not be the most efficient solution, but what about looping?
import matplotlib.pyplot as plt
import itertools as it
X = [2,4,6]
Y = [1,3,7]
radius = [650,890,320]
clr = ['r','b','g']
bubble_id = ['C0','C1','C2']
ax = plt.subplot(111)
for x, y, r, c, id in it.izip(X, Y, radius, clr, bubble_id):
ax.scatter(x,y,s=r,c=c, gid=id)
在视觉上给出相同的情节.
visually gives the same plot.
在Ipython中,我介绍了 PathCollection
方法,看来没有简单的方法可以从中获取单个补丁.
In Ipython I've given a look at PathCollection
methods, and it looks like there is no trivial way to get out the single patches from it.
在2020年进行编辑
由于现在您很可能使用python 3(如果不是,则应该使用),因此可以使用内置的 zip
而不是 itertools.izip
.谢谢@Guimoute回复了答案
Since nowadays your are very likely using python 3 (if not you should), it is possible to use the built-in zip
instead of itertools.izip
.
Thank you @Guimoute for reviving the answer
这篇关于如何在Matplot散点图中为每个气泡设置set_gid()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!