展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

pyecharts基础使用

# 打开cmd
C:\Users\ychen>pip install pyecharts
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting pyecharts
  Downloading https://mirrors.aliyun.com/pypi/packages/2a/7e/bd0d5e87d4077c89294f48e2452bb64677f7183ba688d29220645193c197/pyecharts-2.0.5-py3-none-any.whl (146 kB)
     |████████████████████████████████| 146 kB 327 kB/s
Collecting simplejson
  Downloading https://mirrors.aliyun.com/pypi/packages/7f/24/14885a14bd3c2ac8dfffa16ae77de7a69be3205bd00b53a68f7e531e231d/simplejson-3.19.2-cp36-cp36m-win_amd64.whl (76 kB)
     |████████████████████████████████| 76 kB 106 kB/s
Collecting prettytable
  Downloading https://mirrors.aliyun.com/pypi/packages/9e/6d/40a24eaa03ea4418129708fd3f0f17eda73d568f16d4d4fd412566168b4c/prettytable-2.5.0-py3-none-any.whl (24 kB)
Requirement already satisfied: jinja2 in c:\programdata\anaconda3\lib\site-packages (from pyecharts) (3.0.3)
Requirement already satisfied: MarkupSafe>=2.0 in c:\programdata\anaconda3\lib\site-packages (from jinja2->pyecharts) (2.0.1)
Requirement already satisfied: wcwidth in c:\programdata\anaconda3\lib\site-packages (from prettytable->pyecharts) (0.1.7)
Requirement already satisfied: importlib-metadata in c:\programdata\anaconda3\lib\site-packages (from prettytable->pyecharts) (4.8.3)
Requirement already satisfied: typing-extensions>=3.6.4 in c:\programdata\anaconda3\lib\site-packages (from importlib-metadata->prettytable->pyecharts) (4.1.1)
Requirement already satisfied: zipp>=0.5 in c:\programdata\anaconda3\lib\site-packages (from importlib-metadata->prettytable->pyecharts) (3.6.0)
Installing collected packages: simplejson, prettytable, pyecharts
Successfully installed prettytable-2.5.0 pyecharts-2.0.5 simplejson-3.19.2
  • 新建一个后缀为ipynb的文件
import pyecharts

# 查看版本
print(pyecharts.__version__)
  • 绘制柱状图
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
bar.render()
  • render.html
点击查看详情

  • 链式调用写法
from pyecharts.charts import Bar

bar = (
    Bar()
    .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
    .add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
)
bar.render()
  • render.html
点击查看详情

  • 配置项
from pyecharts.charts import Bar
from pyecharts import options as opts

# V1 版本开始支持链式调用
# 你所看到的格式其实是 `black` 格式化以后的效果
# 可以执行 `pip install black` 下载使用
bar = (
    Bar()
    .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
    .add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
    .set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
    # 或者直接使用字典参数
    # .set_global_opts(title_opts={"text": "主标题", "subtext": "副标题"})
)
bar.render()

# 不习惯链式调用的开发者依旧可以单独调用方法
bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
bar.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
bar.render()
  • render.html
点击查看详情

  • 安装依赖
C:\Users\ychen>pip install snapshot-selenium
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting snapshot-selenium
  Downloading https://mirrors.aliyun.com/pypi/packages/64/d9/89e6835708320f8700246a5683133bf99f2743eaaa218e6f36f31fb7f7e7/snapshot_selenium-0.0.2-py2.py3-none-any.whl (3.0 kB)
Requirement already satisfied: selenium in c:\programdata\anaconda3\lib\site-packages (from snapshot-selenium) (3.141.0)
Requirement already satisfied: urllib3 in c:\programdata\anaconda3\lib\site-packages (from selenium->snapshot-selenium) (1.26.18)
Installing collected packages: snapshot-selenium
Successfully installed snapshot-selenium-0.0.2
  • 渲染成图片
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot

# 使用 snapshot-selenium 渲染图片
from snapshot_selenium import snapshot

bar = (
    Bar()
    .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
    .add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
)
make_snapshot(snapshot, bar.render(), "bar.png")
  • bar.png
点击查看详情

  • 使用主题
from pyecharts.charts import Bar
from pyecharts import options as opts
# 内置主题类型可查看 pyecharts.globals.ThemeType
from pyecharts.globals import ThemeType
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot

bar = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
    .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
    .add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
    .add_yaxis("商家B", [15, 6, 45, 20, 35, 66])
    .set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
)

make_snapshot(snapshot, bar.render(), "bar.png")
  • bar.png
点击查看详情

posted @ 2024-05-06 10:56  DogLeftover  阅读(7)  评论(0编辑  收藏  举报