PYTHON (matplotlib), statistical temperature line chart

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

filename = 'E:\PYTHON\《Python编程》源代码文件\chapter_16\sitka_weather_07-2014.csv'
from matplotlib import pyplot as plt

with open(filename) as f:
    # 创建与该文件相关联的阅读器
    # 下面的就是迭代器,要通过next调用
    # reader = csv.reader(f)
    # header_row = next(reader)
    # #相当于将整个数据的行作为一个数值,如果用next就可以不断调用。
    # #for index,column_header in enumerate(header_row):
    #
    # #利用enumerate,我们定义了index,将每个指标的列进行的位置定位。
    # highs = []
    # for row in reader:
    #     highs.append((row[1]))
    # #这里打印出来的highs就是最高气温的温度,就是统计了所有样本的最高温度。
    class temperature():
        def __init__(self):
            self.reader = csv.reader(f)
            self.header_row = next(self.reader)
            self.highs = []
            self.alldate = []
            #self.header_row表示为列

        def all_row(self):
            for row in self.reader:
                current_date = datetime.strptime(row[0],"%Y-%m-%d")
                self.alldate.append(current_date)
                high = int(row[1])
                self.highs.append(high)
                
    high = temperature()
    high.all_row()
    highs_temp = high.highs
    current_date = high.alldate

    fig = plt.figure(dpi=128, figsize=(10, 6))
    plt.plot(current_date,highs_temp, c='red')
    #current_date作用于X坐标,highs_temp作用于y坐标
    plt.title('Daily high temperatures, july 2014', fontsize=24)
    plt.xlabel('', fontsize=16)
    plt.ylabel('temperature (F)', fontsize=16)
    plt.tick_params(axis='both',which = 'major ',labelsize = 16)
    plt.show()

Insert picture description here
According to the content of the book, I classify the original data and add the temperature and date to the list respectively. (Self.high, self.alldate), but the all_row function must be called first, so that the data will be added to the list.
Here you can also add a code to prevent label overlap after the line chart is generated:

    fig.autofmt_xdate()
    ```

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

filename ='E:\PYTHON\"Python Programming" source code file\chapter_16\death_valley_2014.csv'
from matplotlib import pyplot as plt

with open(filename) as f:
# Create a reader associated with the file
# The following is the iterator, which must be called by next
# reader = csv.reader(f)
# header_row = next(reader)
# #Equivalent to The entire row of data is used as a value, which can be called continuously if you use next.
# #for index,column_header in enumerate(header_row):
#
# #Using enumerate, we define the index and position the column of each indicator.
# highs = []
# for row in reader:
# highs.append((row[1])) # #The
highs printed here is the temperature of the highest temperature, which is the highest temperature of all samples.
class temperature():
def init (self):
self.reader = csv.reader(f)
self.header_row = next(self.reader)
self.highs = []
self.alldate = []
self.lows = []
# self.header_row is represented as a column

    def all_row(self):
        for row in self.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 date')
            else:
                self.lows.append(low)
                self.highs.append(high)
                self.alldate.append(current_date)

high = temperature()
high.all_row()
highs_temp = high.highs
current_date = high.alldate
lows_temp = high.lows
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(current_date,highs_temp, c='red',alpha = 0.5)
plt.plot(current_date,lows_temp, c = 'blue',alpha = 0.5)
#通过faceclor填充了区域的颜色,alpha表示透明度。
plt.fill_between(current_date,highs_temp,lows_temp,facecolor = 'blue',alpha = 0.1)
plt.title('Daily high temperatures, july 2014', fontsize=24)
plt.xlabel('', fontsize=16)
plt.ylabel('temperature (F)', fontsize=16)
#避免标签重叠
fig.autofmt_xdate()
plt.tick_params(axis='both',which = 'major ',labelsize = 16)
plt.show()
进行最低温和最高温的比较,同时运用try-except工具,这里死亡大峡谷的其中数据是缺失的,为了让其程序不会停止,我们用到try-except。

Guess you like

Origin blog.csdn.net/weixin_49712647/article/details/113115702