python - Matplotlib:如何绘制具有不同颜色和注释的簇?

标签 python matplotlib

Matplotlib 让我非常困惑。我有一个 pd.DataFrame ,其中包含 xy 列和 cluster。我希望将这些数据绘制在 x-y 图上,其中每个簇都有不同的颜色以及哪个簇的注释。

我有能力单独完成这些工作。用不同颜色绘制数据:

for c in np.unique(data['cluster'].tolist()):
    df = data[data['c'].isin([c])]
    plt.plot(df['x'].tolist(),df['y'].tolist(),'o')
plt.show()

这会产生:

enter image description here

和注释:

fig, ax = plt.subplots()
x = df['x'].tolist()
y = df['y'].tolist()
ax.scatter(x, y)
for i, txt in enumerate(data['cluster'].tolist()):
    ax.annotate(txt, (x[i],y[i]))
plt.show()

这会产生:

enter image description here

如何将两者结合起来?我不明白如何将 figure/axes/plot API 混合在一起..

<小时/>

示例数据:

pd.DataFrame({'c': ['News',   'Hobbies & Interests',   'Arts & Entertainment',   'Internal Use',   'Business',   'Internal Use',   'Internal Use',   'Ad Impression Fraud',   'Arts & Entertainment',   'Adult Content',   'Arts & Entertainment',   'Internal Use',   'Internal Use',   'Reference',   'News',   'Shopping',   'Food & Drink',   'Internal Use',   'Internal Use',   'Reference'],  
'x': [-95.44078826904297,   127.71454620361328,   -491.93121337890625,   184.5579071044922,   -191.46273803710938,   95.22545623779297,   272.2229919433594,   -67.099365234375,   -317.60797119140625,   -175.90196228027344,   -491.93121337890625,   214.3858642578125,   184.5579071044922,   346.4012756347656,   -151.8809051513672,   431.6130676269531,   -299.4017028808594,   184.5579071044922,   184.5579071044922,   241.29026794433594],  
'y': [-40.87070846557617,   245.00514221191406,   43.07831954956055,   -458.2991638183594,   270.4497985839844,   -453.2981262207031,   -439.6551513671875,   -206.3104248046875,   205.25787353515625,   -58.520164489746094,   43.07831954956055,   -182.91664123535156,   -458.2991638183594,   19.559282302856445,   -281.3316650390625,   103.6922378540039,   280.2445373535156,   -458.2991638183594,   -458.2991638183594,   -113.96920776367188]})

最佳答案

出于方便的原因,我将使用 df.plot.scatter 语法,但应该(几乎)与 ax.scatter 相同。

好的,使用示例数据,您可以 specify a cmap like described in the docs :

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'c': ['News',   'Hobbies & Interests',   'Arts & Entertainment',   'Internal Use',   'Business',   'Internal Use',   'Internal Use',   'Ad Impression Fraud',   'Arts & Entertainment',   'Adult Content',   'Arts & Entertainment',   'Internal Use',   'Internal Use',   'Reference',   'News',   'Shopping',   'Food & Drink',   'Internal Use',   'Internal Use',   'Reference'],  
'x': [-95.44078826904297,   127.71454620361328,   -491.93121337890625,   184.5579071044922,   -191.46273803710938,   95.22545623779297,   272.2229919433594,   -67.099365234375,   -317.60797119140625,   -175.90196228027344,   -491.93121337890625,   214.3858642578125,   184.5579071044922,   346.4012756347656,   -151.8809051513672,   431.6130676269531,   -299.4017028808594,   184.5579071044922,   184.5579071044922,   241.29026794433594],  
'y': [-40.87070846557617,   245.00514221191406,   43.07831954956055,   -458.2991638183594,   270.4497985839844,   -453.2981262207031,   -439.6551513671875,   -206.3104248046875,   205.25787353515625,   -58.520164489746094,   43.07831954956055,   -182.91664123535156,   -458.2991638183594,   19.559282302856445,   -281.3316650390625,   103.6922378540039,   280.2445373535156,   -458.2991638183594,   -458.2991638183594,   -113.96920776367188]})

df['col'] = df.c.astype('category').cat.codes

cmap = plt.cm.get_cmap('jet', df.c.nunique())
ax = df.plot.scatter(
    x='x',y='y', c='col',
    cmap=cmap
)
plt.show()

这里 get_cmap 采用 cmap 名称(您可以在 this example page 上找到各种 map 的名称)和

an integer giving the number of entries desired in the lookup table,

上面的代码结果如下: enter image description here

如果您想添加注释并隐藏颜色条,请使用:

ax = df.plot.scatter(
    x='x',y='y', c='col',
    cmap=cmap, colorbar=False
)
for i, txt in enumerate(df['c'].tolist()):
    ax.annotate(txt, (df.x[i], df.y[i]))
plt.show()

并得到以下内容: enter image description here

提示:如果太小,请使用 plt.scatter(x,y,s=None, c=None, **kwds) 中的“s”参数来更改大小。

关于python - Matplotlib:如何绘制具有不同颜色和注释的簇?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48886737/

相关文章:

python - 如何在没有 Pandas 的情况下在 Python 中处理 csv 文件

Python:正确处理子命令的全局选项的参数解析器

python - 如何将 dtype 分类变量转换为数值?

python - 多处理对 urllib2 没用?

python - 无意中在 matplotlib 中绘制多条线

python - 使用 Tweepy 抓取抓取坐标并绘制它们

python - 如何用Django HTTP.Request处理,请求内容类型,查询参数

python - 保存交互式 Matplotlib 图形

python - 在散点图上叠加线函数 - seaborn

python - 在 matplotlib 中使用 bbox_inches ='tight' 时,如何获得具有精确大小(以像素为单位)的图像?