使用不同的标记颜色基于属性绘制图表。

huangapple go评论58阅读模式
英文:

Plotting a graph with different marker colors based on attributes

问题

我有一个数据框,看起来像这样:

纬度 经度 邮政编码
0 40.244350 -8.443650 3025-151
2 40.626130 -8.645250 3810-419
1 40.626130 -8.645250 3810-498
3 40.626130 -8.645250 3810-419

我想绘制一个图表,使用纬度和经度坐标(线图),其中标记的面颜色基于邮政编码标签而不同。

我想到了以下解决方案:

color_labels = df['cod_postal'].unique()
        
# RGB颜色列表
rgb_values = sns.color_palette('Set2', 8)
    
# 将标签分配给RGB代码
color_map = dict(zip(color_labels, rgb_values))
    
plt.plot(df['latitude'], df['longitude'], marker='o', markerfacecolor=df['cod_postal'].map(color_map))

但是我遇到了以下错误:

ValueError: RGBA sequence should have length 3 or 4

如何修复这个问题?
我知道如果我绘制散点图,它会起作用,但对于我的情况,绘制线图是必不可少的。

提前致谢!

编辑:

为我的期望输出添加了一张参考图片。

英文:

I have a df that looks like this:

latitude longitude cod_postal
0 40.244350 -8.443650 3025-151
2 40.626130 -8.645250 3810-419
1 40.626130 -8.645250 3810-498
3 40.626130 -8.645250 3810-419

I want to plot a graph with the lat and lon coordinates (a line plot) where the marker face color is different based on the cod_postal label.

I have come up with this:

color_labels=df['cod_postal'].unique()
    
#list of rgb
rgb_values=sns.color_palette('Set2', 8)

#Assign label to a rgb code
color_map=dict(zip(color_labels, rgb_values))

plt.plot(df['latitude'], df['longitude'], marker='o', mfc=df['cod_postal'].map(color_map)) 

But I get an error saying:

ValueError: RGBA sequence should have length 3 or 4

How can I fix this?
I know that if I was plotting a scatter it would work, but for my case, being a line plot is essential.

Thanks in advance!

EDIT:

Adding a reference picture for my expected output.
使用不同的标记颜色基于属性绘制图表。

答案1

得分: 1

plt.plot() 期望 mfc 参数只有一个颜色。

使用 marker="o",看起来你试图创建一个散点图。如果是这种情况,你应该使用 plt.scatter。现在可以将多个颜色传递给 c= 参数(每个散点一个颜色)。

color_labels = df['cod_postal'].unique()

# RGB颜色列表
rgb_values = sns.color_palette('Set2', 8)

# 将标签分配给RGB代码
color_map = dict(zip(color_labels, rgb_values))

fig, ax = plt.subplots()
# 创建散点图,每个“cod_postal”使用一个颜色
plt.scatter(df['latitude'], df['longitude'], c=df['cod_postal'].map(color_map).values)

项1、2和3具有相同的坐标,因此它们在图上重叠。


关于提供的参考图像的编辑。

我建议您分两步进行。首先按照上述说明创建散点图,然后创建连接的黑线图。

plt.scatter(df['latitude'], df['longitude'], c=df['cod_postal'].map(color_map).values, zorder=3)
plt.plot(df['latitude'], df['longitude'], c="k")

我调整了数据帧以使图更有趣。

df = pd.DataFrame([
    [40.244350, -8.443650, "3025-151"],
    [41.244350, -9.443650, "3025-151"],
    [42.244350, -8.443650, "3025-151"],
    [40.626130, -8.645250, "3810-419"],
    [41.626130, -8.645250, "3810-419"],
], columns=["latitude", "longitude", "cod_postal"])

使用不同的标记颜色基于属性绘制图表。

英文:

plt.plot() is expecting a single color for the mfc parameter.

Using marker="o", it looks like you're trying to create a scatter plot. If it's the case, you should use plt.scatter. You can now pass multiple colors to the c= parameter (one for each scatter point).

color_labels=df['cod_postal'].unique()
    
#list of rgb
rgb_values=sns.color_palette('Set2', 8)

#Assign label to a rgb code
color_map=dict(zip(color_labels, rgb_values))

fig, ax = plt.subplots()
# Create scatter plot, with one color 'cod_postal'
plt.scatter(df['latitude'], df['longitude'], c=df['cod_postal'].map(color_map).values) 

使用不同的标记颜色基于属性绘制图表。

items 1, 2, and 3 have the same coordinates so they overlap on the figure.


Edit regarding the reference image provided.

I would then suggest you to do that in 2 steps. First create the scatter plot as explained above. Then create a line plot for the connecting black line.

plt.scatter(df['latitude'], df['longitude'], c=df['cod_postal'].map(color_map).values, zorder=3) 
plt.plot(df['latitude'], df['longitude'], c="k") 

I adjusted the dataframe to make the figure a bit more interesting

df = pd.DataFrame([
    [40.244350, -8.443650, "3025-151"],
    [41.244350, -9.443650, "3025-151"],
    [42.244350, -8.443650, "3025-151"],
    [40.626130, -8.645250, "3810-419"],
    [41.626130, -8.645250, "3810-419"],
], columns=["latitude", "longitude", "cod_postal"])

使用不同的标记颜色基于属性绘制图表。

huangapple
  • 本文由 发表于 2023年3月3日 19:19:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定