json 數(shù)據(jù)格式
不同語言之間若要傳輸數(shù)據(jù),傳送過去的數(shù)據(jù)可能無法直接處理,因此規(guī)定一種格式,保證雙方都能夠處理,也就是 json 數(shù)據(jù)格式,一種擁有特殊格式的字符串,json 數(shù)據(jù)格式和 Python 語言中的字典格式非常像。
列表轉(zhuǎn) json:
字典轉(zhuǎn) json:
將 json 字符串轉(zhuǎn) python 列表:
結(jié)合前面的文件操作就可以將文件里的 json 數(shù)據(jù)轉(zhuǎn)換成字典或列表等,繪制圖表內(nèi)容。
pyecharts 模塊
pyecharts 是一個(gè)基于 ECharts 的 Python 數(shù)據(jù)可視化庫,它允許用戶使用 Python 語言生成各種類型的交互式圖表和數(shù)據(jù)可視化。
https://gallery.pyecharts.org/#/README 包含了各種圖表,可點(diǎn)擊進(jìn)去查看想要?jiǎng)?chuàng)建圖表的 python 代碼。
繪制折線圖
首先需要安裝 pyecharts:pip install pyecharts
# 導(dǎo)入 pyecharts 下的 Line 構(gòu)建折線圖對象
from pyecharts.charts import Line
from pyecharts.options import TitleOpts, LegendOpts, VisualMapOpts, ToolboxOpts
# 得到折線圖對象
line = Line()
# 添加 x 軸數(shù)據(jù)
line.add_xaxis(["中國", "美國", "俄國"])
# 添加 y 軸數(shù)據(jù)
line.add_yaxis("GDP", [30, 20, 10])
# 設(shè)置全局配置項(xiàng)
line.set_global_opts(
# 標(biāo)題設(shè)置
title_opts=TitleOpts(title="GDP展示", pos_left="center", pos_bottom="1%"),
# 圖例
legend_opts=LegendOpts(is_show=True),
# 顯示工具箱
toolbox_opts=ToolboxOpts(is_show=True),
# 視覺映射,鼠標(biāo)移動出現(xiàn)效果
visualmap_opts=VisualMapOpts(is_show=True),
)
# 生成圖表, 在 折線圖.html 文件中,文件在當(dāng)前項(xiàng)目中
line.render("折線圖.html")
運(yùn)行結(jié)果:
繪制地圖
# 地圖可視化
from pyecharts.charts import Map, Bar
from pyecharts.options import VisualMapOpts, LabelOpts
# 準(zhǔn)備地圖對象
map = Map()
# 準(zhǔn)備數(shù)據(jù)
data = [
("北京市", 99),
("上海市", 199),
("湖南省", 299),
("臺灣省", 399),
("廣東省", 499)
]
# 添加數(shù)據(jù)
# label_opts=LabelOpts(is_show=False) 每個(gè)省市是否都顯示名字
map.add("測試地圖", data, "china",label_opts=LabelOpts(is_show=False))
# 設(shè)置全局配置
map.set_global_opts(
visualmap_opts=VisualMapOpts(
is_show=True,
# 顏色是否分段顯示
is_piecewise=True,
pieces=[
{"min":1,"max":9,"label":"1-9","color":"#CCFFFF"},
{"min":10,"max":99,"label":"10-99","color":"#FF6666"},
{"min":100,"max":500,"label":"100-500","color":"#990033"}
]
)
)
# 繪圖
map.render("地圖.html")
文章來源:http://www.zghlxwxcb.cn/news/detail-846889.html
更多 pyecharts 模塊可以參考:https://pyecharts.org/#/zh-cn/intro文章來源地址http://www.zghlxwxcb.cn/news/detail-846889.html
到了這里,關(guān)于【Python】繪制簡單圖表的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!