gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:07:55 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com