编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

第2天 | 3天搞定Pyecharts数据分析,主题样式

wxchong 2024-08-01 03:27:06 开源技术 11 ℃ 0 评论

Pyecharts提供10多种不同的风格,也提供了便捷的定制主题的方法。主题选择由InitOpts类指定,可选值在from pyecharts.globals import ThemeType中,使用方法如下:

Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))

ThemeType类中,有LIGHT、DARK、WHITE和CHALK等等可选,具体效果,需要你自行测试。

如果内置的主题不能满足你的需求,那你可以到Echarts官网进行构建,自定义自己的主题风格。假设你构建的主题文件为myTheme.js,可用如下方法进行引用。

(1) 将 myTheme.js 放入至 pyecharts-assets/assets/themes 文件夹。

(2) 注册主题到 pyecharts

 from pyecharts.datasets import register_files
 register_files({"myTheme": ["themes/myTheme", "js"]})

(3) 使用主题

c = Bar(init_opts=opts.InitOpts(theme="myTheme"))

程序清单: theme.py

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.globals import ThemeType

columns = ["一月", "二月", "三月", "四月", "五月", "六月"]
data1 = [29.56, 30.65, 18.15, 24.21, 35.23, 66.45]
data2 = [89.12, 24.54, 23.54, 58.45, 68.01, 52.55]
data3 = [69.25, 54.32, 21.65, 35.42, 58.05, 64.50]
bar = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK)).add_xaxis(columns)
        .add_yaxis("手机", data1)
        .add_yaxis("平板", data2).add_yaxis("电脑", data3)
        .set_global_opts(
        # 标题设置
        title_opts=opts.TitleOpts(title="2021年上半年销售数据分析",
                                  pos_left="center",
                                  subtitle="西南区域"),
        # 图例设置
        legend_opts=opts.LegendOpts(
            pos_left="center",
            pos_top="12%",
            orient="horizontal",
        ),
    )

)
bar.render("bar2.html")

运行程序之后,会生成一个bar2.html,用浏览器打开,会看到如下效果:

好了,主题样式的内容就说到这了,关注我,下一节更精彩。

码字不易,你的关注和转发是对我最大的鼓励,谢谢!

一个当了10年技术总监的老家伙,分享多年的编程经验。想学编程的朋友,关注我,你就赚到了。我正在分享Python、前端、Java和App方面的干货了。赶紧来围观啊!!!

#python##程序员##好平台好讲师##Web##热搜#

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表