I have two different dataframes. First one is High/Lows of a selected ticker from a selected time interval. Second dataframe includes special 'signals' occured at the same time interval.
df:
Time High Low
0 29/03/22 16:00 47871.02 47105.50
1 29/03/22 20:00 47726.00 46950.85
2 30/03/22 00:00 47457.16 46445.42
3 30/03/22 04:00 47580.00 47140.46
4 30/03/22 08:00 47500.00 47064.81
5 30/03/22 12:00 47344.50 46869.00
6 30/03/22 16:00 47700.22 46831.81
7 30/03/22 20:00 47292.68 46850.00
8 31/03/22 00:00 47425.21 46973.91
9 31/03/22 04:00 47170.71 46898.12
10 31/03/22 08:00 47386.70 47000.00
11 31/03/22 12:00 47600.00 46230.00
12 31/03/22 16:00 46492.65 45500.00
13 31/03/22 20:00 45849.99 45200.00
14 01/04/22 00:00 45644.86 44200.00
15 01/04/22 04:00 45110.81 44547.11
16 01/04/22 08:00 45170.62 45008.24
dfs:
Time Frame Signal Price Time
0 4H SELL[37/75] 47440.0 29/03/22 16:00
1 4H BUY[43/75] 47701.0 29/03/22 20:00
2 4H BUY[24/75] 47435.0 30/03/22 04:00
3 4H SELL[38/75] 47384.0 30/03/22 08:00
4 4H SELL[38/75] 47074.0 30/03/22 20:00
5 4H BUY[44/75] 47161.0 31/03/22 08:00
6 4H SELL[31/75] 47152.0 31/03/22 12:00
7 4H SELL[51/75] 46454.0 31/03/22 16:00
So whenever i try to plot these two dataframes within the same plot:
It works fine. This is eaxactly what i want. Signal times matches with H/L times, so they blend to each other and even replacing duplicates on x axis. Because these are only Buy/Sell signals and they only occur at the close of each 4H candle etc.
But, whenever i change 'dfs' to this:
Time Frame Signal Price Time
0 4H RESISTANCE[35/75] 47150.0 30/03/22 18:58
1 4H SELL[38/75] 47074.0 30/03/22 20:00
2 4H SUPPORT[39/75] 47113.0 31/03/22 05:27
3 4H BUY[44/75] 47161.0 31/03/22 08:00
4 4H RESISTANCE[31/75] 47157.0 31/03/22 08:22
5 4H SELL[31/75] 47152.0 31/03/22 12:00
6 4H RESISTANCE[26/75] 47132.0 31/03/22 13:18
7 4H SELL[51/75] 46454.0 31/03/22 16:00
the x axis is not in order.
fig, ax = plt.subplots(figsize=(17.6, 9.6))
ax.plot(df['Time'], df['High'], marker='v', mec='blue',mfc='blue', ms=5, color = 'blue')
ax.plot(df['Time'], df['Low'], marker='v', mec='red',mfc='red', ms=5, color = 'red')
ax.tick_params(axis='x')
ax.scatter(dfs['Time'], dfs['Price'], color='black')
plt.xticks(rotation=45)
plt.show()
Tried to concat and merge two dataframes. Also tried to plot secondary x axis. no luck. plotting with pandas plot doesnt solve the issue either.
I need a simple trick to solve this issue.
Thanks in advance.
CodePudding user response:
The problem comes from the combination of two facts:
- the
Time
column of the modified "dfs" contains dates that are not present in "df", contrary to the original "dfs"; - you are using string values for your
Time
columns, instead of datetime objects.
Before plotting, add these two lines of code:
df["Time"] = pd.to_datetime(df["Time"], format="%d/%m/%y %H:%M")
dfs["Time"] = pd.to_datetime(dfs["Time"], format="%d/%m/%y %H:%M")
And then, with "df" and modified "dfs", the plot is correct: