I am plotting a graph with datetime
and power
which can be seen in the big blue line on the attached image.
After plotting the image, I am trying to insert the 1-d list (only_unusual
) in the same plot.
Actually df['power']
includes the values of the 1-d list (only_unusual
).
But without including y axis alongside with only_unusual
I can't plot the graph.
How can I plot the only_unusual
list in the same plot so that it will be visible on the main blue line with a different color?
My code:
fig, ax = plt.subplots(figsize=(16,12))
ax.plot(pd.to_datetime(df['datetime']), df['power'], color='b',label='Normal')
ax.scatter(only_unusual , color='red', label='Unusual',marker='o')
ax.xaxis_date()
plt.xlabel('Date Time')
plt.ylabel('power')
plt.legend()
fig.autofmt_xdate()
plt.show()
My data:
only_unusual : [13.266, 4.213291, 2.756, 3.6722, 12.356, 12.193, 10.318, 12.203, 8.7549, 9.536, 9.10677, 1.417]
df :
datetime invno power
0 2021-12-01 00:00:00 1 0.000
5 2021-12-01 01:00:00 1 0.000
10 2021-12-01 02:00:00 1 0.000
15 2021-12-01 03:00:00 1 0.000
20 2021-12-01 04:00:00 1 0.000
.... ...... ...... .... .. .....
1129 2021-12-10 09:00:00 5 2.914
1134 2021-12-10 10:00:00 5 10.318
... ... ... ...
1149 2021-12-10 13:00:00 5 2.756
1154 2021-12-10 14:00:00 5 1.297
1159 2021-12-10 15:00:00 5 1.503
1164 2021-12-10 16:00:00 5 1.417
1169 2021-12-10 17:00:00 5 0.084
1170 rows × 3 columns
CodePudding user response:
If the list contains y values (power
)
Normally we could find matching y values with
If the list contains x values (datetime
)
We can just use
isin
to find matchingdatetime
rows:matches = df[df['datetime'].isin(only_unusual)]
Then use these
matches
(instead ofdf
) for the scatter plot:ax.scatter(pd.to_datetime(matches['datetime']), matches['power'], color='r', label='Unusual', marker='o') # ^^^^^^^ ^^^^^^^