這是圖書《Python編程:從入門到實踐》中第16章的練習題16-2; 修改所示圖表的y軸設定。
我使用了plt.ylim()命令。
以下是代碼:
import csv
from datetime import datetime
from matplotlib import pyplot as plt
#打開檔案
filename='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[2])
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')
plt.plot(dates,lows,c='blue')
plt.fill_between(dates,highs,lows,facecolor='blue',alpha=0.33)
#設定圖形格式
plt.title("Daily high and low temperatures -2014\nDeath Valley,CA", fontsize=20)
plt.xlabel('',fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Temperature(F)",fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)
plt.ylim(10,120)
plt.show()
plt.ylim(10,120)的意思是想把坐標軸范圍設定在10到120之間,但是運行后出現了報錯資訊:
2014-02-16 00:00:00 missing data
Traceback (most recent call last):
File "C:\Users\XXX\.spyder-py3\learn\deathvally_highs_lows_2014.py", line 44, in <module>
plt.ylim(10,120)
TypeError: 'tuple' object is not callable
圖表為
坐標軸設定也不正確,沒有按照預想的設定。求大神解答為什么會出現這樣的錯誤?
uj5u.com熱心網友回復:
我關閉程式,重啟電腦之后,這個問題就沒有了。轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/202801.html
上一篇:面試官:我們只想要這樣的C++工程師!現在你該知道要學什么了吧
下一篇:使用LIME解釋黑盒ML模型
