简体   繁体   English

熊猫:用标记绘制数据框对象?

[英]Pandas : plot data frame object with marker?

This is my pandas DataFrame. 这是我的pandas DataFrame。

   value  action
0      1       0
1      2       1
2      3       1
3      4       1
4      3       0
5      2       1
6      5       0

What I want to do is mark value as o if action=0 , x if action=1 . 我想做的是将value标记为o如果action=0 ,则x标记为action=1

So, the plot marker should be like this: oxxxoxo 因此,情节标记应如下所示: oxxxoxo

But have no idea how to do this... 但是不知道该怎么做...

Need your helps. 需要你的帮助。 Thanks. 谢谢。

You can plot the filtered dataframe. 您可以绘制过滤后的数据框。 Ie, you can create two dataframes, one for action 0 and one for action 1. Then plot each individually. 即,您可以创建两个数据框,一个用于动作0,一个用于动作1。然后分别绘制每个。

import pandas as pd

df = pd.DataFrame({"value":[1,2,3,4,3,2,5], "action":[0,1,1,1,0,1,0]})
df0 = df[df["action"] == 0]
df1 = df[df["action"] == 1]

ax = df.reset_index().plot(x = "index", y="value", legend=False, color="gray", zorder=0)
df0.reset_index().plot(x = "index", y="value",kind="scatter", marker="o", ax=ax)
df1.reset_index().plot(x = "index", y="value",kind="scatter",  marker="x", ax=ax)

在此处输入图片说明

Consider the following approach: 考虑以下方法:

plt.plot(df.index, df.value, '-X', markevery=df.index[df.action==1].tolist())
plt.plot(df.index, df.value, '-o', markevery=df.index[df.action==0].tolist())

Result: 结果:

在此处输入图片说明

alternative solution: 替代解决方案:

plt.plot(df.index, df.value, '-')
plt.scatter(df.index[df.action==0], df.loc[df.action==0, 'value'],
            marker='o', s=100, c='green')
plt.scatter(df.index[df.action==1], df.loc[df.action==1, 'value'], 
            marker='x', s=100, c='red')

Result: 结果:

在此处输入图片说明

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

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