[Python initial experience] Matplotlib function library drawing-electronic system introduction experimental report

[Python initial experience] Matplotlib function library drawing-electronic system introduction experimental report

1930713XXXX Nie XX Technical Science Experimental Class

1. The purpose of the experiment:

  • Familiar with the basic syntax of Python.

  • Familiar with Matplotlib library functions to draw curves.

2. Experimental content:

  • Obtain data on the number of newly diagnosed new crowns nationwide from February 1st to February 29th from online channels , and perform Python programming. Use Matplotlib library functions to draw the curve (the horizontal axis is the date, the vertical axis is the number of newly confirmed people), and output the maximum value and the corresponding date, minimum value and the corresponding date, average and median value.

3. Record of experiment process:

  1. Download data on the number of newly diagnosed people nationwide in February. Data source: Zhentu 2019-nCoV historical data

    Enter the number of newly diagnosed daily in February into a txt file in order of date: data.txt

  1. Write Python programs to implement various functions.
  • Output the prompt sentence, enter the data into y to form a list:
print("请输入2月1日到2月29日期间中国新冠疫情每日新增确诊人数:")
print("(29个整数,用空格隔开)")
y = input().split()

  • Taking into account the need to use the Matplotlib function to plot, the y-axis data (the number of newly diagnosed people daily) has been obtained, and the data corresponding to the x-axis (corresponding to the date) needs to be generated:
x = np.arange('2020-02-01','2020-03-01',dtype=np.datetime64)

  • Next is drawing. Use the title(), xlable() and ylable() functions to set the chart title and axis labels. Use the plot() function to draw, where the parameter "-or" represents polyline + dot + red, and label represents the label of this set of data. Since it is national data, the label is "China". Finally, use the show() function to display the picture.
plt.title("Chart of the number of newly confirmed cases per day in February 2020")
plt.xlabel("Date")
plt.ylabel("Number of newly confirmed cases")

plt.plot(x,y,"-or",label="China")
plt.show()

  • View the drawing effect:

  • Next, complete the maximum and minimum data processing, as well as the middle position and average value. Use a list named pairs to store the daily data two-tuple: (number of newly confirmed diagnoses, date), use the sort() function to sort the number of newly diagnosed diagnoses as the first keyword in ascending order, and then you can easily access the newly added diagnoses Information on the date with the largest and smallest number of people, and the median value can be found directly. Accumulate the total number of people through the Sum variable, divide by the number of days to get the average value, and keep one decimal place for output.

Days = 29 
Sum = 0
i = 0

pairs = []

for dat in y:
    y[i] = int(y[i])
    Sum += y[i]
    pairs.append( (y[i],i+1) )
    i+=1
    
pairs.sort()

  • View the running results:

  • So far, the experimental content has been implemented, and all the code ( export.py ) is posted below :

# -*- coding: UTF-8 -*-
# George_Plover
import numpy as np
import matplotlib.pyplot as plt


print("请输入2月1日到2月29日期间中国新冠疫情每日新增确诊人数:")
print("(29个整数,用空格隔开)")
y = input().split()

x = np.arange('2020-02-01','2020-03-01',dtype=np.datetime64)

Days = 29 
Sum = 0
i = 0

pairs = []

for dat in y:
    y[i] = int(y[i])
    Sum += y[i]
    pairs.append( (y[i],i+1) )
    i+=1
    
pairs.sort()

print("新增确诊人数最少的一天是2月",pairs[0][1],"日 新增确诊",pairs[0][0],"例。")
print("新增确诊人数最多的一天是2月",pairs[Days-1][1],"日 新增确诊",pairs[Days-1][0],"例。")
print("2月每日新增确诊人数平均值为:",round (1.0*Sum/Days , 0),"人。")
print("2月每日新增确诊人数中位值为:",pairs[Days//2][0],"人")

plt.title("Chart of the number of newly confirmed cases per day in February 2020")
plt.xlabel("Date")
plt.ylabel("Number of newly confirmed cases")

plt.plot(x,y,"-or",label="China")
plt.show()

4. Experiment summary and thinking:

​ This experiment successfully became familiar with the basic syntax of python (input and output sentences, list, tuple and other applications). Successfully became familiar with some drawing functions of Matplotlib library.

​ However, there are still areas worthy of improvement. For example, the overlap of dates appears in the lower right corner of the picture. You can further understand the reason and try to remove the overlapped redundant dates. In addition, this program can be improved to automatically scrape data from the official website to generate images, and try to make a graphical interface to enhance the interactive experience.

Guess you like

Origin blog.csdn.net/George_Plover/article/details/106109202