mplfinance中是否有一个等价的plt.scatter?如何在mplfinance中绘制数据点图? [英] is there an equivalent of plt.scatter in mplfinance? How to you graph data points in mplfinance?

查看:281
本文介绍了mplfinance中是否有一个等价的plt.scatter?如何在mplfinance中绘制数据点图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mplfance中的plt.scatter等效于什么?

我正在使用mpl金融绘制股价图.

  def graph():文件='prices1.xlsx'数据= pd.read_excel(文件,工作表名称= stockQuote)data.Date = pd.to_datetime(data.Date)数据= data.set_index('日期')mpf.plot(数据,类型='蜡烛',mav =(100),tight_layout = True) 

  graph('AAPL') 

应该让我得到烛台中AAPL价格的图表.
我还有另一个具有买卖价格的Excel表格.看起来像这样

  myPrices = pd.read_excel('transactions.xlsx') 

<身体>
日期符号动作价格
2020-03-20 AAPL 购买 80
2021-03-05 AAPL 出售 120
2020-03-20 TSLA 购买 400

我知道matplotlib具备以下功能:

  plt.scatter(myPrices.index,myPrices ['Buy'],label ='Buy',market ='^',color ='green')plt.scatter(myPrices.index,myPrices ['Sell'],label ='Sell',market ='v',color ='red') 

因为我想绘制'AAPL'图表,所以我想读取transaction.xlsx中的日期,其中Symbol ='AAPL'.我想在购买时在MPLFINANCE GRAPH中用绿色箭头^表示,在出售时用红色箭头v表示.但是,我只在matplotlib中知道此方法.是否有mplfinance的对等物?请帮助T-T

解决方案

mplfinace中的散点图不能单独使用,但可以与烛台结合使用.您的数据将修改为基于月度的数据,并用作示例数据.关于数据要注意的一件事是时间序列数据的长度必须相同,否则会发生错误.此页面是很好的

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_name = stockQuote)
    data.Date = pd.to_datetime(data.Date)
    data = data.set_index('Date')
    mpf.plot(data, type = 'candle', mav = (100), tight_layout = True)

This

graph('AAPL')

should get me the graph of AAPL prices in candlestick.
I have another excel sheet with buy and sell prices. it looks like this

myPrices = pd.read_excel('transactions.xlsx')

Date Symbol Action Price
2020-03-20 AAPL Buy 80
2021-03-05 AAPL Sell 120
2020-03-20 TSLA Buy 400

I know that matplotlib has this:

plt.scatter(myPrices.index, myPrices['Buy'], label = 'Buy', market = '^', color = 'green')
plt.scatter(myPrices.index, myPrices['Sell'], label = 'Sell', market = 'v', color = 'red')

Because I want to graph 'AAPL', I want to read the date in transaction.xlsx where Symbol = 'AAPL'. I want to indicate with a green arrow ^ in the MPLFINANCE GRAPH when I buy it, and red arrow v when I sell. However, I only know this method in matplotlib. is there an equivalent for mplfinance? PLEASE HELP T-T

解决方案

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)

这篇关于mplfinance中是否有一个等价的plt.scatter?如何在mplfinance中绘制数据点图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆