0

I have loaded some stock price data into a dataframe. I want to quickly plot the close price on Y axis, and date on the X axis.

This is what my dataframe looks like:

    Open    High    Low Close   Adjusted_close  Volume
Date                        
1980-12-11  22.000  22.000  22.000  22.000  0.0308  0
1980-12-12  28.750  28.876  28.750  28.750  0.0395  2093900
1980-12-15  27.250  27.376  27.250  27.250  0.0375  785200
1980-12-16  25.250  25.376  25.250  25.250  0.0350  472000
1980-12-17  25.876  26.000  25.876  25.876  0.0359  385900

When I type df.plot() it plots something that looksl like a histogram.

When I type df.plot('Close') it plots a bunch of squiggly lines.

I have two questions:

  1. How do I quickly plot close price versus date
  2. Assuming I have two other columns ('Buy' and 'Sell') which are boolean flags, in the dataframe, how can I plot the 'Buy' and 'Sell' points using say a green up arrow and a red down arrow on the same plot ?

2 Answers 2

1

I just tried with the same data. I had to draw 02 plots in the same figure and had to add colors based on the new column as shown.


import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

# data wrangling
columns = ['date', 'open', 'high', 'low', 'close', 'adjusted_close', 'volume']
df = pd.read_csv('timeseries.csv', parse_dates=True, names=columns)
#df['date'] = pd.to_datetime(df['date'])
signals = [True, True, False, False, True]
df['signals'] = signals

# plots
plt.plot(df['date'], df['close'])
kcolors = ['green' if s else 'red' for s in df['signals']]
plt.scatter(df['date'], df['close'], c=kcolors)
#rotate label if too long
plt.xticks(rotation=60)
plt.show()

enter image description here

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

2 Comments

Hmm, I just double checked your code. It is not doing exactly what I want to do. Here is the problem, your code plots every single data point as a signal. In practise, some date points will not have a signal - that is why I have two columns 'Buy' and 'Sell'. It is only when the Flag in a column is set to True, that I want to plot the relevant symbol (e.g. green up arrow for when the flag is True in the 'Buy' column, or red down arrow for when the flag is True in the 'Sell' column).
@HomunculusReticulli, I have hardcoded the signals as True or False. I agree that in practice, we populate the signals using one or the other approach. Having two columns 'Buy' and 'Sell' is also a great approach. In that case, we will have 03 plots in a single figure ( please correct me if I am wrong). May I know what You are using to show arrows?
1

What you described works perfectly for me.

import pandas as pd
df = pd.read_clipboard()
df['Close'].plot()

enter image description here Are you sure you converted the index to pandas datetime? if not try

df.index = pd.to_datetime(df.index)

1 Comment

Ah I see what I was doing wrong. I was doing df.plot('Close') instead of df['Close'].plot(). What about teh second part of my question? Plotting a symbol (e.g. green/red arrow) at the price/date coordinate where a flag is True?

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.