参考文档文档 : https://gallery.pyecharts.org/#/Map/map_without_label
首先在命令行下载 pyecharts 模块
pip3 install pyecharts
import pyecharts
print(pyecharts.__version__) # 1.9.0
import pyecharts
from pyecharts.charts import Bar
bar = Bar() # 先得到一个表格对象
bar.add_xaxis(["派大星","海绵宝宝","章鱼哥","蟹老板","痞老板"]) # 设置 X 轴
bar.add_yaxis("语文分数",[100,50,40,28,90]) # 以下都是设置 Y 轴
bar.add_yaxis("数学分数",[120,150,80,8,80])
bar.add_yaxis("英语分数",[80,50,50,88,60])
bar.add_yaxis("化学分数",[95,63,70,66,70])
bar.render("./mycharts.html") # render 会生成本地 HTML 文件, 默认是当前目录, 也可以传入路径参数
V1 版本开始支持链式调用, pyecharts所有方法均支持链式调用, 链式调用在魔法方法里介绍过
import pyecharts
from pyecharts.charts import Bar
bar = (
Bar()
.add_xaxis(["派大星","海绵宝宝","章鱼哥","蟹老板","痞老板"])
.add_yaxis("语文分数",[100,50,40,28,90])
.add_yaxis("数学分数",[120,150,80,8,80])
)
bar.render("./mycharts.html")
导入 : options
import pyecharts
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
Bar()
.add_xaxis(["派大星","海绵宝宝","章鱼哥","蟹老板","痞老板"])
.add_yaxis("语文分数",[100,50,40,28,90])
.add_yaxis("数学分数",[120,150,80,8,80])
.add_yaxis("英语分数",[80,50,50,88,60])
.add_yaxis("化学分数",[95,63,70,66,70])
.set_global_opts(title_opts=opts.TitleOpts(title="蟹堡王", subtitle="各科成绩"))
# 或者直接使用字典参数, 使用字典的时候就不需要导入模块
# .set_global_opts(title_opts={"text": "主标题", "subtext": "副标题"})
)
bar.render("./mycharts.html")
pip3 install selenium
模块, 然后下载 chromedriver.exe, 版本需要和谷歌浏览器版本相同, Chrome://version
可查看版本选择合适的版本下载 : http://chromedriver.storage.googleapis.com/index.html
import pyecharts
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
bar = (
Bar()
.add_xaxis(["派大星", "海绵宝宝", "章鱼哥", "蟹老板", "痞老板"])
.add_yaxis("语文分数",[100,50,40,28,90])
.add_yaxis("数学分数",[120,150,80,8,80])
.add_yaxis("英语分数",[80,50,50,88,60])
.add_yaxis("化学分数",[95,63,70,66,70])
.set_global_opts(title_opts={
"text": "蟹堡王", "subtext": "成绩"})
)
make_snapshot(snapshot, bar.render("./mycharts.html"), "./myecharts.png")
提供的主题有很多, 需要导入 ThemeType, 内置主题类型可查看 pyecharts.globals.ThemeType, 也可以在 Pycharm 中使用 .
查看
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
# 内置主题类型可查看 pyecharts.globals.ThemeType
from pyecharts.globals import ThemeType
bar = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS)) # 使用"点"就可以选择
.add_xaxis(["派大星", "海绵宝宝", "章鱼哥", "蟹老板", "痞老板"])
.add_yaxis("语文分数",[100,50,40,28,90])
.add_yaxis("数学分数",[120,150,80,8,80])
.add_yaxis("英语分数",[80,50,50,88,60])
.add_yaxis("化学分数",[95,63,70,66,70])
.set_global_opts(title_opts={
"text": "蟹堡王", "subtext": "成绩"})
)
make_snapshot(snapshot, bar.render("./mycharts.html"), "./myecharts.png")
pyecharts 的简单使用就介绍到这里, 选择合适的图表类型能更直观的体现出数据的变化, 也更有趣, 还能装逼, 快点学起来把