学习python入门时,在学习使用matplotlib时遇到一个问题
书中显示的x坐标日期是齐全的,我查阅过csv文件,日期数据也是齐全的
可是我运行时x坐标却显示不齐全,
求大大们给解解~谢谢
import csv
from datetime import datetime
from matplotlib import pyplot as plt
filename = r'death_valley_2014.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
dates, highs, lows = [], [], []
for row in reader:
try:
current_date = datetime.strptime(row[0], "%Y-%m-%d")
high = int(row[1])
low = int(row[3])
except ValueError:
print(current_date, 'missing data')
else:
dates.append(current_date)
highs.append(high)
lows.append(low)
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, highs, c='red', alpha=0.5) # 实参alpha指定颜色的透明度
plt.plot(dates, lows, c='blue', alpha=0.5)
plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.1) # 实参face color指定了填充区域的颜色
title = "Daily high and low temperatures - 2014\nDeath Valley, CA"
plt.title(title, fontsize=20)
plt.xlabel('', fontsize=10)
fig.autofmt_xdate() # 调用fig.autofmt_xdate()来绘制斜的日期标签,以免它们彼此重叠
plt.ylabel('Temperature (F)', fontsize=10)
plt.tick_params(axis='both', which='major', labelsize=10)
plt.show()
实属愚笨,求赐教