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 did Python pyecharts make the map of traffic congestion?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to draw a map of traffic congestion by Python pyecharts". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to draw a map of traffic congestion by Python pyecharts.

First, climb the congestion index

A certain degree of intelligent transportation provides data on the congestion index of each city, which can be easily crawled with just a few lines of code:

# get the congestion index of each city url = 'https://jiaotong.baidu.com/trafficindex/city/list' # interface api res = requests.get (url) data = res.json ()

Among them, url is the interface address to obtain data, which can be known by simple packet analysis. While data is the returned data, it includes many fields, but we only need to extract the city name and congestion index:

# extraction data citys = [I ['cityname'] for i in data [' data'] ['list']] # extraction City indexs = [float (I [' index']) for i in data ['data'] [' list']] # extract the corresponding index

With the data, we can then visualize it.

Second, data visualization

Use the visual artifact pyecharts library to draw a map, and express the city and the corresponding congestion index. The installation is as follows:

Pip install pyecharts

Some versions need to install additional map libraries as follows:

Pip install echarts-countries-pypkg pip install echarts-cities-pypkg pip install echarts-china-provinces-pypkg pip install echarts-china-cities-pypkg

First define the map:

Geo = Geo () geo.add_schema (maptype = 'china') # join the map of China

Add data and make related settings:

Geo.add ('city congestion index', zip (citys,indexs), type_ = 'effectScatter') # sets the map type and data geo.set_series_opts (label_opts = opts.LabelOpts (is_show = False)) # sets whether to display labels

According to the size of the congestion index, they are smooth, slow, congestion and serious congestion:

Geo.set_global_opts (visualmap_opts = opts.VisualMapOpts (# max_ = 2.5,# is used to continuously express is_piecewise = True, # whether to segment pieces = [{'min':1.0,'max':1.5,'label':' unblocked', 'color':'#16CE95'}) {'min':1.5,'max':1.8,'label':' slow', 'color':'#F79D06'}, {' min':1.8,'max':2.0,'label':' congestion', 'color':'#D80304'}, {' min':2.0' 'serious max':2.5,'label':' congestion', 'color':'#8F0921'}])) # setting legend display

Finally, save the map locally:

Geo.render (path=' city congestion index .html')

At this point, we got the picture we saw at the beginning of the article.

However, because the congestion data changes in real time, wouldn't it be troublesome if I had to run the code every time?

Obviously, witty socialist youth will not do this, you move on.

Third, set up a display website

In order to show the congestion in various cities more easily, I decided to set up a website for display.

In the code, get_geo () returns the map drawn by pyecharts as a function of getting the map. Create a templates folder under the current directory, and create the module file geo.html, as follows:

Urban traffic congestion index {{mygeo | safe}} Thank you for your reading. The above is the content of "how to draw a map of traffic congestion by Python pyecharts". After the study of this article, I believe you have a deeper understanding of how to draw a map of traffic congestion in Python pyecharts, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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