我试图创建一个散点图,其中标记和标记本身的大小根据列数据变化:
#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'])
这种将列分配给函数的参数的方法非常有效,直到我添加了标记列
TypeError: unhashable type: 'Series'
我想知道为什么它在这个实例中不能工作,以及如何更改它,以便根据列数据得到一个特定的标记
发布于 2022-07-17 02:48:21
散点图中的标记必须是唯一的,这样才能通过提取和绘制标记单元来实现数据帧。创建示例数据并绘制图表。参见正式参考中的示例。
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()
https://stackoverflow.com/questions/73007582
复制相似问题