大家好,我是 ?? 【Python當(dāng)打之年(點(diǎn)擊跳轉(zhuǎn))】
本期利用 python 的 pyecharts 可視化庫繪制 北京市歷史天氣數(shù)據(jù),看看 歷史高溫、歷史低溫分布以及白天、夜晚的風(fēng)力、風(fēng)向分布等情況,希望對(duì)大家有所幫助,如有疑問或者需要改進(jìn)的地方可以聯(lián)系小編。
?????? 1. 導(dǎo)入模塊
import pandas as pd
from pyecharts.charts import Line
from pyecharts.charts import Bar
from pyecharts.charts import Scatter
from pyecharts.charts import Pie
from pyecharts.charts import EffectScatter
from pyecharts.charts import Calendar
from pyecharts.charts import Polar
from pyecharts import options as opts
import warnings
warnings.filterwarnings('ignore')
?????? 2. Pandas數(shù)據(jù)處理
2.1 讀取數(shù)據(jù)
df_weather = pd.read_excel('./2018-2022年天氣數(shù)據(jù).xlsx')
2018-2022年五年的歷史天氣數(shù)據(jù)共1839條。
2.2 處理最低氣溫最高氣溫?cái)?shù)據(jù)
df_weather_1 = df_weather.copy()
df_weather_1[['最低氣溫','最高氣溫']] = df_weather_1['最低氣溫/最高氣溫'].str.split(' / ',expand=True)
df_weather_1['最低氣溫'] = df_weather_1['最低氣溫'].str[:-2]
df_weather_1['最高氣溫'] = df_weather_1['最高氣溫'].str[:-1]
df_weather_1['最低氣溫'] = df_weather_1['最低氣溫'].astype('int')
df_weather_1['最高氣溫'] = df_weather_1['最高氣溫'].astype('int')
2.3 處理日期數(shù)據(jù)
df_weather_1['日期'] = pd.to_datetime(df_weather_1['日期'],format='%Y年%m月%d日')
df_weather_1['日期s'] = df_weather_1['日期'].dt.strftime('%Y/%m/%d')
2.4 處理風(fēng)力風(fēng)向數(shù)據(jù)
?????? 3. Pyecharts數(shù)據(jù)可視化
3.1 2018-2022年歷史溫度分布
def get_scatter():
scatter = (
Scatter()
.add_xaxis(x_data)
.add_yaxis("最低氣溫", y_data1)
.add_yaxis("最高氣溫", y_data2)
.set_global_opts(
legend_opts=opts.LegendOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(
is_show=False,
range_color=range_color
),
title_opts=opts.TitleOpts(
title='1-2018-2022年歷史溫度分布',
pos_top='1%',
pos_left="1%",
)
)
)
3.2 2022年歷史溫度分布
歷史最高溫度39℃,歷史最低溫度-12℃。
3.3 2021年歷史溫度分布
def get_calendar():
calendar = (
Calendar()
.add('',
data_21,
calendar_opts=opts.CalendarOpts(
pos_right='5%',
range_='2021',
daylabel_opts=opts.CalendarDayLabelOpts(name_map='cn'),
monthlabel_opts=opts.CalendarMonthLabelOpts(name_map='cn')
),
)
.set_global_opts(
title_opts=opts.TitleOpts(
title='3-2021年歷史溫度分布',
pos_top='1%',
pos_left="1%",
),
visualmap_opts=opts.VisualMapOpts(
range_color=range_color,
)
)
)
3.4 2019年歷史溫度分布
3.5 2022年夜間_白天風(fēng)力分布
def get_pie():
pie = (
Pie()
.add(
"",
[list(z) for z in zip(x_data, y_data1)],
radius=["30%", "50%"],
center=["30%", "55%"],
)
.add(
"",
[list(z) for z in zip(x_data, y_data2)],
radius=["30%", "50%"],
center=["70%", "55%"],
)
.set_global_opts(
title_opts=opts.TitleOpts(
title='5-2022年夜間_白天風(fēng)力分布',
pos_top='1%',
pos_left="1%",
),
legend_opts=opts.LegendOpts(pos_top='10%'),
visualmap_opts=opts.VisualMapOpts(
is_show=False,
range_color=range_color
),
)
)
3.6 2022年夜間風(fēng)向分布
def get_polor():
polor = (
Polar()
.add("", values,type_="bar")
.set_global_opts(
legend_opts=opts.LegendOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(
is_show=False,
range_color=range_color
),
title_opts=opts.TitleOpts(
title='6-2022年夜間風(fēng)向分布',
pos_top='1%',
pos_left="1%",
),
)
)
3.7 2022年白天風(fēng)向分布
3.8 2018-2022年各類型天氣數(shù)量
def get_bar():
bar = (
Bar()
.add_xaxis(x_data)
.add_yaxis("",y_data)
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(
is_show=False,
range_color=range_color
),
legend_opts=opts.LegendOpts(is_show=False),
title_opts=opts.TitleOpts(
title='8-2018-2022年各類型天氣數(shù)量',
pos_top='1%',
pos_left="1%",
),
)
)
3.9 2018-2022年每月平均最高溫度
?????? 可視化項(xiàng)目源碼+數(shù)據(jù)
點(diǎn)擊跳轉(zhuǎn):【全部可視化項(xiàng)目源碼+數(shù)據(jù)】文章來源:http://www.zghlxwxcb.cn/news/detail-519227.html
以上就是本期為大家整理的全部?jī)?nèi)容了,趕快練習(xí)起來吧,原創(chuàng)不易,喜歡的朋友可以點(diǎn)贊、收藏也可以分享(注明出處)讓更多人知道。文章來源地址http://www.zghlxwxcb.cn/news/detail-519227.html
到了這里,關(guān)于Pandas+Pyecharts | 北京近五年歷史天氣數(shù)據(jù)可視化的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!