拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 使用python在同一图形上的多个图,时间坐标

使用python在同一图形上的多个图,时间坐标

白鹭 - 2022-01-26 1996 0 0

我正在尝试使用 python 在同一个图形上绘制多个图。该图应该是线性的,x 坐标代表一天中的时间,y 坐标与值匹配。每个情节匹配不同的日期。

资料存盘在字典中。键代表日期,值包含 2 个串列:第一个匹配 x 坐标,第二个匹配 y 坐标。例如:

dict_data = {"4 April": [[datetime(1900, 1, 1, 22, 59), datetime(1900, 1, 1, 23, 1), datetime(1900, 1, 1, 23, 8), datetime(1900, 1, 1, 23, 50)], [405, 320, 300, 360]], "5 April": [[datetime(1900, 1, 1, 8, 10), datetime(1900, 1, 1, 9, 40), datetime(1900, 1, 1, 11, 8), datetime(1900, 1, 1, 11, 10)], [120, 20, 10, 0]]}

我在 Python 中使用 Matplotlib 发现堆栈溢位绘图时间的帖子。这没有帮助,因为他们创建的图表上的 x 轴是“datetime”型别,而我使用“datetime.time”datetime”。(我不希望 x 轴显示日期)。还有,他们创建的图表是散点图,而我需要它是线性的。

这是我尝试过的:

def multiple_plots(dict_data):
"""
Method to plot multiple times in one figure.
It receives a dictionary with the representation of the data in the csv file.
Every key in the dictionary represent a different date that will have its own plot ont the graph.
"""
for date, coordinates in dict_data.items():
    time_coordinates = coordinates[0]
    # converting the x coordinates in the type datetime.time to int
    x_coordinates = matplotlib.dates.date2num(time_coordinates)
    val_coordinates = coordinates[1]
    plt.plot(list(map(int, x_coordinates)), list(map(int, val_coordinates)), label=date)

plt.legend(loc='best')
plt.show()

uj5u.com热心网友回复:

这应该可以毫不费力地作业:

import matplotlib.pyplot as plt
import datetime

dict_data = {"4 April": [[datetime.datetime(1900, 1, 1, 22, 59), datetime.datetime(1900, 1, 1, 23, 1), datetime.datetime(1900, 1, 1, 23, 8), datetime.datetime(1900, 1, 1, 23, 50)], [405, 320, 300, 360]], "5 April": [[datetime.datetime(1900, 1, 1, 8, 10), datetime.datetime(1900, 1, 1, 9, 40), datetime.datetime(1900, 1, 1, 11, 8), datetime.datetime(1900, 1, 1, 11, 10)], [120, 20, 10, 0]]}

fig, ax = plt.subplots()
for k in dict_data:
    ax.plot(dict_data[k][0], dict_data[k][1])
plt.show()

使用python在同一图形上的多个图,时间坐标

显然这看起来有点局促,但如果你使用简洁的转换器:

plt.rcParams['date.converter'] = 'concise'

然后你得到使用python在同一图形上的多个图,时间坐标

uj5u.com热心网友回复:

基于有问题的编辑进行编辑

代码,使用plot_date和线型'-'

import matplotlib
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt

dict_data = {"4 April": [[np.datetime64("2022-07-01T22:59:00"), np.datetime64("2022-07-01T23:01:00"), np.datetime64("2022-07-01T23:08:00"), np.datetime64("2022-07-01T23:50:00")], [405, 320, 300, 360]], "5 April": [[np.datetime64("2022-07-01T08:10:00"), np.datetime64("2022-07-01T09:40:00"), np.datetime64("2022-07-01T11:08:00"), np.datetime64("2022-07-01T11:10:00")], [120, 20, 10, 0]]}
# dict_data = {"4 April": [[datetime(2022, 7, 1, 22, 59), datetime(2022, 7, 1, 23, 1), datetime(2022, 7, 1, 23, 8), datetime(2022, 7, 1, 23, 50)], [405, 320, 300, 360]], "5 April": [[datetime(2022, 7, 1, 8, 10), datetime(2022, 7, 1, 9, 40), datetime(2022, 7, 1, 11, 8), datetime(2022, 7, 1, 11, 10)], [120, 20, 10, 0]]}

def multiple_plots(dict_data):
    """
    Method to plot multiple times in one figure.
    It receives a dictionary with the representation of the data in the csv file.
    Every key in the dictionary represent a different date that will have its own plot ont the graph.
    """
    for date, coordinates in dict_data.items():
        time_coordinates = coordinates[0]
        # converting the x coordinates in the type np.datetime64 t2022-07-01To int
        x_coordinates = matplotlib.dates.date2num(time_coordinates)
        val_coordinates = coordinates[1]
        plt.plot_date(list(map(int, x_coordinates)), list(map(int, val_coordinates)), '-', label=date)

    plt.legend(loc='best')
    plt.show()

multiple_plots(dict_data)

最终输出: 使用python在同一图形上的多个图,时间坐标



date2num除了时间之外,功能还需要年、月和日。因此,两个简单的修复是:

  1. 发送年、月和时间
dict_data = {"4 April": [[datetime(2022, 7, 1, 22, 59), datetime(2022, 7, 1, 23, 1), datetime(2022, 7, 1, 23, 8), datetime(2022, 7, 1, 23, 50)], [405, 320, 300, 360]], "5 April": [[datetime(2022, 7, 1, 8, 10), datetime(2022, 7, 1, 9, 40), datetime(2022, 7, 1, 11, 8), datetime(2022, 7, 1, 11, 10)], [120, 20, 10, 0]]}
  1. 发送np.datetime64型别
dict_data = {"4 April": [[np.datetime64("2022-07-01T22:59:00"), np.datetime64("2022-07-01T23:01:00"), np.datetime64("2022-07-01T23:08:00"), np.datetime64("2022-07-01T23:50:00")], [405, 320, 300, 360]], "5 April": [[np.datetime64("2022-07-01T08:10:00"), np.datetime64("2022-07-01T09:40:00"), np.datetime64("2022-07-01T11:08:00"), np.datetime64("2022-07-01T11:10:00")], [120, 20, 10, 0]]}

他们都作业。最后的情节是: 使用python在同一图形上的多个图,时间坐标

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *