首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >基于列数据的变化散射标记

基于列数据的变化散射标记
EN

Stack Overflow用户
提问于 2022-07-16 20:50:02
回答 1查看 232关注 0票数 0

我试图创建一个散点图,其中标记和标记本身的大小根据列数据变化:

代码语言:javascript
运行
复制
#Get the different type of rated episodes and color
office.loc[office['scaled_ratings'] < 0.25, 'mcolor'] = 'red'
office.loc[(office['scaled_ratings'] >= 0.25) & (office['scaled_ratings'] < 0.50), 'mcolor'] = 'orange'
office.loc[(office['scaled_ratings'] >= 0.50) & (office['scaled_ratings'] < 0.75), 'mcolor'] = 'lightgreen'
office.loc[office['scaled_ratings'] >= 0.75, 'mcolor'] = 'darkgreen'

#Get episodes with guests and the marker size
office.loc[office['has_guests']== False, 'size'] = 25
office.loc[office['has_guests']== True, 'size'] = 250

#Set marker for episodes
office.loc[office['has_guests']== True, 'marker'] = '*'
office.loc[office['has_guests']== False,'marker'] = 'o'


#Create plot
plt.scatter(office['episode_number'], office['viewership_mil'], c = office['mcolor'], s= office['size'], marker= office['marker'])

这种将列分配给函数的参数的方法非常有效,直到我添加了标记列

代码语言:javascript
运行
复制
TypeError: unhashable type: 'Series'

我想知道为什么它在这个实例中不能工作,以及如何更改它,以便根据列数据得到一个特定的标记

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-17 02:48:21

散点图中的标记必须是唯一的,这样才能通过提取和绘制标记单元来实现数据帧。创建示例数据并绘制图表。参见正式参考中的示例。

代码语言:javascript
运行
复制
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame({'x': np.random.rand(10),
                   'y': np.random.rand(10),
                   'colors': np.random.choice(['C0','C1','C2'], size=10),
                   'sizes': np.random.randint(20,70,10),
                   'symbols': np.random.choice(['*','o','^'], size=10)})

fig, ax = plt.subplots()

for m in df['symbols'].unique():
    dff = df[df['symbols'] == m]
    ax.scatter(dff['x'], dff['y'], c=dff['colors'], s=dff['sizes'], marker=dff['symbols'].unique()[0])
plt.show()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73007582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档