|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这个是Python入门到实践项目2里面的例子,代码里面需要安装matplotlib,还有用到的CSV文件我放在附件里了。
提问:
我的X轴为什么日期少了?和书上代码对比一模一样。
书上的代码:
书上的结果:
书上的X轴的坐标上,每个月的日期都有,我自己运行的程序,怎么就少了好几个月的坐标!
我的程序代码:
- #csv模块把汗在Python标准库里,
- import csv
- from datetime import datetime
- from matplotlib import pyplot as plt
- """从文件中获取最高的温度"""
- filename = 'sitka_weather_07-2014.csv'
- with open(filename) as f:
- reader = csv.reader(f)
- header_row = next(reader)# next调用第一次为第一行,第二次为第二行
- #print(header_row)
-
- dates,highs = [],[]
- for row in reader: #再遍历文件中余下的各行
- current_date = datetime.strptime(row[0],"%Y-%m-%d") #将每一行,第一位的时间表示出来
- #print(current_date)
- dates.append(current_date)
- high = int(row[1])
- highs.append(high)
-
- #根据数据绘制图形
- fig = plt.figure(dpi=128, figsize=(10,6))
- plt.plot(dates,highs,c='red')
- #设置图形的格式
- plt.title("Daily High temperatures, -2014",fontsize=24)
- plt.xlabel('',fontsize=16)
- fig.autofmt_xdate()
- plt.ylabel("Temperature(F)",fontsize=16)
- plt.tick_params(axis='both',which='major',labelsize=10)
- plt.show()
复制代码
自己程序运行的结果。
|
|