0

I have a dataframe(first few rows):

enter image description here

I can plot it with matplotlib.pyplot:

fig = plt.figure()
ax1 = fig.add_subplot(111,  ylabel='Price')

df1[['Close']].plot(ax=ax1)

To get:

enter image description here

What I would like to do is to add a marker to plot, down triangle, at the index 2018-09-10 04:00:00 which is indicated by the value -1 in the position column of the dataframe.

I tried to do this:

fig = plt.figure()
ax1 = fig.add_subplot(111,  ylabel='Price')

df1[['Close']].plot(ax=ax1)
ax1.plot(
    df1.loc[df1.positions == -1.0].index,
    df1.Close[df1.positions == -1.0],
    'v', markersize=5, color='k'
)

I get the plot like this:

enter image description here

So two things. One is that the index gets converted to something that shoots to year 2055, I don't understand why. Plus is there a way to add a marker at the specific position using just the first plot call? I tried to use markevery but with no success.

1 Answer 1

2

If you want to combine pandas plots and matplotlib datetime plots, the pandas plot needs to be plotted in compatibility mode

df1['Close'].plot(ax=ax1, x_compat=True)

That might give you the desired plot already.

If you don't want to use matplotlib, you can plot the filtered dataframe

df1['Close'].plot(ax=ax1)
df1['Close'][df1.positions == -1.0].plot(ax=ax1, marker="v", markersize=5, color='k')
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect. Thank you. Both suggestions are working. I will use the x_compat as it is just creating the markers. I didn't realise that pandas and matplotlib plots are different.
Well the plots are of course not different. But pandas may decide to use different units to plot dates.

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.