如何绘制 Python 中的元组列表并将 x 轴更改为另一个数组?

How do I plot list of tuples in Python and change x axis into another array?

这是我的数据:

('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')

x代表时间,y代表客户,使用这段代码,我生成了一张图片:

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()

但是我想把x轴改成从2022-04-2300:00到2022-04-2324:00的时间跨度,间隔是5分钟,我怎么想这样做?

注意:'2022-04-23 00:00'和'2022-04-23 24:00'是同一个日期,所以你的x轴可以是'2022-04-23 00:00' 到 '2022-04-24 00:00'。

你可以这样获取x轴:

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]