Python+Pyecharts:快速构建各种数据可视化图表❥满足你的一切常用需求❥

Python+Pyecharts:快速构建各种数据可视化图表❥满足你的一切常用需求❥,第1张

Python+Pyecharts:快速构建各种数据可视化图表❥满足你的一切常用需求❥

做一个写出‘有用’代码的人!Echarts 是原百度出品,现已移交Apache开源组织管理,使得其更加强大,那么怎么用呢,下面就用一下最常用的几个图表,几行代码就可以,看就完事了!

首先安装模块:pip install pyecharts ,由于生成的是html页面,所以我在这里引入了一个html转图片的工具imgkit,先下载exe程序,再安装模块:pip install imgkit,如果不需要转图片,可以忽略!

这里使用的是pyecharts自带的演示数据集Faker,choose()随机生成一个列表,列表里的是同一类事务,values()生成一个默认20,150之间随机数的一个列表,列表长度均为7!

常用11个
  • 1.直方图
  • 2.折线图
  • 3.饼图
  • 4.漏斗图
  • 5.仪表盘图
  • 6.联动图
  • 7.水球图
  • 8.分栏显示图
  • 9.表格图
  • 10.时间线图
  • 11.词云图
  • 12.完整代码

各个模块,在这里先导入,下面是各个函数,最后main方法调用,生成各种图表

from pyecharts.charts import Bar
from pyecharts.charts import Line
from pyecharts.charts import Pie
from pyecharts.charts import Funnel
from pyecharts.charts import Gauge
from pyecharts.charts import Grid
from pyecharts.charts import Liquid
from pyecharts.charts import Tab
from pyecharts.components import Table
from pyecharts.globals import ThemeType
from pyecharts.charts import Timeline
from pyecharts.charts import WordCloud
from pyecharts import options as opts
import imgkit
from pyecharts.faker import Faker
1.直方图
def bar_chart() -> Bar:
    bar = (
        Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))
            .add_xaxis(Faker.choose())
            .add_yaxis('A', Faker.values())
            .add_yaxis('B', Faker.values())
            .set_global_opts(title_opts=opts.TitleOpts(title='销售Bar图'))
    )
    return bar

2.折线图
def line_chart() -> Line:
    line = (
        Line()
            .add_xaxis(Faker.choose())
            .add_yaxis('A', Faker.values())
            .add_yaxis('B', Faker.values())
            .set_global_opts(title_opts=opts.TitleOpts(title='销售line图'))
    )
    return line

3.饼图
def pie_chart() -> Pie:
    pie = (
        Pie()
            .add('', [list(item) for item in zip(Faker.choose(), Faker.values())])
            .set_global_opts(title_opts=opts.TitleOpts(title='销售pie图'))
            .set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{c}'))
    )
    return pie

4.漏斗图
def funnel_chart() -> Funnel:
    funnel = (
        Funnel()
            .add('', [list(x) for x in zip(Faker.choose(), Faker.values())])
            .set_global_opts(title_opts=opts.TitleOpts(title='销售funnel图'))
    )
    return funnel

5.仪表盘图
def gauge_chart() -> Gauge:
    gauge = (
        Gauge()
            .add(series_name='当前测速', data_pair=[['speed', 60]])
            .set_global_opts(
            legend_opts=opts.LegendOpts(is_show=False),
            tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{a} 
{b} : {c}km/h") ) ) return gauge

6.联动图
def grid_chart() -> Grid:
    grid = (
        Grid()
            .add(bar_chart(), grid_opts=opts.GridOpts(pos_top='60%'))
            .add(line_chart(), grid_opts=opts.GridOpts(pos_bottom='60%'))
    )
    return grid

7.水球图
def liquid_chart() -> Liquid:
    liquid = (
        Liquid()
            .add('', [0.6], is_outline_show=False)
            .set_global_opts(title_opts=opts.TitleOpts(title='liquid图'))
    )
    return liquid

8.分栏显示图
def tab_chart() -> Tab:
    tabs = Tab()
    tabs.add(bar_chart(), 'bar page')
    tabs.add(line_chart(), 'line page')
    tabs.add(pie_chart(), 'pie page')
    return tabs

9.表格图
def table_chart() -> Table:
    table = Table()
    headers = Faker.choose()
    rows = [Faker.values(), Faker.values(), Faker.values(), Faker.values(), Faker.values()]
    table.add(headers, rows)
    return table

10.时间线图
def timeline_chart() -> Timeline:
    timeline = Timeline()
    x = Faker.choose()
    for i in range(1, 13):
        bar = (
            Bar()
                .add_xaxis(x)
                .add_yaxis('A', Faker.values())
                .set_global_opts(title_opts=opts.TitleOpts(title='一年销量变化'))
        )
        timeline.add(bar, f'{i}月')
    return timeline

11.词云图
def wordcloud_chart() -> WordCloud:
    data1 = [list(x) for x in zip(Faker.clothes, Faker.values())]
    data2 = [list(x) for x in zip(Faker.drinks, Faker.values())]
    data = data1 + data2
    wordcloud = (
        WordCloud()
            .add(series_name='销量分析', data_pair=data, word_size_range=[20, 150])
            .set_global_opts(
            title_opts=opts.TitleOpts(title='销量分析'),
            tooltip_opts=opts.TooltipOpts(is_show=True))
    )
    return wordcloud

12.完整代码
from pyecharts.charts import Bar
from pyecharts.charts import Line
from pyecharts.charts import Pie
from pyecharts.charts import Funnel
from pyecharts.charts import Gauge
from pyecharts.charts import Grid
from pyecharts.charts import Liquid
from pyecharts.charts import Tab
from pyecharts.components import Table
from pyecharts.globals import ThemeType
from pyecharts.charts import Timeline
from pyecharts.charts import WordCloud
from pyecharts import options as opts
import imgkit
from pyecharts.faker import Faker


def bar_chart() -> Bar:
    bar = (
        Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))
            .add_xaxis(Faker.choose())
            .add_yaxis('A', Faker.values())
            .add_yaxis('B', Faker.values())
            .set_global_opts(title_opts=opts.TitleOpts(title='销售Bar图'))
    )
    return bar


def line_chart() -> Line:
    line = (
        Line()
            .add_xaxis(Faker.choose())
            .add_yaxis('A', Faker.values())
            .add_yaxis('B', Faker.values())
            .set_global_opts(title_opts=opts.TitleOpts(title='销售line图'))
    )
    return line


def pie_chart() -> Pie:
    pie = (
        Pie()
            .add('', [list(item) for item in zip(Faker.choose(), Faker.values())])
            .set_global_opts(title_opts=opts.TitleOpts(title='销售pie图'))
            .set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{c}'))
    )
    return pie


def funnel_chart() -> Funnel:
    funnel = (
        Funnel()
            .add('', [list(x) for x in zip(Faker.choose(), Faker.values())])
            .set_global_opts(title_opts=opts.TitleOpts(title='销售funnel图'))
    )
    return funnel


def gauge_chart() -> Gauge:
    gauge = (
        Gauge()
            .add(series_name='当前测速', data_pair=[['speed', 60]])
            .set_global_opts(
            legend_opts=opts.LegendOpts(is_show=False),
            tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{a} 
{b} : {c}km/h") ) ) return gauge def grid_chart() -> Grid: grid = ( Grid() .add(bar_chart(), grid_opts=opts.GridOpts(pos_top='60%')) .add(line_chart(), grid_opts=opts.GridOpts(pos_bottom='60%')) ) return grid def liquid_chart() -> Liquid: liquid = ( Liquid() .add('', [0.6], is_outline_show=False) .set_global_opts(title_opts=opts.TitleOpts(title='liquid图')) ) return liquid def tab_chart() -> Tab: tabs = Tab() tabs.add(bar_chart(), 'bar page') tabs.add(line_chart(), 'line page') tabs.add(pie_chart(), 'pie page') return tabs def table_chart() -> Table: table = Table() headers = Faker.choose() rows = [Faker.values(), Faker.values(), Faker.values(), Faker.values(), Faker.values()] table.add(headers, rows) return table def timeline_chart() -> Timeline: timeline = Timeline() x = Faker.choose() for i in range(1, 13): bar = ( Bar() .add_xaxis(x) .add_yaxis('A', Faker.values()) .set_global_opts(title_opts=opts.TitleOpts(title='一年销量变化')) ) timeline.add(bar, f'{i}月') return timeline def wordcloud_chart() -> WordCloud: data1 = [list(x) for x in zip(Faker.clothes, Faker.values())] data2 = [list(x) for x in zip(Faker.drinks, Faker.values())] data = data1 + data2 wordcloud = ( WordCloud() .add(series_name='销量分析', data_pair=data, word_size_range=[20, 150]) .set_global_opts( title_opts=opts.TitleOpts(title='销量分析'), tooltip_opts=opts.TooltipOpts(is_show=True)) ) return wordcloud def html2img(html_path: str, img_name: str): exe_path = 'E:\wkhtmltopdf\bin\wkhtmltoimage.exe' cfg = imgkit.config(wkhtmltoimage=exe_path) imgkit.from_file(html_path, img_name, config=cfg) if __name__ == '__main__': # 1.bar bar_chart().render('bar.html') html2img('bar.html', 'bar.png') # 2.line line_chart().render('line.html') html2img('line.html', 'line.png') # 3.pie pie_chart().render('pie.html') html2img('pie.html', 'pie.png') # 4.funnel funnel_chart().render('funnel.html') html2img('funnel.html', 'funnel.png') # 5.gauge gauge_chart().render('gauge.html') html2img('gauge.html', 'gauge.png') # 6.grid grid_chart().render('grid.html') html2img('grid.html', 'grid.png') # 7.liquid liquid_chart().render('liquid.html') html2img('liquid.html', 'liquid.png') # 8.tab tab_chart().render('tab.html') html2img('tab.html', 'tab.png') # 9.table table_chart().render('table.html') html2img('table.html', 'table.png') # 10.timeline timeline_chart().render('timeline.html') html2img('timeline.html', 'timeline.png') # 11.wordcloud wordcloud_chart().render('wordcloud.html') html2img('wordcloud.html', 'wordcloud.png')

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5157203.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存