0

Here is my data:

('2022-04-23 14:51', 'customer1', 50, 'red')
('2022-04-23 16:19', 'customer2', 50, 'red')
('2022-04-23 16:20', 'customer2', 50, 'red')
('2022-04-23 16:34', 'customer3', 50, 'red')
('2022-04-23 17:25', 'customer4', 50, 'red')
('2022-04-23 17:37', 'customer5', 50, 'red')
('2022-04-23 18:29', 'customer6', 50, 'red')
('2022-04-23 18:33', 'customer7', 50, 'red')

The x represents time, y represents customer, using this code, I have generated a picture:

import matplotlib.pyplot as plt

plt.scatter(*zip(*qr_dict)) # this is a list of tuples
plt.xlabel('time')
plt.ylabel('customer')
plt.xticks(rotation=270)
plt.subplots_adjust(bottom=0.55)
plt.grid()
plt.show()

However, I want to change x axis into a time span from 2022-04-23 00:00 to 2022-04-23 24:00, and interval is 5 minute, how am I suppose to do that?

1 Answer 1

2

Note: '2022-04-23 00:00' and '2022-04-23 24:00' are the same date, so your x axis can be from '2022-04-23 00:00' to '2022-04-24 00:00'.

You can obtain the x axis in this way:

from datetime import datetime, timedelta
import numpy as np

x_start = datetime.strptime('2022-04-23 00:00', "%Y-%m-%d %H:%M")
x_end = datetime.strptime('2022-04-24 00:05', "%Y-%m-%d %H:%M")

timestamps = np.arange(x_start, x_end, timedelta(minutes=5), dtype=datetime)

x_axis = [timestamp.strftime("%Y-%m-%d %H:%M") for timestamp in timestamps]
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much, I've tried this, but it's not what I want. I want to reset the x axis like you wrote, and at the same time, I need to show these data in picture
You have to set plt.xticks to show the labels of the x axis
Actually, I did use plt.xticks to show the labels of the x axis, but it didn't show the time interval on x axis
I've solved this, nicc96 was right, the reason that I didn't get what I want is that I need to cluster my data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.