Pyecharts is a visual artifact for drawing bar graphs in Python
Last night I wrote about the visualization of Matplotlib. Today I recommend a visualization tool – Pyecharts.
Previous link: Python visual chart generation -Matplotlib
The installation
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyecharts
Copy the code
Pyecharts introduction
Pyecharts is a python/Echarts link, a third-party library used to generate Echarts ICONS. Pyecharts is available in v0.5.X and v1 versions, which are incompatible with each other. V1 is a new version and the development team has decided that the former will not be updated.
features
- Simple API design, silky smooth use, support chain call
- It contains over 30 common charts, and you can find everything
- Support mainstream Notebook environment, Jupyter Notebook and JupyterLab
- Easy to integrate into Flask, Django, and other mainstream Web frameworks
- Highly flexible configuration items can be easily matched to create beautiful charts
- Detailed documentation and examples to help developers get started faster
- Up to 400+ map files and native Baidu map provide strong support for geographic data visualization
advantage
Pyecharts can output links to the web version, directly call resources rendering chart, convenient and fast, output is not a picture, but an adjustable page, dynamic, cool, are its world! It can support browsing interface in the mobile phone, can also modify the relevant parameters, in general, extremely convenient, and the theme can be randomly matched, color adjustment. Matplotlib is suitable for visual reports of companies, corporate presentations and daily office work. Because the charts are too cool, it is not suitable for scientific research papers. Matplotlib is the first choice for scientific research papers.
Bar chart template series
1. Crystal bar chart
Crystal bar chart is suitable for the comparison of several data, the generated bar chart has a kind of exquisite, beautiful and fresh water out of hibiscus, the most suitable for visual display.
from pyecharts import options as optsfrom pyecharts.charts import Barfrom pyecharts.commons.utils import JsCode data_x = [' coke ', 'spirits',' orange ', 'green' and 'tea', 'bud', 'Qingdao] data_y = [47, 53, 27, 23, 94, 18, C = 48] (Bar (). Add_xaxis (data_x). Add_yaxis (merchants A data_y, category_gap="60%") .set_series_opts( itemstyle_opts={ "normal": { "color": JsCode( """new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgba(0, 244, 255, 1)' }, { offset: 1, color: 'rgba(0, 77, 167, 1)' }], false)""" ), "barBorderRadius": [30, 30, 30, 30], "shadowColor": "RGB (0, 160, 221)",}}).set_global_opts(title_opts= opts.titleopts (title=" sales "), xaxis_opts= opts.axisopts (name=' category ', Name_location ='middle', name_gap=30, # distance between tag and axis, default is 20, 20 name_textSTYLE_OPts = opts.textStyLeopts (font_family='Times New Roman', font_size=16 # tag font size)), AxisOpts(name=' number ', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), # toolbox_opts= opts.toolboxopts () # tooloptions).render(" crystal column.html "))Copy the code
Bar diagram of an X-axis label that is too long
Sometimes when we draw the bar chart, the X-axis label is too long, which leads to the abnormal display of the graph. After modifying the word count, we are afraid that the display effect is not obvious enough. This legend solves this problem and is suitable to show the problem of the X label being too long.
From Pyecharts import options as optsfrom Pyecharts. Charts Import Bar c = (Bar().add_xaxis([" name long X label 1", "Long name X label 2"," long name X label 3", "long name X label 4"," long name X label 5", "long name X label 6",]). Add_yaxis (" merchant A", [10, 20, 30, 40, 50, Add_yaxis (" merchant B", [20, 10, 40, 30, 40, 50]) .set_global_opts( xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15),), AxisOpts(name=' number ', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 )), Title_opts = opts.titleopts (title=" 1", subtitle=" 2"), # toolbox_opts= opts.toolboxopts () # tooloptions).render(" fix X tag too long. HTML "))Copy the code
Flip the X and Y histogram
The intuitive display bar chart is too monotonous, sometimes we need to adjust the theme appropriately, flip the XY axis, so that more intuitive comparison display, suitable for multiple data categories for comparison.
From Pyecharts import options as optsfrom Pyecharts. Charts Import Bardata_x = [' Coke ', 'Sprite ',' orange juice ', 'green tea ',' milk tea ', 'Budweiser ', 'Qingdao] data_y = [47, 53, 27, 123, 94, 18, 48] c = (Bar () add_xaxis (data_x). Add_yaxis (" merchants A "data_y). Add_yaxis (merchants" B ", data_y) .reversal_axis() .set_series_opts(label_opts=opts.LabelOpts(position="right")) .set_global_opts(title_opts= opts.titleopts (title=" title "), xaxis_opts= opts.axisopts (name=' quantity ', name_location='middle', Name_gap =30, the distance between # tag and axis, default is 20, 20 name_textSTYLE_OPts = opts.textStyLeopts (font_family='Times New Roman', font_size=16 # tag font size)), Yaxis_opts = opts.axisopts (name=' category ', name_location='middle', name_gap=40, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), ).render(" flip xy.html ")Copy the code
Movable X and Y axis bars (suitable for too many data categories)
The X-axis can be moved, and we can display the dimensions of the X-axis we want to display by mouse control. This is used for situations where there are too many data categories, which cannot be displayed by general visualization, such as the sales volume of one, we can use this to display 30 data categories.
from pyecharts import options as optsfrom pyecharts.charts import Barfrom pyecharts.globals import ThemeType data_x = Day [' 0 ', 'one day', '2 days',' 3 days', '4 days',' 5 days', '6 days',' 7 days', 'eight days',' 9 days', 'ten days',' 11 days', '12 days',' 13 days, 14 days' ', '15 days',' 16 days', '17 days, 19 days' 18 days', ' 'and' 20 days', '21 days',' 22 days', '23 days',' 24 days', '25 days',' 26 days', '27 days, 28 days'', '29 days'] data_y = [5, 27, 27, 7, 13, 5, 1, 2, 29, 20, 21, 28, 5, 22, 23, 4, 20, 26, 25, 1, 3, 14, 23, 11, 4, 8, 2, 22, 13, 22]c = ( Bar({"theme": Themetype.macarons}).add_xaxis(data_x).add_yaxis(" business A", Data_y).set_global_opts(title_opts= opts.titleopts (title=" title "), datazoom_opts= opts.datazoomopts (), # datazoom_opts= opts.datazoomopts (Orient ="vertical"), # render(" change X histogram ")Copy the code
The X-axis can be moved
The Y axis can be moved
For more information:
gallery.pyecharts.org/#/README