简体   繁体   English

如何根据 Pandas Dataframe 中的列值对 Plot 上的特定数据点进行着色

[英]How to Color Specific Data Points on a Plot Based on Column Value in Pandas Dataframe

Let's say I have the following dataset:假设我有以下数据集:

d = {'Team': ['Duke', 'LSU'], 'Wins': [20, 18], 'Losses' : [5, 7], 'Conference' : ['ACC', 'SEC']}
df = pd.DataFrame(data=d)
df

    Team    Wins   Losses   Conference
0   Duke     20      5          ACC
1   LSU      18      7          SEC

Then I make a scatterplot of it然后我做了一个散点图

plt.plot(d['Losses'], d['Wins'], 'o')

I would like to color code my scatter plot by Conference.我想通过会议对我的散布 plot 进行颜色编码。 More specifically, I would like to only color SEC teams red, while all other data points are the default blue.更具体地说,我只想将 SEC 团队涂成红色,而所有其他数据点都是默认的蓝色。 Additionally, how would I go about coloring just Duke red, while every other datapoint is blue?此外,我将如何 go将杜克红色着色,而其他所有数据点都是蓝色? I have about 200 teams in my dataset.我的数据集中有大约 200 个团队。 How would I ago about doing this?我怎么会这样做呢? Thanks!谢谢!

IIUC you can try IIUC你可以试试

import pandas as pd
import numpy as np

d = {'Team': ['Duke', 'LSU'], 
     'Wins': [20, 18], 
     'Losses' : [5, 7], 
     'Conference' : ['ACC', 'SEC']}
df = pd.DataFrame(data=d)

df["color"] = np.where(df["Conference"]=="SEC", "red", "blue")

df.plot(x='Losses', y='Wins', kind="scatter", color=df["color"]);

If then you want to use the same logic for Duke you just need to change the line with np.where accordingly.如果你想对 Duke 使用相同的逻辑,你只需要相应地用np.where更改行。

Update For this particular case I think you should have a look at plotly更新对于这种特殊情况,我认为您应该看看plotly

import plotly.express as px
px.scatter(df,x="Losses", y="Wins", color="Conference", hover_name="Team")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何 pandas dataframe 的 plot 特定列? - How to plot specific column of pandas dataframe? 如何根据特定列中的数值分解 pandas dataframe - How to explode pandas dataframe based on number value in a specific column 从pandas DataFrame绘制数据,依赖于列的点的颜色 - Plot data from pandas DataFrame, colour of points dependant on a column 如何在带有误差线的条形图中绘制熊猫数据框的特定行和列(基于行名和列名)? - How to plot specific rows and columns of pandas dataframe (based on name of row and name of column) in bar plot with error bars? 根据值对熊猫数据框中的数据项进行颜色设置 - Color data items in a pandas dataframe based on value 基于第三列中的值的颜色散点图? - Color scatter plot points based on a value in third column? 如何将 plot 和 pandas dataframe 列作为数据,另一列作为颜色编号? - How to plot a pandas dataframe with one column as data and the other column as color number? Pandas 条形图 plot 根据值,每列颜色不同 - Pandas bar graph plot with different color for each column based on value 如何根据第三列值绘制带有颜色的2D数据点 - How to plot 2D data points with color according to third column value 如何基于Pandas中特定值的一列转换仅在一列中仅具有唯一值的DataFrame - How to convert a DataFrame that only has unique value in one column based on one column in specific value in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM