-1

This is my 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.

So, the plot marker should be like this: o x x x o x o

But have no idea how to do this...

Need your helps. Thanks.

2 Answers 2

0

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:

enter image description here

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:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

You can plot the filtered dataframe. I.e., you can create two dataframes, one for action 0 and one for action 1. Then plot each individually.

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)

enter image description here

2 Comments

Thanks but I want to add marker on line plot. How can I do that?
A line plot can be done via .plot(kind="line", ...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.