当前位置:网站首页>Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map
Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map
2022-07-02 03:43:00 【Squirrels love cookies】
Preface
The weather has been unusually hot recently , I saw that the surface temperature in some places actually reached 70°, That's ridiculous
So I want to collect weather data , Make a visualization , Recall the weather last year
development environment
- python 3.8 Run code
- pycharm 2021.2 Auxiliary knock code
- requests Third-party module
Weather data collection
1. Send a request
url = 'https://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D=54511&areaInfo%5BareaType%5D=2&date%5Byear%5D=2022&date%5Bmonth%5D=5'
response = requests.get(url)
print(response)
return <Response [200]>: The request is successful
2. get data
print(response.json())
3. Parsing data Extract the weather information
Structured data analysis :Python Dictionary values
Unstructured data analysis : Web page structure
json_data = response.json()
html_data = json_data['data']
select = parsel.Selector(html_data)
trs = select.css('table tr')
for tr in trs[1:]:
# Web page structure
# html Webpage <td>asdfwaefaewfweafwaef</td> <a></a> <div></div>
# ::text: I need this The text content in the tag
td = tr.css('td::text').getall()
print(td)
4. Save the data
with open(' Weather data .csv', encoding='utf-8', mode='a', newline='') as f:
csv_writer = csv.writer(f)
csv_writer.writerow(td)
Data visualization
Reading data
data = pd.read_csv(' Weather data .csv')
data
Split date / week
data[[' date ',' week ']] = data[' date '].str.split(' ',expand=True,n=1)
data
Remove redundant characters
data[[' maximum temperature ',' Minimum temperature ']] = data[[' maximum temperature ',' Minimum temperature ']].apply(lambda x: x.str.replace('°',''))
data.head()
To the north, to the wide, to the deep 2021 year 10 Monthly synoptic thermodynamic map distribution
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import seaborn as sns
# Set global default font by Elegant black
plt.rcParams['font.family'] = ['Microsoft YaHei']
# Set global axis label dictionary size
plt.rcParams["axes.labelsize"] = 14
# Set the background
sns.set_style("darkgrid",{
"font.family":['Microsoft YaHei', 'SimHei']})
# Set canvas length and width and dpi
plt.figure(figsize=(18,8),dpi=100)
# Custom color card
cmap = mcolors.LinearSegmentedColormap.from_list("n",['#95B359','#D3CF63','#E0991D','#D96161','#A257D0','#7B1216'])
# Draw a heat map
ax = sns.heatmap(data_pivot, cmap=cmap, vmax=30,
annot=True, # The values shown on the heat map
linewidths=0.5,
)
# take x The axis scale is placed on the top
ax.xaxis.set_ticks_position('top')
plt.title(' Beijing recently 10 Monthly weather distribution ',fontsize=16) # Picture title text and font size
plt.show()
Beijing 2021 Annual daily maximum and minimum temperature change
color0 = ['#FF76A2','#24ACE6']
color_js0 = """new echarts.graphic.LinearGradient(0, 1, 0, 0, [{offset: 0, color: '#FFC0CB'}, {offset: 1, color: '#ed1941'}], false)"""
color_js1 = """new echarts.graphic.LinearGradient(0, 1, 0, 0, [{offset: 0, color: '#FFFFFF'}, {offset: 1, color: '#009ad6'}], false)"""
tl = Timeline()
for i in range(0,len(data_bj)):
coordy_high = list(data_bj[' maximum temperature '])[i]
coordx = list(data_bj[' date '])[i]
coordy_low = list(data_bj[' Minimum temperature '])[i]
x_max = list(data_bj[' date '])[i]+datetime.timedelta(days=10)
y_max = int(max(list(data_bj[' maximum temperature '])[0:i+1]))+3
y_min = int(min(list(data_bj[' Minimum temperature '])[0:i+1]))-3
title_date = list(data_bj[' date '])[i].strftime('%Y-%m-%d')
c = (
Line(
init_opts=opts.InitOpts(
theme='dark',
# Animate
animation_opts=opts.AnimationOpts(animation_delay_update=800),#(animation_delay=1000, animation_easing="elasticOut"),
# Set width 、 Height
width='1500px',
height='900px', )
)
.add_xaxis(list(data_bj[' date '])[0:i])
.add_yaxis(
series_name="",
y_axis=list(data_bj[' maximum temperature '])[0:i], is_smooth=True,is_symbol_show=False,
linestyle_opts={
'normal': {
'width': 3,
'shadowColor': 'rgba(0, 0, 0, 0.5)',
'shadowBlur': 5,
'shadowOffsetY': 10,
'shadowOffsetX': 10,
'curve': 0.5,
'color': JsCode(color_js0)
}
},
itemstyle_opts={
"normal": {
"color": JsCode(
"""new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: '#ed1941' }, { offset: 1, color: '#009ad6' }], false)"""
),
"barBorderRadius": [45, 45, 45, 45],
"shadowColor": "rgb(0, 160, 221)",
}
},
)
.add_yaxis(
series_name="",
y_axis=list(data_bj[' Minimum temperature '])[0:i], is_smooth=True,is_symbol_show=False,
# linestyle_opts=opts.LineStyleOpts(color=color0[1],width=3),
itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_js1)),
linestyle_opts={
'normal': {
'width': 3,
'shadowColor': 'rgba(0, 0, 0, 0.5)',
'shadowBlur': 5,
'shadowOffsetY': 10,
'shadowOffsetX': 10,
'curve': 0.5,
'color': JsCode(color_js1)
}
},
)
.set_global_opts(
title_opts=opts.TitleOpts(" Beijing 2021 Annual daily maximum and minimum temperature change \n\n{}".format(title_date),pos_left=330,padding=[30,20]),
xaxis_opts=opts.AxisOpts(type_="time",max_=x_max),#, interval=10,min_=i-5,split_number=20,axistick_opts=opts.AxisTickOpts(length=2500),axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(color="grey"))
yaxis_opts=opts.AxisOpts(min_=y_min,max_=y_max),# Axis color ,axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(color="grey"))
)
)
tl.add(c, "{}".format(list(data_bj[' date '])[i]))
tl.add_schema(
axis_type='time',
play_interval=100, # Indicates the playback speed
pos_bottom="-29px",
is_loop_play=False, # Loop or not
width="780px",
pos_left='30px',
is_auto_play=True, # Auto play or not .
is_timeline_show=False)
tl.render_notebook()
To the north, to the wide, to the deep 10 The daily maximum temperature changes in January
# Background color
background_color_js = (
"new echarts.graphic.LinearGradient(0, 0, 0, 1, "
"[{offset: 0, color: '#c86589'}, {offset: 1, color: '#06a7ff'}], false)"
)
# Line style
linestyle_dic = {
'normal': {
'width': 4,
'shadowColor': '#696969',
'shadowBlur': 10,
'shadowOffsetY': 10,
'shadowOffsetX': 10,
}
}
timeline = Timeline(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js),
width='980px',height='600px'))
bj, gz, sh, sz= [], [], [], []
all_max = []
x_data = data_10[data_10[' City '] == ' Beijing '][' Japan '].tolist()
for d_time in range(len(x_data)):
bj.append(data_10[(data_10[' Japan '] == x_data[d_time]) & (data_10[' City ']==' Beijing ')][" maximum temperature "].values.tolist()[0])
gz.append(data_10[(data_10[' Japan '] == x_data[d_time]) & (data_10[' City ']==' Guangzhou ')][" maximum temperature "].values.tolist()[0])
sh.append(data_10[(data_10[' Japan '] == x_data[d_time]) & (data_10[' City ']==' Shanghai ')][" maximum temperature "].values.tolist()[0])
sz.append(data_10[(data_10[' Japan '] == x_data[d_time]) & (data_10[' City ']==' Shenzhen ')][" maximum temperature "].values.tolist()[0])
line = (
Line(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js),
width='980px',height='600px'))
.add_xaxis(
x_data,
)
.add_yaxis(
' Beijing ',
bj,
symbol_size=5,
is_smooth=True,
is_hover_animation=True,
label_opts=opts.LabelOpts(is_show=False),
)
.add_yaxis(
' Guangzhou ',
gz,
symbol_size=5,
is_smooth=True,
is_hover_animation=True,
label_opts=opts.LabelOpts(is_show=False),
)
.add_yaxis(
' Shanghai ',
sh,
symbol_size=5,
is_smooth=True,
is_hover_animation=True,
label_opts=opts.LabelOpts(is_show=False),
)
.add_yaxis(
' Shenzhen ',
sz,
symbol_size=5,
is_smooth=True,
is_hover_animation=True,
label_opts=opts.LabelOpts(is_show=False),
)
.set_series_opts(linestyle_opts=linestyle_dic)
.set_global_opts(
title_opts=opts.TitleOpts(
title=' To the north, to the wide, to the deep 10 Monthly maximum temperature change trend ',
pos_left='center',
pos_top='2%',
title_textstyle_opts=opts.TextStyleOpts(color='#DC143C', font_size=20)),
tooltip_opts=opts.TooltipOpts(
trigger="axis",
axis_pointer_type="cross",
background_color="rgba(245, 245, 245, 0.8)",
border_width=1,
border_color="#ccc",
textstyle_opts=opts.TextStyleOpts(color="#000"),
),
xaxis_opts=opts.AxisOpts(
# axislabel_opts=opts.LabelOpts(font_size=14, color='red'),
# axisline_opts=opts.AxisLineOpts(is_show=True,
# linestyle_opts=opts.LineStyleOpts(width=2, color='#DB7093'))
is_show = False
),
yaxis_opts=opts.AxisOpts(
name=' The highest temperature ',
is_scale=True,
# min_= int(min([gz[d_time],sh[d_time],sz[d_time],bj[d_time]])) - 10,
max_= int(max([gz[d_time],sh[d_time],sz[d_time],bj[d_time]])) + 10,
name_textstyle_opts=opts.TextStyleOpts(font_size=16,font_weight='bold',color='#5470c6'),
axislabel_opts=opts.LabelOpts(font_size=13,color='#5470c6'),
splitline_opts=opts.SplitLineOpts(is_show=True,
linestyle_opts=opts.LineStyleOpts(type_='dashed')),
axisline_opts=opts.AxisLineOpts(is_show=True,
linestyle_opts=opts.LineStyleOpts(width=2, color='#5470c6'))
),
legend_opts=opts.LegendOpts(is_show=True, pos_right='1%', pos_top='2%',
legend_icon='roundRect',orient = 'vertical'),
))
timeline.add(line, '{}'.format(x_data[d_time]))
timeline.add_schema(
play_interval=1000, # Rotation speed
is_timeline_show=True, # Whether or not shown timeline Components
is_auto_play=True, # Auto play or not
pos_left="0",
pos_right="0"
)
timeline.render_notebook()
边栏推荐
- Analyse de 43 cas de réseaux neuronaux MATLAB: Chapitre 42 opérations parallèles et réseaux neuronaux - - opérations parallèles de réseaux neuronaux basées sur CPU / GPU
- Review materials of project management PMP high frequency examination sites (8-1)
- In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
- 【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真
- [untitled] basic operation of raspberry pie (2)
- [ibdfe] matlab simulation of frequency domain equalization based on ibdfe
- Basic operations of MySQL database (based on tables)
- 一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起
- 《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮
- Getting started with MQ
猜你喜欢
Pycharm2021 delete the package warehouse list you added
One of the future trends of SAP ui5: embrace typescript
How should the team choose the feature branch development mode or trunk development mode?
Vite: configure IP access
Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini
The 7th Blue Bridge Cup single chip microcomputer provincial competition
近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
蓝桥杯单片机省赛第十届
Interface debugging tool simulates post upload file - apipost
随机推荐
SQL Yiwen get window function
Exchange rate query interface
蓝桥杯单片机省赛第十一届第一场
What do you know about stock selling skills and principles
潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
微信小程序中 在xwml 中使用外部引入的 js进行判断计算
Welcome the winter vacation multi school league game 2 partial solution (B, C, D, F, G, H)
Kotlin基础学习 17
蓝桥杯单片机第六届温度记录器
The first practical project of software tester: web side (video tutorial + document + use case library)
Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)
Getting started with MQ
NLog使用
Introduction to Robotics II. Forward kinematics, MDH method
《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真
蓝桥杯单片机第四届省赛
MD5 of Oracle
Network connection mode of QT
[personal notes] PHP common functions - custom functions