【Python可视化】pyecharts

Echarts 是一个由百度开源的数据可视化,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可。而 Python 是一门富有表达力的语言,很适合用于数据处理。当数据分析遇上数据可视化时,pyecharts 诞生了。

需要安装

复制代码
pip install pyecharts

下面介绍几种用法,更多用法访问pyecharts使用文档

全局配置项

折线图

python 复制代码
from pyecharts.charts import Line
from pyecharts.options import TitleOpts,LegendOpts,ToolboxOpts,VisualMapOpts
line = Line()
line.add_xaxis(["test1","test2","test3"])
line.add_yaxis("score",[90,85,96])
line.set_global_opts(
    title_opts=TitleOpts(title="三次测试成绩",pos_left="center",pos_bottom="1%"),
    legend_opts=LegendOpts(is_show=True),
    toolbox_opts=ToolboxOpts(is_show=True),
    visualmap_opts=VisualMapOpts(is_show=True)
)
line.render()

地图

python 复制代码
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
map = Map()
data = [
    ("北京市",95),
    ("天津市",89),
    ("上海市",87),
    ("山西省",50),
    ("山东省",45),
    ("河南省",41),
    ("河北省",40),
    ("江苏省",62),
    ("广东省",70)
]
map.add("测试地图",data,"china")
map.set_global_opts(
    visualmap_opts=VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        pieces=[
            {"min":1,"max":50,"label":"1-50","color":"#CCFFFF"},
            {"min":51,"max":80,"label":"51-80","color":"#FF6666"},
            {"min":81,"max":100,"label":"81-100","color":"#990033"}
        ]
    )
)
map.render()

柱状图

python 复制代码
from pyecharts.charts import Bar,Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType

bar1 = Bar()
bar1.add_xaxis(["test1","test2","test3"])
bar1.add_yaxis("score",[90,85,96],label_opts=LabelOpts(position="right"))
bar1.reversal_axis() #反转x轴y轴

bar2 = Bar()
bar2.add_xaxis(["test1","test2","test3"])
bar2.add_yaxis("score",[88,90,85],label_opts=LabelOpts(position="right"))
bar2.reversal_axis()

bar3 = Bar()
bar3.add_xaxis(["test1","test2","test3"])
bar3.add_yaxis("score",[66,77,88],label_opts=LabelOpts(position="right"))
bar3.reversal_axis()

#时间线设置
timeline = Timeline({"theme":ThemeType.LIGHT}) #设置主题颜色
timeline.add(bar1,"高一")
timeline.add(bar2,"高二")
timeline.add(bar3,"高三")
#播放设置
timeline.add_schema(
    play_interval=1500, #播放间隔(ms)
    is_timeline_show=True,
    is_auto_play=True,
    is_loop_play=True
)

timeline.render("Mike高中三年的成绩.html")

项目-动态GDP柱状图

数据来源:Data_1960-2019GDP.csv(本文开头处提供下载)

实现要求:将每一年GDP排名前八国家的数据展现出来

代码示例:

python 复制代码
from pyecharts.charts import Bar,Timeline
from pyecharts.options import *

from pyecharts.globals import ThemeType
#路径按自己存放的写,如果将文件存放在同一文件夹下,可以按照示例写
f = open("1960-2019全球GDP数据.csv","r",encoding="GB2312")
data_lines = f.readlines()
f.close()
#删除第一行数据
data_lines.pop(0)
#将数据转换为字典存储
data_dict = {}
for line in data_lines:
    year = int(line.split(",")[0])
    country = line.split(",")[1]
    gdp = float(line.split(",")[2])
    try:
        data_dict[year].append([country, gdp])
    except:
        data_dict[year] = []
        data_dict[year].append([country, gdp])

timeline = Timeline({"theme":ThemeType.ROMANTIC})
#排序年份
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的国家
    year_data = data_dict[year][0:8]
    x_data = []
    y_data = []
    # for循环每一年的数据,基于每一年的数据,创建每一年的bar对象并将其添加到时间线中
    for country_gdp in year_data:
        x_data.append(country_gdp[0])#x轴添加国家
        y_data.append(country_gdp[1])#y轴添加gdp数据

    #构建柱状图
    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()
    bar.set_global_opts(
        title_opts=TitleOpts(title=f"{year}年全球GDP排名前八国家数据")
    )
    timeline.add(bar,str(year))
    timeline.add_schema(
        play_interval=1000,
        is_timeline_show=True,
        is_auto_play=True,
        is_loop_play=False
    )
timeline.render("1960-2019全球GDP排名前八国家数据.html")

学习更多图形样式展示,可以访问pyecharts-gallery

相关推荐
CP-DD6 分钟前
C/C++ 通用代码模板
c语言·开发语言·c++
188_djh8 分钟前
# 使用python写一个PDF文件转换成word 文件
python·pdf·word·python-docx·pypdf2·python3.9·pdf_to_word.py
李少兄12 分钟前
【Java基础】Java集合遍历方式
java·开发语言·windows
King.62416 分钟前
SQL2API 核心理念:如何重构数据服务交付范式
大数据·开发语言·数据库·人工智能·sql·lua
@正在学习驰骋的小马17 分钟前
一、小白如何用Pygame制作一款跑酷类游戏(成品展示+添加背景图和道路移动效果)
python·游戏·pygame
薯条不要番茄酱23 分钟前
【JavaEE初阶】多线程重点知识以及常考的面试题-多线程进阶(二)
java·开发语言·java-ee
User_芊芊君子34 分钟前
Java与C在典型场景下的性能对比深度剖析
java·c语言·开发语言
KENYCHEN奉孝43 分钟前
一个基于Django的写字楼管理系统实现方案
数据库·python·django·sqlite
alpha xu1 小时前
LLM中的N-Gram、TF-IDF和Word embedding
人工智能·python·语言模型·自然语言处理·sklearn·word2vec
珹洺2 小时前
JSP技术入门指南【一】利用IDEA从零开始搭建你的第一个JSP系统
java·开发语言·前端·html·intellij-idea·jsp