Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Visualization Operation in Python

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to achieve visualization operation in Python, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Python provides a csv module to read and write csv files. Because the format of the csv file itself is relatively simple (usually the first row is the header, which explains the meaning of each column of data, and then each row represents a row of data), it is also very easy to read the csv file using the csv module:

Create a reader for the csv module.

A loop calls the next () method of the csv reader to read the contents of the csv file line by line. The next () method returns a list list representing a row of data, and each element of the list list represents a cell data.

This section uses the csv file of the 2017 Guangzhou weather data (the data are from the http://lishi.tianqi.com/ website. The following program demonstrates the use of a csv reader to read two lines of a csv file.

Import csvfilename = 'guangzhou-2017.csv'# Open file with open (filename) as f: # create cvs file reader reader = csv.reader (f) # read the first line, which is the header data. Header_row = next (reader) print (header_row) # reads the second row, which is the real data. First_row = next (reader) print (first_row)

Line 7 of the above program creates the CSV reader, and lines 9 and 12 each read one line of the file, where line 7 returns the header data of the csv file; line 9 returns the real data. When you run the above program, you can see the following output:

['Date',' Max TemperatureC', 'Min TemperatureC',' Description', 'WindDir',' WindForce'] ['2017-1-1','24','13', 'sunny', 'southwest wind', 'force 1']

As you can see from the output above, each line of the file contains six data, namely, date, maximum temperature, minimum temperature, weather conditions, wind direction, wind force.

After mastering the usage of the csv reader, the following program will use Matplotlib to visually display the maximum and minimum temperatures in Guangzhou in July 2017. the visualization part of the code is worth paying attention to!

Import csvfrom datetime import datetimefrom matplotlib import pyplot as pltfilename = 'guangzhou-2017.csv'# Open file with open (filename) as f: # create cvs file reader reader = csv.reader (f) # read the first line, which is the header data. Header_row = next (reader) print (header_row) # define read start date start_date = datetime (2017, 6, 30) # define end date end_date = datetime (2017, 8, 1) # define three list lists as data dates, highs, lows = [], [] [] for row in reader: # format the value of the first column as date d = datetime.strptime (row [0],'% Ymuri% MMI% d') # only show the data if start_date of July 2017

< d < end_date: dates.append(d) highs.append(int(row[1])) lows.append(int(row[2]))# 配置图形fig = plt.figure(dpi=128, figsize=(12, 9))# 绘制最高气温的折线plt.plot(dates, highs, c='red', label='最高气温', alpha=0.5, linewidth = 2.0, linestyle = '-', marker='v')# 再绘制一条折线plt.plot(dates, lows, c='blue', label='最低气温', alpha=0.5, linewidth = 3.0, linestyle = '-.', marker='o')# 为两个数据的绘图区域填充颜色plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.1)# 设置标题plt.title("广州2017年7月最高气温和最低气温")# 为两条坐标轴设置名称plt.xlabel("日期")# 该方法绘制斜着的日期标签fig.autofmt_xdate()plt.ylabel("气温(℃)")# 显示图例plt.legend()ax = plt.gca()# 设置右边坐标轴线的颜色(设置为none表示不显示)ax.spines['right'].set_color('none')# 设置顶部坐标轴线的颜色(设置为none表示不显示)ax.spines['top'].set_color('none')plt.show() 上面程序的前半部分代码用于从 csv 文件中读取 2017 年 7 月广州的气温数据,程序分别使用了 dates、highs 和 lows 三个 list 列表来保存日期、最高气温、最低气温。 程序的后半部分代码绘制了两条折线来显示最高气温和最低气温,其中第 31 行代码用于绘制最高气温,第 34 行代码用于绘制最低气温;第 37 行代码控制在两条折线之间填充颜色。程序也对坐标轴、图例进行了简单的设置。运行上面程序,可以看到如图 1 所示的折线图。

The above is how to achieve visual operations in Python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report