IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> python的16.1节课后练习16-2比较希特卡和死亡谷的气温的问题,最后图像为什么出现乱码? -> 正文阅读

[Python知识库]python的16.1节课后练习16-2比较希特卡和死亡谷的气温的问题,最后图像为什么出现乱码?

自己写的代码

#文件1.py

import csv

import matplotlib.pyplot as plt

from datetime import datetime

from read_fun import read_fun

death_valley_whe = 'death_valley_2014.csv'
sitka_whe = 'sitka_weather_2014.csv'

t1,t2 = [],[]
d1,d2 = [],[]

with open(death_valley_whe) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    read_fun(reader,t1,d1)

with open(sitka_whe) as f1:
    reader = csv.reader(f1)
    header_row = next(reader)
    read_fun(reader,t2,d2)

fig = plt.figure(dpi=128,figsize=(10,6))
plt.plot(d1,t1,alpha =0.5)
#plt.plot(dates2,sitka_mean_tem,c='blue',alpha=0.5)
plt.title("Temperature in death valley and sitka whether",fontsize=10)
fig.autofmt_xdate()
plt.xlabel("date",fontsize = 10)
plt.ylabel("temperature Dead-valley(red) and Sitka(blue)",fontsize=10)
plt.tick_params(axis='both',which='major',labelsize=10)
plt.ylim(10,120)

plt.show()

#文件 read_fun.py
from datetime import datetime
def read_fun(reader,tempus,dates):
    for row in reader:
        try:
            date = datetime.strptime(row[0],'%Y-%m-%d')
            mean_tem = row[2]
        except ValueError:
            print(date + " , missing data ")
        else:
            tempus.append(mean_tem)
            dates.append(date)
            print(date)

已经解决。

自己分析,如果日期集合和温度集合都是正确的,那么不该出现错误,后来自己把日期集合数出来,没错,但是把温度集合数出来如下:

['42', '47', '46', '47', '44', '40', '44', '40', '42', '44', '47', '50', '46', '50', '50', '50', '50', '48', '48', '49', '45', '52', '46', '54', '54', '48', '50', '52', '54', '60', '52', '44', '40', '40', '39', '41', '45', '50', '56', '57', '62', '54', '54', '56', '58', '60', '', '56', '61', '52', '53', '54', '55', '56', '56', '55', '62', '52', '50', '48', '51', '59', '58', '64', '62', '58', '60', '62', '58', '58', '56', '58', '62', '62', '68', '57', '54', '56', '61', '62', '62', '62', '62', '58', '58', '55', '64', '60', '58', '54', '51', '50', '58', '54', '63', '65', '67', '74', '70', '72', '68', '70', '66', '69', '70', '70', '71', '70', '72', '72', '65', '60', '65', '64', '58', '58', '63', '70', '69', '70', '75', '75', '80', '72', '60', '59', '66', '64', '69', '68', '70', '70', '75', '80', '84', '80', '74', '64', '61', '64', '68', '77', '79', '83', '82', '84', '75', '76', '80', '75', '80', '76', '78', '81', '81', '82', '88', '90', '88', '86', '84', '86', '78', '78', '81', '78', '74', '78', '82', '84', '83', '83', '84', '86', '85', '81', '86', '86', '88', '94', '91', '90', '90', '88', '88', '92', '92', '90', '86', '84', '86', '91', '93', '90', '90', '88', '86', '87', '85', '86', '86', '90', '88', '92', '89', '94', '88', '88', '90', '91', '91', '87', '76', '80', '82', '82', '82', '83', '86', '84', '85', '86', '87', '85', '83', '84', '84', '88', '82', '78', '77', '82', '84', '81', '80', '80', '80', '83', '84', '85', '88', '82', '85', '85', '82', '81', '82', '86', '84', '80', '79', '80', '82', '82', '84', '87', '87', '86', '84', '75', '77', '78', '77', '78', '76', '82', '76', '68', '64', '68', '70', '70', '68', '68', '71', '73', '73', '74', '72', '74', '70', '70', '72', '66', '67', '74', '64', '66', '65', '70', '70', '66', '64', '67', '68', '74', '64', '60', '62', '62', '64', '62', '54', '52', '53', '50', '57', '60', '61', '62', '62', '62', '56', '56', '59', '60', '54', '49', '44', '46', '52', '52', '55', '50', '52', '47', '48', '51', '53', '52', '60', '59', '50', '47', '55', '56', '53', '52', '51', '52', '52', '48', '52', '50', '47', '43', '42', '41', '47', '44', '42', '37', '50', '52', '48', '47', '42', '37', '38', '33']

纯粹都是字符型的。但是画图纵坐标要求是数值类型的变量(实属类型的),到此,恍然大悟,忘记把字符型数据进行转换了,解决方法如下:

把 文件read_fun.py 第7行的变为如下形式:

mean_tem = int(row[2])

自己后来又优化了下代码:

#文件1.py

import csv

from matplotlib import pyplot as plt

from datetime import datetime

from fun import read_fun

death_valley_whe = 'death_valley_2014.csv'
sitka_whe = 'sitka_weather_2014.csv'
#t1,d1代表死亡峡谷的日期和平均气温
#t2,d2代表锡特卡的日期和平均气温
t1,t2 = [],[]
d1,d2 = [],[]

read_fun(death_valley_whe,t1,d1)
read_fun(sitka_whe,t2,d2)

fig = plt.figure(dpi=128,figsize=(10,6))
plt.plot(d1,t1,c='red',alpha =0.5)
plt.plot(d2,t2,c='blue',alpha=0.5)
plt.title("Temperature in death valley and sitka whether",fontsize=10)
fig.autofmt_xdate()
plt.xlabel("date",fontsize = 10)
plt.ylabel("temperature Dead-valley(red) and Sitka(blue)",fontsize=10)
plt.tick_params(axis='both',which='major',labelsize=10)
plt.show()




#文件 fun.py

from datetime import datetime
import csv

def read_fun(file_var,tempus,dates):
    with open(file_var) as f:
        reader = csv.reader(f)
        header_row = next(reader)
        for row in reader:
            try:
                date = datetime.strptime(row[0],'%Y-%m-%d')
                #mean_tem 是平均气温,这里必须转换成int形式
                #因为文件内的数据默认不是int类型的,可能是字符串类型的或者字符类型的
                mean_tem = int(row[2])
            except ValueError:
                print(date," , missing data ")
            else:
                tempus.append(mean_tem)
                dates.append(date)

最后,作为一个复习,自己又把自己家乡(内蒙古乌兰察布市)2021年的全年最高气温和最低气温画了一个图,具体过程如下:

先从乌兰察布市天气网站下载2021年的全年气温,下载是1个月1个月地下载,还有很多不必要的数据,下载下来后自己用c++语言把需要的日期、最高气温、最低气温提取出来(其余不要,不会用python提取数据。只能用c++了),最后处理出来的数据如下:第一列代表日期,第二列代表这一天的最低气温,第三列代表这一天的最高气温,自己也把它放在date_file.csv文件中,不得不说,计算机处理重复性的事情还真是一绝,编好代码不到1秒给你处理完毕,如下:

2021-01-01,-8,-18
2021-01-02,-7,-20
2021-01-03,-7,-22
2021-01-04,-14,-23
2021-01-05,-13,-30
2021-01-06,0,-30
2021-01-07,-19,-27
2021-01-08,-13,-21
2021-01-09,-7,-19
2021-01-10,-8,-17
2021-01-11,-10,-17
2021-01-12,-4,-14
2021-01-13,-3,-10
2021-01-14,0,-16
2021-01-15,-10,-21
2021-01-16,-12,-19
2021-01-17,-8,-16
2021-01-18,-1,-12
2021-01-19,3,-13
2021-01-20,2,-14
2021-01-21,1,-15
2021-01-22,3,-11
2021-01-23,3,-11
2021-01-24,3,-10
2021-01-25,0,-14
2021-01-26,-4,-14
2021-01-27,-5,-22
2021-01-28,-8,-17
2021-01-29,-1,-12
2021-01-30,0,-10
2021-01-31,0,-16
2021-02-01,-11,-21
2021-02-02,-8,-16
2021-02-03,-5,-13
2021-02-04,1,-10
2021-02-05,2,-9
2021-02-06,4,-13
2021-02-07,-2,-13
2021-02-08,0,-10
2021-02-09,5,-11
2021-02-10,8,-4
2021-02-11,12,-9
2021-02-12,8,-4
2021-02-13,6,-7
2021-02-14,-4,-15
2021-02-15,0,-16
2021-02-16,-9,-17
2021-02-17,-1,-13
2021-02-18,7,-6
2021-02-19,9,-1
2021-02-20,12,-1
2021-02-21,12,-7
2021-02-22,4,-7
2021-02-23,3,-6
2021-02-24,8,-6
2021-02-25,7,-7
2021-02-26,7,-4
2021-02-27,5,-4
2021-02-28,-3,-9
2021-03-01,-1,-10
2021-03-02,5,-6
2021-03-03,8,-3
2021-03-04,11,-5
2021-03-05,0,-9
2021-03-06,3,-8
2021-03-07,8,-6
2021-03-08,9,-5
2021-03-09,11,-1
2021-03-10,12,0
2021-03-11,13,-1
2021-03-12,9,-5
2021-03-13,11,-1
2021-03-14,14,-7
2021-03-15,0,-10
2021-03-16,3,-6
2021-03-17,6,-2
2021-03-18,2,-2
2021-03-19,3,-6
2021-03-20,-1,-10
2021-03-21,3,-7
2021-03-22,9,-3
2021-03-23,14,0
2021-03-24,13,-3
2021-03-25,16,5
2021-03-26,14,-1
2021-03-27,16,-4
2021-03-28,6,-6
2021-03-29,9,-4
2021-03-30,16,1
2021-03-31,11,3
2021-04-01,9,1
2021-04-02,9,-2
2021-04-03,6,-6
2021-04-04,11,-1
2021-04-05,11,-1
2021-04-06,12,-3
2021-04-07,13,-1
2021-04-08,16,1
2021-04-09,16,3
2021-04-10,15,5
2021-04-11,11,2
2021-04-12,2,-8
2021-04-13,6,-4
2021-04-14,13,1
2021-04-15,10,-3
2021-04-16,4,-5
2021-04-17,13,-2
2021-04-18,17,1
2021-04-19,22,6
2021-04-20,22,6
2021-04-21,19,6
2021-04-22,15,5
2021-04-23,20,3
2021-04-24,13,5
2021-04-25,12,7
2021-04-26,15,1
2021-04-27,9,-1
2021-04-28,10,1
2021-04-29,6,0
2021-04-30,8,0
2021-05-01,13,1
2021-05-02,22,8
2021-05-03,19,1
2021-05-04,8,1
2021-05-05,18,3
2021-05-06,8,0
2021-05-07,20,4
2021-05-08,15,6
2021-05-09,20,5
2021-05-10,21,8
2021-05-11,25,7
2021-05-12,17,8
2021-05-13,19,10
2021-05-14,22,7
2021-05-15,13,5
2021-05-16,17,6
2021-05-17,20,10
2021-05-18,22,10
2021-05-19,23,10
2021-05-20,20,9
2021-05-21,26,13
2021-05-22,25,6
2021-05-23,13,2
2021-05-24,13,4
2021-05-25,24,8
2021-05-26,18,6
2021-05-27,18,4
2021-05-28,22,9
2021-05-29,25,10
2021-05-30,25,11
2021-05-31,16,4
2021-06-01,19,5
2021-06-02,14,5
2021-06-03,17,8
2021-06-04,20,8
2021-06-05,25,13
2021-06-06,25,13
2021-06-07,27,12
2021-06-08,18,9
2021-06-09,19,7
2021-06-10,25,13
2021-06-11,26,14
2021-06-12,28,16
2021-06-13,23,16
2021-06-14,26,14
2021-06-15,27,16
2021-06-16,16,10
2021-06-17,21,10
2021-06-18,23,11
2021-06-19,24,11
2021-06-20,27,12
2021-06-21,28,12
2021-06-22,28,15
2021-06-23,18,15
2021-06-24,21,15
2021-06-25,26,15
2021-06-26,27,16
2021-06-27,28,15
2021-06-28,29,17
2021-06-29,28,14
2021-06-30,27,13
2021-07-01,28,14
2021-07-02,21,12
2021-07-03,22,13
2021-07-04,26,15
2021-07-05,23,12
2021-07-06,25,14
2021-07-07,25,15
2021-07-08,28,16
2021-07-09,30,18
2021-07-10,32,17
2021-07-11,29,15
2021-07-12,17,16
2021-07-13,30,20
2021-07-14,31,19
2021-07-15,28,18
2021-07-16,29,18
2021-07-17,24,17
2021-07-18,26,16
2021-07-19,27,17
2021-07-20,27,16
2021-07-21,24,16
2021-07-22,22,15
2021-07-23,26,17
2021-07-24,28,18
2021-07-25,27,16
2021-07-26,26,14
2021-07-27,23,15
2021-07-28,24,16
2021-07-29,25,14
2021-07-30,29,16
2021-07-31,26,12
2021-08-01,22,11
2021-08-02,25,16
2021-08-03,18,13
2021-08-04,25,14
2021-08-05,24,13
2021-08-06,26,14
2021-08-07,28,15
2021-08-08,23,13
2021-08-09,26,13
2021-08-10,25,12
2021-08-11,25,11
2021-08-12,26,12
2021-08-13,26,14
2021-08-14,26,14
2021-08-15,25,13
2021-08-16,27,15
2021-08-17,27,15
2021-08-18,25,15
2021-08-19,24,13
2021-08-20,22,10
2021-08-21,26,14
2021-08-22,25,11
2021-08-23,24,8
2021-08-24,14,8
2021-08-25,15,9
2021-08-26,18,7
2021-08-27,20,9
2021-08-28,19,9
2021-08-29,21,9
2021-08-30,23,13
2021-08-31,22,13
2021-09-01,24,12
2021-09-02,25,13
2021-09-03,26,13
2021-09-04,16,10
2021-09-05,15,8
2021-09-06,20,10
2021-09-07,23,11
2021-09-08,23,10
2021-09-09,21,8
2021-09-10,28,12
2021-09-11,26,12
2021-09-12,25,13
2021-09-13,22,13
2021-09-14,23,13
2021-09-15,17,11
2021-09-16,18,6
2021-09-17,21,8
2021-09-18,20,11
2021-09-19,17,8
2021-09-20,19,7
2021-09-21,20,6
2021-09-22,24,10
2021-09-23,25,11
2021-09-24,19,11
2021-09-25,20,9
2021-09-26,18,9
2021-09-27,21,7
2021-09-28,23,6
2021-09-29,22,6
2021-09-30,19,2
2021-10-01,19,5
2021-10-02,24,10
2021-10-03,16,4
2021-10-04,7,2
2021-10-05,9,3
2021-10-06,8,0
2021-10-07,13,2
2021-10-08,12,5
2021-10-09,12,-1
2021-10-10,9,-3
2021-10-11,12,-5
2021-10-12,14,6
2021-10-13,12,6
2021-10-14,13,-1
2021-10-15,6,-9
2021-10-16,6,-5
2021-10-17,10,-2
2021-10-18,10,-9
2021-10-19,7,-5
2021-10-20,10,-7
2021-10-21,8,-6
2021-10-22,9,-6
2021-10-23,12,-3
2021-10-24,13,-1
2021-10-25,13,-2
2021-10-26,10,-6
2021-10-27,11,-3
2021-10-28,10,0
2021-10-29,12,-5
2021-10-30,9,-6
2021-10-31,6,-7
2021-11-01,9,-4
2021-11-02,8,-3
2021-11-03,10,-3
2021-11-04,13,0
2021-11-05,14,-7
2021-11-06,-7,-15
2021-11-07,-9,-16
2021-11-08,-5,-11
2021-11-09,-2,-6
2021-11-10,4,-10
2021-11-11,1,-8
2021-11-12,3,-7
2021-11-13,6,-6
2021-11-14,5,-10
2021-11-15,3,-8
2021-11-16,6,-8
2021-11-17,5,-5
2021-11-18,6,-9
2021-11-19,4,-8
2021-11-20,1,-12
2021-11-21,-7,-17
2021-11-22,-5,-13
2021-11-23,2,-11
2021-11-24,2,-8
2021-11-25,5,-11
2021-11-26,5,-7
2021-11-27,7,-4
2021-11-28,7,-5
2021-11-29,-2,-12
2021-11-30,-6,-12
2021-12-01,-1,-12
2021-12-02,2,-10
2021-12-03,4,-10
2021-12-04,5,-8
2021-12-05,4,-12
2021-12-06,2,-13
2021-12-07,2,-6
2021-12-08,2,-8
2021-12-09,1,-9
2021-12-10,2,-10
2021-12-11,-2,-17
2021-12-12,-5,-15
2021-12-13,-2,-13
2021-12-14,-1,-11
2021-12-15,0,-15
2021-12-16,-9,-21
2021-12-17,-11,-21
2021-12-18,-6,-12
2021-12-19,0,-10
2021-12-20,2,-11
2021-12-21,3,-13
2021-12-22,2,-12
2021-12-23,-5,-19
2021-12-24,-14,-26
2021-12-25,-15,-22
2021-12-26,-6,-17
2021-12-27,-3,-15
2021-12-28,-2,-18
2021-12-29,-4,-13
2021-12-30,-3,-14
2021-12-31,-1,-15

?接下来就是写python3代码处理数据,画图了:

#wlcb_tem.py

import csv 
from datetime import datetime
from matplotlib import pyplot as plt
from read_fun import read_fun

#读入乌兰察布2021年全年日期、最高气温和最低气温
dates = []
highs = []
lows = []
read_fun(dates,highs,lows)

#画图分析
fig = plt.figure(dpi = 128,figsize=(10,6))
plt.plot(dates,highs,c='red',alpha=0.5)
plt.plot(dates,lows,c='blue',alpha=0.5)

plt.title("Ulanqab City daily high(red) and low(blue) temperatures - 2021")
plt.xlabel("",fontsize=14)
plt.ylabel("Temperature (Celsius)")
plt.tick_params(axis='both',which='major',labelsize=14)
fig.autofmt_xdate()
plt.fill_between(dates,highs,lows,alpha=0.1)


plt.show()
#read_fun.py

import csv
from datetime import datetime 

#dates:日期集合
#highs:最高气温集合
#lows:最低气温集合
def read_fun(dates,highs,lows):
    file_name = "date_file.csv"
 
    with open(file_name) as f:
        reader = csv.reader(f)
        header_row = next(reader)
        for row in reader:
            try:
                date = datetime.strptime(row[0],"%Y-%m-%d")
                #必须把两个气温都转换成int类型,不然画图y轴坐标会乱码
                high = int(row[1])
                low = int(row[2])
            except ValueError:
                print(date,",data missing.")
            else:
                dates.append(date)
                highs.append(high)
                lows.append(low)

            

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-01-28 11:52:10  更:2022-01-28 11:53:48 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/16 2:31:10-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码