Assuming df['time']
is from type timedelta64[ns]
and df['a']
as well as df['b']
are from type float64
, the two series can be plotted like this:
import pandas as pd
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, sharex=True)
time_values = pd.to_datetime(df['time'])
axs[0].plot(time_values, df['a'])
axs[1].plot(time_values, df['b'])
plt.show()
This works.. But gives the following Warning:
FutureWarning: Passing timedelta64-dtype data is deprecated, will raise a TypeError in a future version
So what should be used instead of pd.to_datetime
to display timedelta64[ns]
as human-readable time in matplotlib
?
I faced the same issue when reading time data from SQL using pandas. These 2 lines solved my problem. I tried to find another direct solution without success.
time_values = df['time'].apply(lambda x: np.nan if pd.isnull(x) else str(x)[-8:])
time_values = pd.to_datetime(time_values, format='%H:%M:%S').dt.time
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With