gpt4 book ai didi

Python matplotlib 和绘制日期时间信息

转载 作者:行者123 更新时间:2023-12-01 02:54:53 26 4
gpt4 key购买 nike

我从 API 生成了以下数据。我现在尝试使用 matplotlib 绘制一个图表,显示这些天的日期和湿度。问题是我那些日子的时间不同。如何按时间过滤,以便仅显示我选择的一天中的时间。我附加了一个 python 文件,其中仅包含从我的程序中取出的相关数据,以便能够执行此操作。

我是 matplotlibnumpy 的新手

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

'''This is the data generated'''

date=[datetime.datetime(2017, 6, 2, 12, 0),
datetime.datetime(2017, 6, 2, 15, 0),
datetime.datetime(2017, 6, 2, 18, 0),
datetime.datetime(2017, 6, 2, 21, 0),
datetime.datetime(2017, 6, 3, 0, 0),
datetime.datetime(2017, 6, 3, 3, 0),
datetime.datetime(2017, 6, 3, 6, 0),
datetime.datetime(2017, 6, 3, 9, 0),
datetime.datetime(2017, 6, 3, 12, 0),
datetime.datetime(2017, 6, 3, 15, 0),
datetime.datetime(2017, 6, 3, 18, 0),
datetime.datetime(2017, 6, 3, 21, 0),
datetime.datetime(2017, 6, 4, 0, 0),
datetime.datetime(2017, 6, 4, 3, 0),
datetime.datetime(2017, 6, 4, 6, 0),
datetime.datetime(2017, 6, 4, 9, 0),
datetime.datetime(2017, 6, 4, 12, 0),
datetime.datetime(2017, 6, 4, 15, 0),
datetime.datetime(2017, 6, 4, 18, 0),
datetime.datetime(2017, 6, 4, 21, 0),
datetime.datetime(2017, 6, 5, 0, 0),
datetime.datetime(2017, 6, 5, 3, 0),
datetime.datetime(2017, 6, 5, 6, 0),
datetime.datetime(2017, 6, 5, 9, 0),
datetime.datetime(2017, 6, 5, 12, 0),
datetime.datetime(2017, 6, 5, 15, 0),
datetime.datetime(2017, 6, 5, 18, 0),
datetime.datetime(2017, 6, 5, 21, 0),
datetime.datetime(2017, 6, 6, 0, 0),
datetime.datetime(2017, 6, 6, 3, 0),
datetime.datetime(2017, 6, 6, 6, 0),
datetime.datetime(2017, 6, 6, 9, 0),
datetime.datetime(2017, 6, 6, 12, 0),
datetime.datetime(2017, 6, 6, 15, 0),
datetime.datetime(2017, 6, 6, 18, 0),
datetime.datetime(2017, 6, 6, 21, 0)]

humidity=[69, 70, 64, 68, 76, 81, 76, 76, 67, 54,
53, 59, 68, 81, 80, 73, 65, 59, 57, 64,
92, 82, 81, 81, 71, 63, 60, 61, 67, 76,
80, 75, 67, 83, 81, 82]

最佳答案

使用 pandas 可以简化这种情况。代码是:

import datetime
import pandas as pd
import matplotlib.pyplot as plt

'''This is the data generated'''

date=[datetime.datetime(2017, 6, 2, 12, 0),
datetime.datetime(2017, 6, 2, 15, 0),
datetime.datetime(2017, 6, 2, 18, 0),
...,
datetime.datetime(2017, 6, 6, 15, 0),
datetime.datetime(2017, 6, 6, 18, 0),
datetime.datetime(2017, 6, 6, 21, 0)]

humidity=[69, 70, 64, 68, 76, 81, 76, 76, 67, 54,
53, 59, 68, 81, 80, 73, 65, 59, 57, 64,
92, 82, 81, 81, 71, 63, 60, 61, 67, 76,
80, 75, 67, 83, 81, 82]

df = pd.DataFrame(humidity, index=date)
df.plot(figsize=(16, 6), marker='o')
plt.show()

这将为您提供以下图表: enter image description here

也可以使用纯 matplotlib 和更多自定义图形来完成此操作。下面是一个示例:

import datetime
import matplotlib.pyplot as plt

'''This is the data generated'''

date=[datetime.datetime(2017, 6, 2, 12, 0),
datetime.datetime(2017, 6, 2, 15, 0),
datetime.datetime(2017, 6, 2, 18, 0),
...,
datetime.datetime(2017, 6, 6, 15, 0),
datetime.datetime(2017, 6, 6, 18, 0),
datetime.datetime(2017, 6, 6, 21, 0)]

humidity=[69, 70, 64, 68, 76, 81, 76, 76, 67, 54,
53, 59, 68, 81, 80, 73, 65, 59, 57, 64,
92, 82, 81, 81, 71, 63, 60, 61, 67, 76,
80, 75, 67, 83, 81, 82]

plt.figure(figsize=(16,9), dpi=100)
OX_ticks_str = [dateRef.strftime('%Y-%m-%d %H:%M') for dateRef in date]
OX_ticks_pos = range(len(humidity))
plt.plot(range(len(humidity)), humidity, color='b', alpha=1.0, marker='o', ms=3, mfc='b', mec='b', label='Humdity')
plt.title('Humidity', weight='bold')
plt.xticks(OX_ticks_pos, OX_ticks_str, rotation=40, horizontalalignment='right', fontsize=7)
plt.grid()
plt.legend(loc='upper left', fancybox=True, fontsize='large', framealpha=0.5)
plt.tight_layout()
plt.show()

在这种情况下,它会为您提供以下图表: enter image description here

关于Python matplotlib 和绘制日期时间信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44326498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com