Python 如何绘制熊猫特定颜色的散点图?

Python 如何绘制熊猫特定颜色的散点图?,python,pandas,matplotlib,Python,Pandas,Matplotlib,我通过一步一步地阅读别人的代码来学习python数据分析。 我有一个名为df的数据帧,如下所示: 我想画一个散点图,其中,total匹配作为x轴,win rate作为y轴,每个点在color map中用相应的颜色着色 这样(不考虑名字描述): 代码是: df.plot(kind='scatter', x='total matches', y='win rate', color=df['color map'].tolist(), figsize=(15,10), title='win rate

我通过一步一步地阅读别人的代码来学习python数据分析。 我有一个名为df的数据帧,如下所示:

我想画一个散点图,其中,
total匹配
作为x轴,
win rate
作为y轴,每个点在
color map
中用相应的颜色着色

这样(不考虑名字描述):

代码是:

df.plot(kind='scatter', x='total matches', y='win rate', color=df['color map'].tolist(), figsize=(15,10), title='win rate vs # matches by champions')
但此raise
ValueError:无法将字符串转换为浮点:“绿色”

如何修复它?

此参数语法似乎有效

给定的
df

     name  total_matches  win_rate color_map
0   Ivern           6671     55.87     green
1  Anivia           6433     53.97       red
2  Xerath           5108     53.62      blue
3    Ahri          30841     53.55    yellow
4    Sona          11847     53.14     black
使用:


此代码将获得您想要的内容,而无需将整个数据帧转换为字典。但是,我必须将每个Pandas列转换为列表,以便将名称标签添加到每个点,但是如果没有名称标签,您可以直接使用数据帧

# All the necessary imports
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style

# There are several style templates in Matplotlib, this one seems to fit your look
style.use('fivethirtyeight')

# Reduce font size to reduce clutter
plt.rcParams['font.size'] = 6.0
fig, ax = plt.subplots()

# Create list from each dataframe column
x = df['total matches'].tolist()
y = df['win rate'].tolist()
colors = df['color map'].tolist()
names = df['name'].tolist()

# Create scatter plot, c property takes list of colors
ax.scatter(x, y, c=colors)

# Iterate through names to add label to each point
for i, txt in enumerate(names):
    ax.annotate(txt, (x[i],y[i]))

# Show the chart, matplotlib can save the chart in several formats as well
plt.show()

这应该归功于我的熊猫版本。当我更新我的熊猫时,从
0.19.2
0.21.1
。错误消失了。

请将您的df.to_dict()而不是图片发布。我没有任何
ValueError
。我的
pd.\uuuuu版本是
0.21.1
@user32185哇!我把我的熊猫从0.19.2升级到了0.21.1,效果很好。非常感谢!:)
df.plot(kind='scatter', x='total_matches', 
        y='win_rate', c=df.color_map, s=80)
# All the necessary imports
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style

# There are several style templates in Matplotlib, this one seems to fit your look
style.use('fivethirtyeight')

# Reduce font size to reduce clutter
plt.rcParams['font.size'] = 6.0
fig, ax = plt.subplots()

# Create list from each dataframe column
x = df['total matches'].tolist()
y = df['win rate'].tolist()
colors = df['color map'].tolist()
names = df['name'].tolist()

# Create scatter plot, c property takes list of colors
ax.scatter(x, y, c=colors)

# Iterate through names to add label to each point
for i, txt in enumerate(names):
    ax.annotate(txt, (x[i],y[i]))

# Show the chart, matplotlib can save the chart in several formats as well
plt.show()