Skip to content Skip to sidebar Skip to footer

Is There An Equivalent Of Plt.scatter In Mplfinance? How To You Graph Data Points In Mplfinance?

What is the equivalent of plt.scatter in mplfinance??? I am graphing stock prices using mpl finance. def graph(): file = 'prices1.xlsx' data = pd.read_excel(file, sheet_nam

Solution 1:

Scatter plots in mplfinace cannot be used alone, but can be used in combination with candlesticks. Your data is modified to monthly based data and used as sample data. One thing to note on the data is that the length of the time series data must be the same or an error will occur. This page is a good reference.

import pandas as pd
import numpy as np
import io

data = '''
Date Symbol Action Price
2020-03-01 AAPL Buy 80
2020-04-01 AAPL Sell 130
2020-05-01 AAPL Buy 90
2020-06-01 AAPL Sell 125
2020-07-01 AAPL Buy 125
2020-08-01 AAPL Sell 110
2020-09-01 AAPL Buy 95
2020-10-01 AAPL Sell 125
2020-11-01 AAPL Buy 125
2020-12-01 AAPL Sell 140
2021-01-01 AAPL Buy 115
2021-02-01 AAPL Sell 135
'''

df = pd.read_csv(io.StringIO(data), delim_whitespace=True)

df['Date'] = pd.to_datetime(df['Date'])

buy = df[df['Action'] == 'Buy']
buy2 = df[['Date']].merge(buy,how='outer')
sell = df[df['Action'] == 'Sell']
sell2 = df[['Date']].merge(sell,how='outer')

import mplfinance as mpf
import yfinance as yf

data = yf.download("AAPL", interval='1mo', start="2020-03-01", end="2021-03-01")
data.dropna(how='any', inplace=True)

ap = [mpf.make_addplot(buy2['Price'], type='scatter', marker='^', markersize=200, color='g'),
      mpf.make_addplot(sell2['Price'], type='scatter', marker='v', markersize=200, color='r')
     ]
      
mpf.plot(data, type='candle', ylabel='Candle', addplot=ap, volume=False)

enter image description here

Post a Comment for "Is There An Equivalent Of Plt.scatter In Mplfinance? How To You Graph Data Points In Mplfinance?"