highfestiva / finplot

Performant and effortless finance plotting for Python
MIT License
888 stars 179 forks source link

Two axes with different intervals #519

Open StephanHQ opened 2 months ago

StephanHQ commented 2 months ago

Hello,

my question is, how can finplot plot two different df which both have different intervals. Also the second df has no real interval, but sporadic values. I managed to plot both, but both ax do not align. The second df is currently being plotted with equal x spacing between rows, although the timestamp significantly varies between each one.

Bildschirmfoto 2024-05-07 um 15 36 24
highfestiva commented 2 months ago

You can fill in null data in the missing time intervals on the second dataframe, then you should be able to display them in the same axis.

StephanHQ commented 2 months ago

Thank your for your lightning speed reply. I tried that before and i receive "Segmentation fault: 11". I uploaded the used df just in case. this function is used when the errors occurs: fplt.plot(df['pivot_point'], ax=ax, style='o-', color='#ff00ff') ofc using this function fplt.plot(df['pivot_point'].dropna(), ax=ax, style='o-', color='#ff00ff') doesnt lead to an error.

df.csv

highfestiva commented 2 months ago

There is no timestamp on your data. Is this intended? The pivot_point is all null, so .dropna() is going to lead to an empty sequence.

StephanHQ commented 2 months ago

my bad. the df has been indexed after timestamp. df.to_csv doesnt inherit timestamp indexing. I adjusted to normal indexing and inserted timestamp as a column (see dv.csv). dv.csv Im using this plot function now: fplt.plot(df['timestamp'],df['pivot_point'], ax=ax, style='o-', color='#ff00ff') However, it still leads to the error: "Segmentation fault: 11".

highfestiva commented 2 months ago

That sounds like I'm supplying Qt with some bad data, or possibly a bug in Qt. Any chance you could create a minimal example with only the plot call and only the data causing the error?

StephanHQ commented 2 months ago

I can not currently recreate the error in a single file. I will take some more time to do that. However, going to my main question, how can we align both plots on timestamp x? I provided the csv and the py to show you.

data.csv

Code start

import finplot as fplt import pandas as pd

data = pd.read_csv('data.csv', parse_dates=['timestamp']) data.set_index('timestamp', inplace=True) ax, ax2 = fplt.create_plot('Stock Price with Zigzag Indicator', rows=2)

fplt.candlestick_ochl(data[['open', 'close', 'high', 'low']], ax=ax) zigzag_points = data['pivot_point'].dropna() fplt.plot(zigzag_points.index, zigzag_points.values, ax=ax2, color='#0000ff', width=1.5)

fplt.show()

highfestiva commented 2 months ago

Replace .dropna() with .interpolate()?