PyEchart-- Data Analyst weapon

Echart

https://echarts.baidu.com/

ECharts, an open source visualization library implemented in JavaScript, you can smooth run on the PC and mobile devices, is compatible with the vast majority of current browsers (IE8 / 9/10/11 , Chrome, Firefox, Safari , etc.), dependent on the underlying light-weight level vector graphics library ZRender , provides an intuitive, interactive rich, highly personalized custom data visualization charts.

Rich visualization types

ECharts provide a general line graph , bar graph , scatter plot , pie chart , K line graph , a statistical box plots for geographic data visualization maps , thermodynamic diagram , diagram , a relational data visualization diagram , TreeMap , sun FIG multidimensional data visualization parallel coordinates , as well as for the BI funnel , dashboards , and supports between mashup FIG Fig.

 

PyEchart

Echarts is a visualization of Baidu open source data, with a good interactive, sophisticated graphic design, it has been recognized by many developers. The Python is an expressive language, it is suitable for data processing. When the data analysis encounter data visualization, pyecharts born.

 

✨ properties

  • Simple API design, such as silky smooth, support for chained calls
  • 30+ kinds of common chart encompasses everything
  • Notebook supports mainstream environment, Jupyter Notebook and JupyterLab
  • It can be easily integrated into Flask, Sanic, Django Web framework and other mainstream
  • Highly flexible configuration options, you can easily match the beautiful chart
  • Detailed documentation and samples to help developers get started faster project
  • Up to 400 + map files, and supports native Baidu map, provide strong support for geographic data visualization

 

Why not matplotlib?

He said echart than its advantages:

(1) many types, relying echart library.

(2) interactive, relying echart library.

(3) embedded in a web page easily. Front and rear ends to support the separation mode and a separation mode.

 

Common: all data analysts use.

 

Examples

https://pyecharts.org/#/zh-cn/web_flask

All achieved using python

Sample Code

from flask import Flask
from jinja2 import Markup, Environment, FileSystemLoader from pyecharts.globals import CurrentConfig # 关于 CurrentConfig,可参考 [基本使用-全局变量] CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader("./templates")) from pyecharts import options as opts from pyecharts.charts import Bar app = Flask(__name__, static_folder="templates") def bar_base() -> Bar: c = ( Bar() .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]) .add_yaxis("商家A", [5, 20, 36, 10, 75, 90]) .add_yaxis("商家B", [15, 25, 16, 55, 48, 8]) .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题")) ) return c @app.route("/") def index(): c = bar_base() return Markup(c.render_embed()) if __name__ == "__main__": app.run()

 

Front and rear ends separation mode

Step 3: Create a new HTML file

Create an HTML file templates file located in the root directory of the project folder, the following index.html here as an example. The main uses jqueryand pyechartsmanagement of echarts.min.jsdependence

index.html

<!DOCTYPE html>
<html> <head> <meta charset="UTF-8"> <title>Awesome-pyecharts</title> <script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script> <script type="text/javascript" src="https://assets.pyecharts.org/assets/echarts.min.js"></script> </head> <body> <div id="bar" style="width:1000px; height:600px;"></div> <script> $( function () { var chart = echarts.init(document.getElementById('bar'), 'white', {renderer: 'canvas'}); $.ajax({ type: "GET", url: "http://127.0.0.1:5000/barChart", dataType: 'json', success: function (result) { chart.setOption(result); } }); } ) </script> </body> </html>

Step 4: write flask and pyecharts code for rendering charts

Under Save the following code as app.py file and move to the root directory of the project.

Directory structure is as follows

sunhailindeMacBook-Pro:pyecharts_flask sunhailin$ tree -L 1
.
├── app.py
└── templates

Note: Due to the current json data types of problems can not be converted pyecharts in JSCode types of data into the json data format to return to the front page in use. Therefore, in the case before and after use to avoid the use of separate end JSCode for drawing.

app.py

from random import randrange

from flask import Flask, render_template from pyecharts import options as opts from pyecharts.charts import Bar app = Flask(__name__, static_folder="templates") def bar_base() -> Bar: c = ( Bar() .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]) .add_yaxis("商家A", [randrange(0, 100) for _ in range(6)]) .add_yaxis("商家B", [randrange(0, 100) for _ in range(6)]) .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题")) ) return c @app.route("/") def index(): return render_template("index.html") @app.route("/barChart") def get_bar_chart(): c = bar_base() return c.dump_options() if __name__ == "__main__": app.run()

 

Guess you like

Origin www.cnblogs.com/lightsong/p/11247641.html