简体   繁体   English

Python:如何在绘制散点图时区分类别值?

[英]Python: How to differentiate categorical values while plotting a scatter plot?

I have a dataframe that looks like this: 我有一个看起来像这样的dataframe

ContextID   EscAct_Curr_A   Outlier?
7289972 0.5622317497295798  True
7289973 0.5622317497295798  True
7289998 0.5793991301212501  False
7289999 0.5793991301212501  False
7290024 0.5665235864470339  False
7290025 0.5665235864470339  False

What I would like to do is to plot a scatter plot wherein the ContextID is in the x-axis and the EscAct_Curr_A is on the y-axis and based on the Outlier? 我想做的是绘制一个scatter plot其中ContextID在x轴上,而EscAct_Curr_A在y轴上并基于Outlier? column. 柱。 All those True must be of one colour and all those False must be of some other different colour. 所有那些True必须具有一种颜色,所有那些False必须具有某种其他颜色。

import pandas as pd
import matplotlib.pyplot as plt

d = {'ContextID': [7289972, 7289973, 7289998], 'EscAct_Curr_A': [0.5622317497295798, 0.5622317497295798, 0.5793991301212501], 'Outlier': [True, True, False]}

df = pd.DataFrame(d)

plt.scatter(df.ContextID, df.EscAct_Curr_A, c=df.Outlier)

I think the easiest way to understand how to do this would be to break the problem up and plot each piece for true/false outliers and bring them together. 我认为了解此操作的最简单方法是将问题分解并为正确/错误的异常值绘制每个图块并将它们组合在一起。 The below example gives you control and will show you how to control colors somewhat. 下面的示例为您提供控制,并将向您展示如何稍微控制颜色。

Frame was copied from your example. 框架是从您的示例中复制的。

frame.head()
Out:
   ContextID  EscAct_Curr_A  Outlier?
0    7289972       0.562232      True
1    7289973       0.562232      True
2    7289998       0.579399     False
3    7289999       0.579399     False
4    7290024       0.566524     False

# Create Objects
fig, ax= plt.subplots()

# Set Title
ax.set_title('Context ID vs EscAct_Curr_A, Color by Outliers')

# Scatter where outliers are true, color red
ax.scatter('ContextID', 'EscAct_Curr_A', c='r', data=frame.loc[frame.loc[:,'Outlier?']==True,:], label='Outliers True')

# Scatter where outliers are false, color blue
ax.scatter('ContextID', 'EscAct_Curr_A', c='b', data=frame.loc[frame.loc[:,'Outlier?']==False,:], label='Outliers Fasle')

# Set Labels On Axes
ax.set_xlabel('ContextID')
ax.set_ylabel('EscAct_Curr_A')

# Toggle Legend On
ax.legend()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM