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

實屬愚笨,求賜教
uj5u.com熱心網友回復:
日期資料太密集,想顯示也顯示不完吧。你試著只取7天的資料看看效果。
uj5u.com熱心網友回復:
只是顯示月份啊,就是說12個刻度就夠了
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/106428.html
