Plot data from pandas DataFrame, colour of points dependant on a column

蓝咒 提交于 2020-05-09 06:09:32

问题


I have a pandas DataFrame with 3 columns, shown below.

col1 value flag 1 0 0 2 0.03915 0 3 0.13 1

I want to create a scatterplot from this dataframe where col1 is the x axis and value is the y axis, and the part I'm struggling to do is, for the rows that have flag=0 I want the color of this point to be blue and similarly if flag=1 I want to color the point red.

Is there a simple to to check the flag column per row and color the point accordingly?


回答1:


You can use the built-in df.plot.scatter() function and pass the flag column to the color argument:

import pandas as pd

idx = [1,2,3]
value = [0., 0.03, 0.13]
flag = [0, 0, 1]

df = pd.DataFrame(dict(idx=idx, value=value, flag=flag))

df.plot.scatter('idx', 'value', c='flag', cmap='RdBu_r')




回答2:


Assuming the dataframe containing the given data is df, this is what you want. You can create a list of colors according to your condition flag column and its values. Feed that colors list to color argument in the built-in DataFrame.plot.scatter function. (You can find the docs here.)

colors = ['r' if flag==1 else 'b' for flag in df.flag]
df.plot.scatter('col1', 'value', color=colors)

Hope this helps.




回答3:


You can pass a vector to the scatter plot as follows

import matplotlib.pyplot as plt

df = pd.DataFrame(data = {'value':[0, 0.4, 0.13], 'flag':[0,0,1]})
plt.scatter(df['value'], df.index, c=df['flag'])



来源:https://stackoverflow.com/questions/49684037/plot-data-from-pandas-dataframe-colour-of-points-dependant-on-a-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!