国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【python】數(shù)據(jù)可視化開發(fā)

這篇具有很好參考價(jià)值的文章主要介紹了【python】數(shù)據(jù)可視化開發(fā)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

折線圖

json數(shù)據(jù)格式

  • 一種輕量級(jí)的數(shù)據(jù)交互模式,可以按照J(rèn)SON指定的格式去組織和封裝數(shù)據(jù)
  • JSON本質(zhì)上是一種帶有特定格式的字符串
  • 負(fù)責(zé)不同編程語言中的數(shù)據(jù)傳遞和交互

python數(shù)據(jù)和json數(shù)據(jù)的相互轉(zhuǎn)換

# 導(dǎo)入json模塊

# 準(zhǔn)備符合json格式要求的python數(shù)據(jù)
data=[{"name":"張三","age":16},{"name":"李四","age":20}]
# 通過json.dumps(data)方法把python數(shù)據(jù)轉(zhuǎn)化為json數(shù)據(jù)
data=json.dumps(data,ensure_ascii=False)	# 不使用ASCII碼確保中文可以正常轉(zhuǎn)換
# 通過json.loads(data)方法把json數(shù)據(jù)轉(zhuǎn)化為python數(shù)據(jù)
data=json.loads(data)

pyecharts模塊

  • 做出數(shù)據(jù)可視化效果圖
  • Echarts是由百度開源的數(shù)據(jù)可視化
  • 官方示例https://gallery.pecharts.org/#/README
pip install pyecharts

構(gòu)建基礎(chǔ)折線圖

# 導(dǎo)包,導(dǎo)入Line功能構(gòu)建折線圖對(duì)象
from pyecharts.chart import Line

# 得到折線圖對(duì)象
line=Line()
# 添加x軸數(shù)據(jù)
line.add_xaxis(["中國(guó)","美國(guó)","英國(guó)"])
# 添加y軸數(shù)據(jù)
line.add_yaxis("GDP",[30, 20, 10])
# 生成圖表
line.render()

全局配置選項(xiàng)

set_global_opts方法

全局配置選項(xiàng)可以通過set_global_opts方法來進(jìn)行配置(標(biāo)題、圖例、工具箱)。

line.set_global_opts(
	title_opts=TitleOpts("測(cè)試",pos_left="center",pos_bottom="1%"),	# 標(biāo)題
	legend_opts=LegendOpts(is_show=True),	# 圖例
	toolbox_opts=ToolboxOpts(is_show=True),	# 工具箱
	visualmap_opts=VisualMapOpts(is_show=True),	# 視覺映射
	tooltip_opts=TooltipOpts(is_show=True),
)

數(shù)據(jù)處理

通過json模塊對(duì)數(shù)據(jù)進(jìn)行處理

# 處理數(shù)據(jù)
f_us=open("D:/美國(guó).txt","r",encoding="UTF-8")
us_data=f_us.read()
# 去掉不符合JSON規(guī)范的開頭
us_data=us_data.replace("jsonp_1629344292311_69436(","")
# 去掉不符合JSON規(guī)范的結(jié)尾
us_data=us_data[:-2]
# JSON轉(zhuǎn)python字典
us_dict=json.loads(us_data)
# 獲取trend key
trend_data=us+dict['data'][0]['trend']
# 獲取日期數(shù)據(jù),用于x軸
x_data=trend_data['updateDate'][:314]
# 獲取確診數(shù)據(jù),用于y軸
y_data=trend_data['list'][0]['data'][:314]
# 生成圖表

完成折線圖

from pyecharts.charts import Line
# 生成圖表
line=Line()	#構(gòu)建折線圖對(duì)象
# 添加x軸數(shù)據(jù)
line.add_xaxis(us_x_data)
# 添加y軸數(shù)據(jù)
line.add_yaxis("美國(guó)確診人數(shù)",us_y_data)
line.add_yaxis("日本確診人數(shù)",jp_y_data)
line.add_yaxis("印度確診人數(shù)",in_y_data)
line.render()
# 關(guān)閉文件
f_us.close()
f_jp.close()
f_in.close()

地圖

# 地圖可視化的基本使用
from pyecharts.charts import Map
# 準(zhǔn)備地圖對(duì)象
map=Map()
# 準(zhǔn)備數(shù)據(jù)
data=[
	("北京",99),
	("上海",199),
	("湖南",299),
	("臺(tái)灣",399)]
# 添加數(shù)據(jù)
map.add("測(cè)試地圖",data,"Chiana")
# 繪圖
map.render()

全國(guó)疫情可視化地圖開發(fā)

import json
from pyecharts.charts import Map
from pyecharts.options import *
# 讀取數(shù)據(jù)文件
f=open("D:/疫情.txt","r",encoding="UTF-8")
data=f.read()
f.close()
# 取各省數(shù)據(jù)
# 將字符串json轉(zhuǎn)換為python字典
data_dict=json.loads(data)
# 從字典中取出省份的數(shù)據(jù)
province_data_list=data_dict=["areaTree"][0]["children"]
# 組裝每個(gè)省份和確診人數(shù)為元組,并各個(gè)省的數(shù)據(jù)都封裝入列表內(nèi)
data_list=[]
for province_data in province_data_list:
	province_name=province_data["name"]
	province_confirm=province_data["total"]["confirm"]
	data_list.append((province_name,province_confirm))
# 創(chuàng)建地圖對(duì)象
map=Map()
# 添加數(shù)據(jù)
map.add("各省份確診人數(shù)",data_list,"china")
# 設(shè)置全局配置,定制分段的視覺映射
map.set_global_opts(
	title_opts=TitleOpts(title="全國(guó)疫情地圖"),
	visualmap_opts=VisualMapOpts(
		is_show=True,
		is_piecewise=True,
		pieces=[
			{"min":1,"max":99,"label":"1~99","color":"#CCFFFF"},
			{"min":100,"max":999,"label":"100~999","color":"#FFFF99"},
			{"min":1000,"max":4999,"label":"1000~4999","color":"#FF9966"},
			{"min":5000,"max":9999,"label":"5000~9999","color":"#FF6666"},
			{"min":10000,"max":99999,"label":"10000~99999","color":"#CC3333"},
			{"min":100000,"label":"100000+","color":"#990033"},
		]
	)
)
# 繪圖
map.render("全國(guó)疫情地圖.html")

柱狀圖

基礎(chǔ)柱狀圖

from pyecharts.charts import Bar
# 構(gòu)建柱狀圖對(duì)象
bar=Bar()
# 添加x軸數(shù)據(jù)
bar.add_xaxis(["中國(guó)","美國(guó)","英國(guó)"])
# 添加y軸數(shù)據(jù)
bar.add_yaxis("GDP",[30,20,10])
# 反轉(zhuǎn)x軸和y軸
bar.reversal_axis()
# 繪圖
bar.render("基礎(chǔ)柱狀圖.html")

基礎(chǔ)時(shí)間線柱狀圖

Timeline()時(shí)間線

from pyecharts.charts import Bar,Timeline
from pyecharts.options import *

bar1=Bar()
bar1.add_xaxis(["中國(guó)","美國(guó)","英國(guó)"])
bar1.add_yaxis("GDP",[30,20,10],label_opts=LabelOpts(position="right"))
bar1.reversal_axis()

bar2=Bar()
bar2.add_xaxis(["中國(guó)","美國(guó)","英國(guó)"])
bar2.add_yaxis("GDP",[50,30,20],label_opts=LabelOpts(position="right"))
bar2.reversal_axis()

# 創(chuàng)建時(shí)間線對(duì)象
timeline=Timeline()
# timeline對(duì)象添加bar柱狀圖
timeline.add(bar1,"2021年GDP")
timeline.add(bar2,"2022年GDP")
# 通過時(shí)間線繪圖而不是bar對(duì)象
timeline.render("基礎(chǔ)柱狀圖-時(shí)間線.html")

動(dòng)態(tài)柱狀圖繪制

列表的sort方法

sorted函數(shù)可以對(duì)數(shù)據(jù)容器進(jìn)行排序文章來源地址http://www.zghlxwxcb.cn/news/detail-612684.html

列表.sort(key=選擇排序依據(jù)的函數(shù),reverse=True|False)
  • 參數(shù)key,要求傳入一個(gè)函數(shù),表示將列表的每一個(gè)元素都傳入函數(shù)中,返回排序的依據(jù)
  • 參數(shù)reverse,是否反轉(zhuǎn)排序結(jié)果
帶名函數(shù)形式
# 嵌套列表要求對(duì)外層列表進(jìn)行排序,依據(jù)內(nèi)層列表第二個(gè)元素?cái)?shù)字
# 無法使用sorted函數(shù),可以使用列表的sort方法
my_list=[["a",33],["b",55],["c",11]]
# 定義排序方法
def choose_sort_key(element):
	return element[1]

my_list.sort(key=choose_sort_key,reverse=True)
print(my_list)
匿名lambda形式
my_list=[["a",33],["b",55],["c",11]]
my_list.sort(key=lambda element:element[1],reverse=True)
print(my_list)

數(shù)據(jù)處理

from pyecharts.charts import Bar,Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType
# 讀取數(shù)據(jù)
f=open("D:/1960-2019全球GDP數(shù)據(jù).csv","r",encoding="GB2312")
data_lines=f.readlines()
f.close()
# 刪除第一個(gè)
data_lines.pop(0)
# 將數(shù)據(jù)轉(zhuǎn)換為字典格式
data_dict={}
for line in data_lines:
	year=int(line.split(",")[0])
	country=line.split(",")[1]
	gdp=float(line.split(",")[2])
	try:	# 判斷字典里有沒有指定key
		data_dict[year].append([country,gdp])
	except KeyError:
		data_dict[year].append([country,gdp])

GDP動(dòng)態(tài)圖表繪制

# 創(chuàng)建時(shí)間線對(duì)象
timeline=Timeline({"theme":ThemeType.LIGHT})
# 排序年份
sorted_year_list=sorted(data.dict.keys())
for year in sorted_year_list:
	data_dict[year].sort(key=lambda element:element[1],reverse=True)
	# 取本年前8的國(guó)家
	year_data=data_dict[year][0:8]
	x_data=[]
	y_data=[]
	for country_gdp in year_data:
		x_data.append(country_gdp[0])
		y_data.append(country_gdp[1]/100000000)
	bar=Bar()
	x_data.reverse()
	y_data.reverse()
	bar.add_xaxis(x_data)
	bar.add_yaxis("GDP(億)",y_data,label_opts=LabelOpts(position="right"))
	bar.reversal_axis()
	# 設(shè)置每一年的圖表的標(biāo)題
	bar.set_global_opts(
		title_opts=TitleOpts(title=f"{year}年全球前8GDP數(shù)據(jù)")
	timeline.add(bar,str(year))

# for循環(huán)每年的數(shù)據(jù),創(chuàng)建每年bar對(duì)象
# 將每年bar對(duì)象添加到時(shí)間線中
# 設(shè)置時(shí)間線自動(dòng)播放
timeline.add_schema(
	play_interval=1000,
	is_timeline_show=True,
	is_auto_play=True,
	is_loop_play=False)
timeline.render("1960~2019全球GDP前8國(guó)家.html")

到了這里,關(guān)于【python】數(shù)據(jù)可視化開發(fā)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包