The matplotlib module of Python real-combat data visualization (actual combat article)

Source: Internet
Author: User

Frontier

Through the previous discussion of Python real-world data visualization of the Matplotlib module (basic article) of learning, we have a preliminary understanding of the Matplotlib Module Pyplot Foundation, this section of the actual combat will use the CSV module to get weather data, And visualize weather data using the Matplotlib module.

Supporting Resources

In view of the Python programming from the beginning to combat the book's supporting resources on the Internet hard to find sadness, I have deep experience. So, here is a link for download (Help me to support my oh ('? '). )
Baidu Cloud Link: https://pan.baidu.com/s/1-XE0pBS8IaDLoUBdO8hDOw Password: n39g

CSV file format

CSV-comma-separated value file format, comma-separated values (comma-separated values,csv, sometimes also called character-delimited values, because delimited characters can also be not commas), whose files store tabular data (numbers and text) in plain text. Plain text means that the file is a sequence of characters and does not contain data that must be interpreted like a binary number. A CSV file consists of any number of records separated by a newline character, each record consists of a field, and the delimiter between the fields is another character or string, most commonly a comma or tab. Typically, all records have exactly the same sequence of fields. are usually plain text files.
For example:

Country,Indicator,Year,ValueAFG,NGDP_R,2002,183.26AFG,NGDP_R,2003,198.736AFG,NGDP_R,2004,200.069AFG,NGDP_R,2005,223.737AFG,NGDP_R,2006,235.731AFG,NGDP_R,2007,267.177AFG,NGDP_R,2008,277.498AFG,NGDP_R,2009,334.621AFG,NGDP_R,2010,362.857AFG,NGDP_R,2011,386.368AFG,NGDP_R,2012,440.336AFG,NGDP_R,2013,456.453..............................................
Get weather data using the CSV module

By placing the Sitka_weather_07-2014.csv file in the same directory as the project, and then using the CSV module provided by the Python standard library to parse the data rows in the CSV file (that is, the records mentioned above), we can quickly extract the values of interest. The case code is as follows:

import csv# 包含一个sitka城市七月份天气信息的CSV文件,里面都是用逗号隔开filename = ‘sitka_weather_07-2014.csv‘# 打开csv文件,实例化一个csv模块的reader阅读器对象,是一个可迭代对象,所以可以使用for循环遍历该reader阅读器对象,# 也可以调用BIF-next函数遍历下一行,需要注意的是reader里面的每行记录只能被遍历一次,其中reader有一个line_num属性返回遍历过程中对应的行号。with open(filename) as f:    reader = csv.reader(f)  # 返回的是一个csv的阅读器对象,直接打印获取不到里面的内容    print(reader)  # 打印出的是<_csv.reader object at 0x000002D1B24912B8>    head_row = next(reader)  # 返回第一行的记录内容组成的字符串列表    print(head_row)  # 打印 [‘AKDT‘, ‘Max TemperatureF‘...]    for row in reader:        # 根据阅读器对象的记录(一行算一个记录)只能遍历一次的特性,行号从2开始了哦        print(reader.line_num, row)  # 打印 2 [‘2014-7-1‘, ‘64‘, ‘56‘, ‘50‘....]

The results of the operation are as follows:

OK, preliminary get to the content of the CSV file, but we have to note that the reader inside each row of records can only be traversed once, we add a line below a deeper understanding of the code:

import csvfilename = ‘sitka_weather_07-2014.csv‘with open(filename) as f:    reader = csv.reader(f)    print(reader)    print(list(reader))  # 值得注意的是,当先调用 print(list(reader)),发现遍历到最后了,再执行后面的代码就报错了    head_row = next(reader)    print(head_row)    for row in reader:        print(reader.line_num, row)

The results of the operation are as follows:

The above results are analyzed: for the list built-in function, the internal implementation presumably can guess is that the reader object inside the record traversal and then added to a list, and then return the entire list. The call to the list built-in function has actually been traversed once, the traversal pointer points to the last record, and since the reader's record (one row for a record) can only be traversed once, it makes no sense for the subsequent code to traverse the reader object again.

Draw Sitka July 2014 daily maximum temperature line chart

We already know how to get the weather data, so we'll use the Pyplot module to draw the Sitka July 2014 maximum daily Air Line chart. The code is as follows (comments help to understand the code):

# Draw Sitka July Weather Data # import Python-supported CSV modules to process CSV files, parse data rows in CSV files, let's extract the values we're interested in import csv# the Pyplot module in the Matplotlib package, Used to visualize the maximum daily temperature from matplotlib import Pyplot as pltfrom datetime import datetime# contains a CSV file of city weather information, which is separated by commas with the filename = ' s  Itka_weather_07-2014.csv ' # Opens a CSV file, instantiates a CSV module reader object with open (filename) as F:reader = Csv.reader (f) Head_row =    Next (reader) # Returns a list of strings consisting of the record contents of the first row, for example: [' 1 ', ' 2 ' ....] Highs = [] # is used to store the highest temperature per day dates = [] # is used to store the date for the row in reader: # row is also a list of strings that return the recorded contents of the traversed row current_date = d Atetime.strptime (Row[0], "%y-%m-%d") # Speaks of a time string converted to a DateTime object in the specified format dates.append (current_date) high = Int (row[1 ] # Because the front has been next once, according to the nature of reader can only be traversed once, here for the loop from the second line to start traversing highs.append (high) # will be the highest temperature per day in the highs list, when the list of stored elements are numbers rather than String, then we can use this list for visualization of fig = Plt.figure (dpi=128, figsize= (6)) Plt.plot (dates, highs, c= ' red ') plt.title ("Daily High Temperatures,july ", fontsize=24) Plt.xlabel (", fontsize=16) Fig.autofmt_xdate () # Draw an oblique date label Plt.ylabel ("TeMperature (F) ", fontsize=16) Plt.tick_params (axis= ' both ', which= ' major ', labelsize=14) plt.show () 

The results of the operation are as follows:

Careful observation of the above operation results, the x-axis or book pictures and some of the discrepancy, in fact, this is correct, because we are running the results of the beginning of July, but the x-axis will be included in June.

Draw Sitka 2014 daily highest and lowest temperature line chart

We have drawn the Sitka July daily high-temperature line chart, and now we will get the weather data for the whole year of Sitka 2014, and then draw the Sitka 2014 maximum daily and lowest temperature line chart. Let's first put the Sitka_weather_2014.csv file in the same directory as the project. The code is as follows:

# Draw Sitka Weather data for the whole year without error checking # import Python-supported CSV modules to process CSV files, parse data rows in CSV files, let's extract the values we're interested in import csv# the Pyplot module in the Matplotlib package, Used to visualize the maximum daily temperature from matplotlib import Pyplot as pltfrom datetime import datetime# contains a CSV file of city weather information, which is separated by commas with the filename = ' s Itka_weather_2014.csv ' # Opens a CSV file, instantiates a CSV module reader object with open (filename) as F:reader = Csv.reader (f) head_row = NE    XT (Reader) # Returns a list of strings consisting of the record contents of the first row, for example: [' 1 ', ' 2 ' ....] Highs = [] # is used to store the highest temperature per day of the year dates = [] # for storing the date lows = [] # is used to store the lowest temperature per day of the year for the row in reader: # row also returns the traversal of the row String list of element contents current_date = Datetime.strptime (Row[0], "%y-%m-%d") dates.append (current_date) high = i NT (Row[1]) # Because the front has been next once, according to the nature of reader can only be traversed once, here for loop from the second line to start traversing highs.append (high) # will be the maximum temperature per day in the highs list, when the list stores the Elements are numbers instead of strings, we can use this list for visualization of low = Int (row[3]) lows.append (low) FIG = Plt.figure (dpi=128, figsize= (10, 6 ) Plt.plot (dates, highs, c= ' red ', alpha=0.5) # draw the highest temperature line chart for every day of the year Plt.plot (dates, lows, c= ' Blue ', alpha=0.5# Draw the lowest temperature of every day of the Year Line chart Plt.fill_between (dates, highs, lows, facecolor= ' Blue ', alpha=0.1) # fills the area between the daily high and low temperatures plt.title (" Daily High and Low temperatures,-", fontsize=24) Plt.xlabel (", fontsize=16) Fig.autofmt_xdate () Plt.ylabel (" Temperature (F) ", fontsize=16) Plt.tick_params (axis= ' both ', which= ' major ', labelsize=14) plt.show ()

The results of the operation are as follows:

Because some weather stations occasionally fail to collect all the data that should be collected, if we use the previous code example to obtain weather data for a city, then an error will be reported because the city's CSV file may be missing data for one day, so it will report valueerror errors. Just like this is missing a day's data:

So, given the above question, the next section gets the weather data for the whole year of Death Valley will be treated with exception handling mechanism to prevent this situation.

Plot Death Valley 2014 daily highest and lowest temperature line chart

As mentioned earlier, there may be a lack of data in the actual application, and so on, as a program developer, you must envisage a variety of potential problems, and use practical methods to solve the problem. We will use the exception handling mechanism to plot the daily maximum and minimum temperature line charts for the Death Valley 2014. The code is as follows:

# import Python supported CSV module, used to process CSV file, parse data row in CSV file, let us extract value of our interest import csv# imported into Matplotlib package Pyplot module, used to visualize the maximum daily temperature from Matplotlib Import Pyplot as pltfrom datetime import datetime# A CSV file that contains a city weather information filename = ' death_valley_2014.csv ' # open CSV file , instantiates a CSV module reader object with open (filename) as F:reader = Csv.reader (f) head_row = Next (reader) # Returns the string column that consists of the first row of the record contents    Table, for example: [' 1 ', ' 2 ' ....] Highs = [] # is used to store the highest temperature per day dates = [] # for storing the date lows = [] # used to store the lowest temperature per day for row in reader: # row is also a word that returns the contents of the record that traversed the row String list try:current_date = Datetime.strptime (Row[0], "%y-%m-%d") high = Int (row[1]) # Because the front already NE The XT is once, depending on the nature of the reader being traversed only once, where the for loop traverses low = Int (row[3]) from the second line except Valueerror:print (current _date, ' missing Data ') Else:dates.append (current_date) highs.append (high) # Keep the highest temperature per day in highs List, when the list stores elements that are numbers instead of strings, then we can use this list to visualize the lows.append (low) FIG = Plt.figure (dpi=128, figsize= (6)) Plt.plo T (dates, highs, C= ' Red ', alpha=0.5) plt.plot (dates, lows, c= ' Blue ', alpha=0.5) plt.fill_between (dates,highs,lows,facecolor= ' Blue ', alpha=0.1) 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 =14) Plt.show ()

The results of the operation are as follows (two images):
(1) Line chart

(2) Terminal diagram

By the above two figures can be known, 2014-02-16 this day the temperature data is lost, so in the line chart actually did not draw the 2014-02-16 day of the highest and lowest temperature (due to too dense to see).

The matplotlib module of Python real-combat data visualization (actual combat article)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.