简体   繁体   中英

Pandas and matplotlib - plot scatter on line graph

I'm building a trading bot and currently backtesting. I have two pandas DataFrames, one much longer than the other. The longer contains all the dates and all the indexes for x amount of years. The other data frame only contain the dates and indexes of which I bought or sold.

Long_frame = {'date':['2020-01-10', '2020-01-11', '2020-01-12', '2020-01-13', '2020-01-14', '2020-01-15'], 'index': [2, 4, 6, 8, 10, 20]}
Short_frame = {'date':['2020-01-10', '2020-01-11', '2020-01-13', '2020-01-15'], 'index': [2, 4, 8, 20]}

When I try to plot this on the same graph the line or scatter of the short list end up really compact in the beginning of the graph. How should I plot this to get a meaningful graph? The optimal graph would be to only have one line, the long one, and plot points on that line where trades occurred.

Thanks!

You can use the dates for your x-axis:

Long_frame = {'date':['2020-01-10', '2020-01-11', '2020-01-12', '2020-01-13', '2020-01-14', '2020-01-15'],'index': [2, 4, 6, 8, 10, 20]}
Short_frame = {'date':['2020-01-10', '2020-01-11', '2020-01-13', '2020-01-15'], 'index': [2, 4, 8, 20]}


import matplotlib.pyplot as plt

plt.plot(Long_frame['date'], Long_frame['index'])
plt.plot(Short_frame['date'], Short_frame['index'],'.', markersize=15)

The trade date points are located at the date position and both graphs are 'equally distributed'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM