Issue
I have a dictionary with dates (year, month, day) and time stamps (hour, minute, second) that look like this:
dict = {datetime.date(2021, 11, 1): [datetime.time(7, 43, 23)],
datetime.date(2021, 11, 8): [datetime.time(6, 4, 45),
datetime.time(6, 3, 36),
datetime.time(5, 56, 26)],
datetime.date(2021, 11, 12): [datetime.time(8, 5, 11),
datetime.time(5, 44, 45),
datetime.time(5, 24, 14),
datetime.time(4, 56, 5)]}
I would like to make a scatterplot out of it. First I wrote this code:
from matplotlib import pyplot as plt
for key, value in dict.items():
x = [key]*len(value)
y = value
plt.scatter(x, y)
plt.tight_layout()
plt.show()
This code produced an error:
TypeError: float() argument must be a string or a number, not 'datetime.time'
Then I tried to convert datetime using matplotlib.dates
changed my code to this:
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
for key, value in dict.items():
x = [mdates.date2num(key)]*len(value)
y = [mdates.date2num(y) for y in value]
plt.scatter(x, y)
plt.tight_layout()
plt.show()
This code produced another error:
ValueError: Could not convert object to NumPy datetime
At first, the problem looked really simple. However, now I do not understand how to approach it using matplotlib.
My questions:
- how to handle datetime objects using matplotlib tools?
- how to set ticks and tickarks correctly when dealing with datetime objects in matplotlib?
Solution
You can use pandas' datetime features:
import pandas as pd
arr = []
for key, value in dict.items():
for time in value:
arr.append([key, time])
df = pd.DataFrame(arr, columns=['day', 'time'])
df.day = pd.to_datetime(df.day)
df.time = pd.to_datetime(df.time.astype('str'))
plt.scatter(df.day, df.time)
Answered By - Simone
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.