pyecharts运行了但是没有图_python数据可视化之pyecharts

pyecharts运行了但是没有图_python数据可视化之pyecharts_第1张图片

Python ❤️ Echarts = pyecharts

pyecharts 是 Python 和 Echarts 的结合,绘制出的图表十分精美且种类丰富

pyecharts运行了但是没有图_python数据可视化之pyecharts_第2张图片

pyecharts目前更新到 1.7.0,1.x版本与0.5.x版本在语法上有很大区别

安装

这里我们直接安装最新版本:

pip(3) install pyecharts

如果安装后导入有问题,可以尝试源码安装:

$ git clone https://github.com/pyecharts/pyecharts.git$ cd pyecharts$ pip install -r requirements.txt$ python setup.py install# 或者执行 python install.py

柱形图

from pyecharts.charts import Barfrom pyecharts import options as optsx = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]y1 = [5, 20, 36, 10, 75, 90]y2 = [15, 6, 45, 20, 35, 66]bar = (    Bar()    .add_xaxis(x)    .add_yaxis("商家A", y1)    .add_yaxis("商家B", y2)    .set_global_opts(title_opts=opts.TitleOpts(title="主标题",                                               subtitle="副标题")))# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件# 也可以传入路径参数,如 bar.render("mycharts.html")bar.render()

pyecharts运行了但是没有图_python数据可视化之pyecharts_第3张图片

在notebook中显示图表

pyecharts也支持Jupyter Notebook 和 JupyterLab,但是在notebook中渲染图表时要求不同:

Jupyter Notebook 用 bar.render_notebook()直接渲染

JupyterLab 则稍微复杂一些:

  • 必须在引入 pyecharts.charts 等模块前声明Notebook 类型

from pyecharts.globals import CurrentConfig, NotebookTypeCurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB
  • 第一次渲染时要先用 bar.load_javascript() 预先加载基本 JavaScript 文件到 Notebook 中

  • 然后在不同的cell中运行bar.render_notebook()

pyecharts运行了但是没有图_python数据可视化之pyecharts_第4张图片

这样就可以一边运行代码,一边查看图片结果啦。

Bar-DataZoom(slider+inside)

from pyecharts.faker import Fakerfrom pyecharts import options as optsfrom pyecharts.charts import Barbar = (        Bar()        .add_xaxis(Faker.days_attrs)        .add_yaxis("", Faker.days_values, color=Faker.rand_color())        .set_global_opts(            title_opts=opts.TitleOpts(title="Bar-DataZoom"),            datazoom_opts=[opts.DataZoomOpts(), opts.DataZoomOpts(type_="inside")],        )    )bar.render_notebook()

pyecharts运行了但是没有图_python数据可视化之pyecharts_第5张图片

效果比较炫酷

堆叠柱形图

from pyecharts.charts import Barfrom pyecharts import options as optsx = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]y1 = [5, 20, 36, 10, 75, 90]y2 = [15, 6, 45, 20, 35, 66]#通过stack="stack1"设置堆叠bar = (    Bar()    .add_xaxis(x)    .add_yaxis("商家A", y1, stack="stack1")    .add_yaxis("商家B", y2, stack="stack1")    .set_global_opts(title_opts=opts.TitleOpts(title="主标题",                                               subtitle="副标题")))bar.render_notebook()

pyecharts运行了但是没有图_python数据可视化之pyecharts_第6张图片

更换图表风格

以上图表都是pyecharts的默认主题,其实pyecharts内置10+种不同主题,可以自行选用。

from pyecharts.charts import Barfrom pyecharts import options as opts#引入主题模块from pyecharts.globals import ThemeTypex = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]y1 = [5, 20, 36, 10, 75, 90]y2 = [15, 6, 45, 20, 35, 66]# init_opts=opts.InitOpts(theme=ThemeType.LIGHT) 选用LIGHT主题# .reversal_axis()交换 XY 轴# .set_series_opts(label_opts=opts.LabelOpts(position="right")) 指定数据显示位置为右侧bar = (    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))    .add_xaxis(x)    .add_yaxis("商家A", y1)    .add_yaxis("商家B", y2)    .reversal_axis()    .set_series_opts(label_opts=opts.LabelOpts(position="right"))    .set_global_opts(title_opts=opts.TitleOpts(title="主题为LIGHT",                                               subtitle="副标题")))bar.render_notebook()

pyecharts运行了但是没有图_python数据可视化之pyecharts_第7张图片

添加标记点

from pyecharts.charts import Barfrom pyecharts import options as optsfrom pyecharts.globals import ThemeTypex = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]y1 = [5, 20, 36, 10, 75, 90]y2 = [15, 6, 45, 20, 35, 66]# 通过markpoint_opts添加标记点bar = (    Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))    .add_xaxis(x)    .add_yaxis("商家A", y1)    .add_yaxis("商家B", y2)    .set_series_opts(        label_opts=opts.LabelOpts(is_show=False),                markpoint_opts=opts.MarkPointOpts(                    data=[                        opts.MarkPointItem(type_="max", name="最大值"),                        opts.MarkPointItem(type_="min", name="最小值"),                                       ]            ),        )    .set_global_opts(title_opts=opts.TitleOpts(title="主题为DARK",                                               subtitle="最大值、最小值标记点")))bar.render_notebook()

pyecharts运行了但是没有图_python数据可视化之pyecharts_第8张图片

饼图

from pyecharts import options as optsfrom pyecharts.charts import Piefrom pyecharts.globals import ThemeTypelist = [['可乐', 76], ['雪碧', 49], ['橙汁', 115]]bar = (    Pie(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))    .add("", list)    .set_global_opts(title_opts=opts.TitleOpts(title="饼图"))    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b} : {c}  {d}%")))bar.render_notebook()

pyecharts运行了但是没有图_python数据可视化之pyecharts_第9张图片

多饼图

from pyecharts import options as optsfrom pyecharts.charts import Piefrom pyecharts.commons.utils import JsCodefn = """    function(params) {        if(params.name == '其他')            return '\\n\\n\\n' + params.name + ' : ' + params.value + '%';        return params.name + ' : ' + params.value + '%';    }    """def new_label_opts():        return opts.LabelOpts(formatter=JsCode(fn), position="center")c = (        Pie()        .add(            "",            [list(z) for z in zip(["剧情", "其他"], [25, 75])],            center=["20%", "30%"],            radius=[60, 80],            label_opts=new_label_opts(),        )        .add(            "",            [list(z) for z in zip(["奇幻", "其他"], [24, 76])],            center=["55%", "30%"],            radius=[60, 80],            label_opts=new_label_opts(),        )        .add(            "",            [list(z) for z in zip(["爱情", "其他"], [14, 86])],            center=["20%", "70%"],            radius=[60, 80],            label_opts=new_label_opts(),        )        .add(            "",            [list(z) for z in zip(["惊悚", "其他"], [11, 89])],            center=["55%", "70%"],            radius=[60, 80],            label_opts=new_label_opts(),        )        .set_global_opts(            title_opts=opts.TitleOpts(title="Pie-多饼图示例"),            legend_opts=opts.LegendOpts(                type_="scroll", pos_top="20%", pos_left="80%", orient="vertical"            ),        )    )c.render_notebook()

pyecharts运行了但是没有图_python数据可视化之pyecharts_第10张图片

多维散点图

from pyecharts import options as optsfrom pyecharts.charts import Scatterfrom pyecharts.faker import Fakerfrom pyecharts.globals import ThemeTypefrom pyecharts.commons.utils import JsCodeScatter = (        Scatter(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))        .add_xaxis(Faker.choose())        .add_yaxis(            "商家A",            [list(z) for z in zip(Faker.values(), Faker.choose())],            label_opts=opts.LabelOpts(                formatter=JsCode(                    "function(params){return params.value[1] +' : '+ params.value[2];}"                ),                #position="inside"            ),        )         .set_global_opts(            title_opts=opts.TitleOpts(title="Scatter-多维度数据"),            tooltip_opts=opts.TooltipOpts(                formatter=JsCode(                    "function (params) {return params.name + ' : ' + params.value[2];}"                )            ),            visualmap_opts=opts.VisualMapOpts(                type_="size", max_=150, min_=20, dimension=1            ),        )    )Scatter.render_notebook()

pyecharts运行了但是没有图_python数据可视化之pyecharts_第11张图片

漏斗图

from pyecharts import options as optsfrom pyecharts.charts import Funnel, Pagefrom pyecharts.globals import ThemeTypelabels = ['浏览人数', '加购人数', '购买人数']data = [100, 50, 30]funnel = (        Funnel(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))        .add("", [list(z) for z in zip(labels, data)],            label_opts=opts.LabelOpts(position="inside"))        .set_global_opts(title_opts=opts.TitleOpts(title="销售转化漏斗"))    )    funnel.render_notebook()

pyecharts运行了但是没有图_python数据可视化之pyecharts_第12张图片

词云

from pyecharts import options as optsfrom pyecharts.charts import Page, WordCloudfrom pyecharts.globals import SymbolTypewords = [    ("火锅", 1446),    ("面包", 928),    ("油条", 906),    ("牛奶", 825),    ("凉皮", 514),    ("早餐", 486),    ("吐司", 53),    ("芝士", 163),    ("酸奶", 86),    ("奶茶", 17),    ("蛋糕", 678),    ("烧烤", 567),    ("土豆", 437),    ("巧克力", 422),    ("甜甜圈", 353),    ("苹果", 331)]wordcloud = (        WordCloud()        .add(            "",            words,            word_size_range=[20, 100],            rotate_step = 90,#设置文字角度            textstyle_opts=opts.TextStyleOpts(font_family="cursive"),        )        .set_global_opts(title_opts=opts.TitleOpts(title="WordCloud"))    )wordcloud.render_notebook()

pyecharts运行了但是没有图_python数据可视化之pyecharts_第13张图片

中国疫情地图

from pyecharts import options as optsfrom pyecharts.charts import Map#数据截至 2020/2/29 22:14 现存确诊data = [['湖北', 34617], ['广东', 366], ['山东', 332], ['浙江', 188], ['四川', 184],         ['湖南', 170], ['黑龙江', 166], ['重庆', 148],['北京', 132], ['江西', 123],        ['安徽', 116], ['江苏', 108], ['河南', 81], ['广西', 74], ['香港', 62],         ['福建', 53],['上海', 47], ['陕西', 37], ['贵州', 32], ['河北', 31],         ['台湾', 29], ['内蒙古', 26], ['辽宁', 25], ['天津', 24],['山西', 20],         ['吉林', 17], ['海南', 16], ['云南', 15], ['新疆', 11], ['甘肃', 7],         ['宁夏', 4], ['澳门', 2],        ['青海', 0], ['西藏', 0]]map = (        Map()        .add("现存确诊", data, "china")        .set_global_opts(            title_opts=opts.TitleOpts(title="现存确诊疫情地图"),            visualmap_opts=opts.VisualMapOpts(max_=35000, is_piecewise=True,            pieces=[                {"min": 10000, "label": '>10000人', "color": "#6666CC"},                {"min": 1000, "max": 10000, "label": '1001-10000人', "color": "#9999FF"},                {"min": 500, "max": 999, "label": '999-1000人', "color": "#CCCCFF"},                {"min": 100, "max": 499, "label": '100-499人', "color": "#FF9999"},                {"min": 10, "max": 99, "label": '10-99人', "color": "#FFCCCC"},                {"min": 1, "max": 9, "label": '0-9人', "color": "#CCCCCC"},                {"min": 0, "max": 0, "label": '0人', "color": "#ffffff"},            ],),        )    )map.render_notebook()

pyecharts运行了但是没有图_python数据可视化之pyecharts_第14张图片

以上只是pyecharts的一些基本示例,然而它的强大之处远不止于此。

具体可参考官方文档:https://pyecharts.org/#/zh-cn/intro

你可能感兴趣的:(pyecharts运行了但是没有图_python数据可视化之pyecharts)