Home > other >  Plotting 'time' vs 'date' in Matplotlib
Plotting 'time' vs 'date' in Matplotlib

Time:11-19

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:

  1. how to handle datetime objects using matplotlib tools?
  2. how to set ticks and tickarks correctly when dealing with datetime objects in matplotlib?

CodePudding user response:

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)

enter image description here

CodePudding user response:

You can convert your time objects to datetime by combining with a suitable dummy date, and then plot:

from datetime import date
from datetime import datetime

value = [datetime.combine(date.today(), value) for v in value]
scatter(x, value)

matplotlib doesn't do this for you because it has no way to know what you want the start date to be.

If you don't want the date showing up in the y-axis, then set the date formatter: https://matplotlib.org/stable/api/dates_api.html

  • Related