我已从API生成以下数据。我现在正尝试使用matplotlib
绘制图表,显示当天的日期和湿度。问题是那些日子我有不同的时间。如何按时间过滤,以便仅显示我选择的时间。我附上了一个python文件,只有从我的程序中取出的相关数据才能够做到这一点。
我是matplotlib
和numpy
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]
答案 0 :(得分:2)
使用将简化此案例的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()
也可以使用纯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()