When it comes to data visualization in Python, most people opt for Matplotlib, but the resulting charts cannot be interactively manipulated, such as timeline dragging, interactive legends, etc. This article will cover Pyecharts in more detail.

preface

At present, Pyecharts has been updated to 1.x version, which has a very different composition syntax from 0.5.x version. The syntax of 1.x version is closer to Echarts, but it is still easy to understand. Therefore, this article is based on Pyecharts 0.5.11. Most examples come from official documentation and are annotated. I suggest you open jupyter Notebook and learn.

This can be done using PIP install Pyecharts ==0.5.11.

Before going into detail on how to draw each chart, let’s talk about the basic steps of Pyecharts drawing ⬇️


Bar chart/bar chart

Bar/bar charts, showing the size of data by the height of the bar/width of the bar.

Just look at the code, and everything will be commented out in the code

Is_stack Implements data stacking

from pyecharts import Bar # import bar module attr = [" shirt "and" sweater ", "snow spins unlined upper garment", "pants", "high heels", "socks"] # data set x axis v1 = [5, 20, 36, 10, 75, 90] # the first set of data the v2 = [8, 10, 25, 60, Add (attr, v1, is_stack=True) # Add (attr, v1, is_stack=True) Attr, v2, is_stack=True)bar.render() #Copy the code

Use marker points and marker lines

from pyecharts import Barbar = Bar("Examples of marker lines and markers")bar.add("Merchants A", attr, v1, mark_point=["average"]) Add (" merchant B", attr, v2, mark_line=["min", "Max "]) # render()Copy the code

Is_convert swaps the XY axis

from pyecharts import Barbar = Bar("X and y switch")bar.add("Merchants A", attr, v1)bar.add(Merchants "B", attr, v2, is_convert=True) Bar.render ()Copy the code

DataZoom effect, type ‘slider’

import randomattr = ["{} days".format(i) for i in range(30)]v1 = [random.randint(1, 30) for _ in range(30)] Bar. Add ("", attr, v1, is_label_show=True, Is_datazoom_show =True) # set datazoombar.render()Copy the code

Multiple dataZoom effects, supporting X and Y axis simultaneously

days = ["{} days".format(i) for i in range(30)]days_v1 = [random.randint(1, 30) for _ in range(30)]bar = Bar("Bar-datazoom - xaxis/yaxis example")bar.add(    "",    days,    days_v1,    Is_datazoom_show =True, datazoom_type="slider", datazoom_range=[10, 25], Is_datazoom_extra_show =True, datazoom_extra_type="slider", datazoom_extra_range=[10, 25], is_toolbox_show=False,)bar.render()Copy the code

3 d histogram

Set Grid3D_shading to make columns more realistic

bar3d = Bar3D("Sample 3D Bar Chart", width=1200, height=600)bar3d.add(    "",    x_axis,    y_axis,    [[d[1], d[0], d[2]] for d in data],    is_visualmap=True,    visual_range=[0, 20],    visual_range_color=range_color,    grid3d_width=200,    grid3d_depth=80,    grid3d_shading="lambert",)bar3d.render()Copy the code

Funnel figure

Labels are displayed on the inside

from pyecharts import Funnel # import funnel figure attr = [" shirt "and" sweater ", "snow spins unlined upper garment", "pants", "high heels", "socks"] value = [20, 40, 60, 80, 100, 120]funnel = ="inside", attr, value, is_label_show=True, Label_text_color ="# FFF ", # color)funnel.render()Copy the code

Labels are displayed externally

funnel = Funnel("Sample funnel plot", width=600, height=400, title_pos='center')funnel.add(    "Goods",    attr,    value,    is_label_show=True,    label_pos="outside",    legend_orient="vertical",    legend_pos="left",)funnel.render()Copy the code

The dashboard

from pyecharts import Gauge # gauge = gauge (" dashboard example ") # title gauge. Add (" business indicator ", "completion rate ", 66.66) # data gauge. Render ()Copy the code

The line chart

from pyecharts import Line # import related line chart attr = [" shirt "and" sweater ", "snow spins unlined upper garment", "pants", "high heels", "socks"] v1 = [5, 20, 36, 10, 10, 100] v2 = [55, 60, 16, 20, 15, Add (" merchant A", attr, v1, mark_point=["average"]) # add(" merchant B", attr, v1, mark_point=["average"]) # v2, is_smooth=True, mark_line=["max", "average"])line.render()Copy the code

Point to other configurations

line = Line("Sample line chart")line.add(    "Merchants A",    attr,    v1,    mark_point=["average", {"coord": ["Pants", 10]."name": "This is the first marker I want."}],) Line. add(" merchant B", attr, v2, is_smooth=True, mark_point=[{"coord": [" hosiery ", 80], "name": "This is the second marker I want "}],) # add second data line.render()Copy the code

Plot area using area_opacity

line = Line("Line Chart - Sample Area Chart")line.add(    "Merchants A",    attr,    v1,    line_opacity=0.2,    area_opacity=0.4,    symbol=None,)line.add(    Merchants "B",    attr,    v2,    is_fill=True,    area_color="# 000", area_opacity = 0.3,# set area is_smooth=True,)line.render()Copy the code

3 d line chart

Draw a spring


from pyecharts import Line3Dimport math_data = []for t inrange(0, 25000): _t = t / 1000 x = (1 + 0.25 * math. Cos _t (75 *)) * math.h cos (_t) y = (1 + 0.25 * math. Cos _t (75 *)) * math.h sin (z = _t _t) Math. Sin (75 * _t) _data.appEnd ([x, y, z])range_color = ['# 313695'.'#4575b4'.'#74add1'.'#abd9e9'.'#e0f3f8'.'#ffffbf'.'#fee090'.'#fdae61'.'#f46d43'.'#d73027'.'#a50026']line3d = Line3D("Sample 3D Line Chart", width=1200, height=600)line3d.add(    "",    _data,    is_visualmap=True,    visual_range_color=range_color,    visual_range=[0, 30],    is_grid3d_rotate=True,    grid3d_rotate_speed=180,)line3d.render()Copy the code


The pie chart

The pie chart mainly shows the proportion of data of different categories in the total. The radians of each represent the ratio of the amount of data.

The pie chart example

from pyecharts import Pieattr = ["Shirt"."Cardigan"."Chiffon shirt."."Pants"."High heels"."Socks"]v1 = [11, 12, 13, 10, 10, 10]pie = Pie("Sample pie chart")pie.add("", attr, v1, is_label_show=True) Pie.render ()Copy the code

Adjust the circle

from pyecharts import Pieattr = ["Shirt"."Cardigan"."Chiffon shirt."."Pants"."High heels"."Socks"]v1 = [11, 12, 13, 10, 10, 10]pie = Pie("Pie chart - Circle Chart Example", title_pos='center'¥create pie chart pie.add("",    attr,    v1,    radius=[40, 75], Label_text_color =None, is_label_show=True, Legend_pos ="left", legend_pos="left", legend_pos="left", legend_pos="left")pie.render()Copy the code

Pie chart – Rose chart

from pyecharts import Pieattr = ["Shirt"."Cardigan"."Chiffon shirt."."Pants"."High heels"."Socks"]v1 = [11, 12, 13, 10, 10, 10]v2 = [19, 21, 32, 20, 20, 33]pie = Pie("Pie Chart - Sample Rose Chart", title_pos='center', width=900)pie.add(    Products "A",    attr,    v1,    center=[25, 50],    is_random=True,    radius=[30, 75],    rosetype="radius",)pie.add(    "Product B",    attr,    v2,    center=[75, 50],    is_random=True,    radius=[30, 75],    rosetype="area",    is_legend_show=False,    is_label_show=True,)pie.render()Copy the code

The map

National map (with VisualMap)

Pyecharts import Mapvalue = [155, 10, 66, 78, 33, 80, 190, 53, 49.6]attr = ["Fujian"."Shandong"."Beijing"."Shanghai"."Gansu"."Xinjiang"."Henan"."Guangxi"."Tibet"    ]map = Map("Map with VisualMap example", width=1200, height=600)map.add(    "",    attr,    value,    maptype="china".Is_visualmap =True, visual_text_color="#000",)map.render()Copy the code


Different province map

from pyecharts import Mapvalue = [20, 190, 253, 77, 65]attr = ['Shantou'.shanwei.'Jieyang'.'Yangjiang'.'Zhaoqing']map = Map("Sample Map of Guangdong", width=1200, height=600)map.add(    "", attr, value, maptype="Guangdong", is_visualmap=True, visual_text_color="# 000") # set guangdong map.render()Copy the code


The world map

Value = [95.1, 23.2, 43.3, 66.4, 88.5]attr= ["China"."Canada"."Brazil"."Russia"."United States"]map = Map("Sample World Map", width=1200, height=600)map.add(    "",    attr,    value,    maptype="world".Is_visualmap =True, visual_text_color="#000",)map.render()Copy the code


Word cloud

from pyecharts import WordCloudname = [    'Sam S Club'.'Macys'.'Amy Schumer'.'Jurassic World'.'Charter Communications'.'Chick Fil A'.'Planet Fitness'.'Pitch Perfect'.'Express'.'Home'.'Johnny Depp'.'Lena Dunham'.'Lewis Hamilton'.'KXAN'.'Mary Ellen Mark'.'Farrah Abraham'.'Rita Ora'.'Serena Williams'.'NCAA baseball tournament'.'Point Break']value = [    10000, 6181, 4386, 4055, 2467, 2244, 1898, 1484, 1112,    965, 847, 582, 555, 550, 462, 366, 360, 282, 273, 265]wordcloud = WordCloud(width=1300, height=620)wordcloud.add("", name, value, word_size_range=[20, 100]) # set size wordcloud.render()Copy the code

conclusion

Some of the basic diagrams that are used in data visualization are here, and the drawing method is pretty much the same: Create an instance of what you need and add various data and configurations to it. Finally, let’s review the general steps for drawing diagrams with PyEcharts: