picture

writing |  doug

source :Python technology 「ID: pythonall」

 picture

With the rapid development of Internet , The amount of data is also growing wildly , Data analysis in recent years , Data mining is becoming more and more popular . When it comes to data analysis , Data visualization is indispensable , After all, charts are more intuitive than cold numbers , Trends and conclusions can be seen at a glance , After all, a picture is worth a thousand words .

and Python As the main language of data analysis , Naturally, there are many visual class libraries , such as matplotlib, A common bar chart 、 Scatter plot 、 Line graphs can be generated . But if you want to show it on the web, it will be a bit stretched .

done  web We all know the basics of data visualization Echarts   This library , This is a data visualization class library opened by Baidu . Speak true , Although I don't like Baidu at all , But this tool does work , Let's go one size at a time , You can't deny all Baidu products just because you don't like Baidu . It relies on good interaction , Beautiful graphic design , And easy access for developers , It plays an important role in data visualization .

and Python It's an expressive language , Very suitable for data processing . When data analysis meets data visualization ,pyecharts The birth of . It can not only generate independent web pages , You can also do it in flask , Django And so on .

Let's talk about it today pyecharts There are several kinds of charts we often use in English .

install

Directly through pip Can be installed .

pip install pyecharts

Old rules , For the smooth development of the story , Let's import the required modules first .

from pyecharts.charts import Barfrom pyecharts.charts import Linefrom pyecharts import options as optsfrom pyecharts.charts import EffectScatterfrom pyecharts.globals import SymbolTypefrom pyecharts.charts import Gridfrom pyecharts.charts import WordCloudfrom pyecharts.charts import Mapimport random

Histogram

The most commonly used chart is the bar chart ,pyecharts Generating a histogram is very simple . Fill in directly x Axis and y The data of the axis is enough .

x = ['1 month ', '2 month ', '3 month ', '4 month ', '5 month ', '6 month ', '7 month ', '8 month ', '9 month ', '10 month ', '11 month ', '12 month ']data_china = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]data_russia = [1.6, 5.4, 9.3, 28.4, 22.7, 60.7, 162.6, 199.2, 56.7, 43.8, 3.0, 4.9]
bar = Bar()bar.add_xaxis(x)bar.add_yaxis(" precipitation ", data_china)bar.set_global_opts(title_opts=opts.TitleOpts(title="Bar - Basic example "))bar.render()

stay PyCharm After running the above code in, you will find that , There's nothing on the console , It will not be like matplotlib Generate a picture as well , Are we in the wrong position , But if you are careful, you will be there Python Found one in the same level directory of the file html file , To open it , Why , So here it is .

in fact  render Will generate local HTML file , By default, it will generate... In the current directory render.html file , Of course, we can also pass in path parameters , Such as  bar.render("mycharts.html"). But it's too much trouble to test like this , Fortunately pyechars Provides a thoughtful  Notebook Pattern , So that we can be in Jupyter Notebook / Jupyter Lab / Nteract / Zeppelin   Rendering in four environments .

This paper is based on Jupyter Notebook Test done under , Just put the bar.render() Change it to bar.rerender_notebook() that will do . I'll do it again after the change run You get the following :

 picture

meanwhile ,pyechars Chain calls are also supported .

bar = (    Bar()    .add_xaxis(x)    .add_yaxis('china', data_china)    .set_global_opts(title_opts=opts.TitleOpts(title="Bar -  Basic example ")))bar.render_notebook()

in addition ,pyechars It also supports adding multiple y Axis record , Just call it one more time add_yaxis that will do .

bar = (    Bar()    .add_xaxis(x)    .add_yaxis('china', data_china)    .add_yaxis("sussia", data_russia)    .set_global_opts(title_opts=opts.TitleOpts(title="Bar -  Multi histogram ")))bar.render_notebook()

 picture

Sometimes I think the bar chart is too high to read , We can also put x Axis and y The shafts are interchangeable , Generate a horizontal bar chart . Multi histogram and xy Shaft exchange does not conflict , It can be used in superposition .

bar = (    Bar()    .add_xaxis(x)    .add_yaxis('china', data_china)    .add_yaxis('russia', data_russia)    .reversal_axis()    .set_series_opts(label_opts=opts.LabelOpts(position="right"))    .set_global_opts(title_opts=opts.TitleOpts(title="Bar -  Flip  XY  Axis ")))bar.render_notebook()

 picture

The pie chart

Pie chart is also one of the most frequently used charts , Especially for graphs that occupy percentage classes , It's very intuitive to see the proportion of each category in the total share .

pie = (    Pie()    .add("", [list(z) for z in zip(x, data_china)])    .set_global_opts(title_opts=opts.TitleOpts(title="Pie -  Basic example "))    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")))pie.render_notebook()

 picture

Doughnut pie chart .

pie = (    Pie(init_opts=opts.InitOpts(width="600px", height="400px"))    .add(        series_name=" rainfall ",        data_pair=[list(z) for z in zip(x, data_china)],        radius=["50%", "70%"],        label_opts=opts.LabelOpts(is_show=False, position="center"),    )    .set_global_opts(legend_opts=opts.LegendOpts(pos_left="legft", orient="vertical"))    .set_series_opts(        tooltip_opts=opts.TooltipOpts(            trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"        ),    label_opts=opts.LabelOpts(formatter="{b}: {c}")    ))pie.render_notebook()

 picture


Broken line diagram

Line chart is usually used to show the trend of data in different time periods , For example, in the stock market K Line chart is a kind of line chart .

line = (    Line()    .add_xaxis(x)    .add_yaxis('china', data_china)    .set_global_opts(title_opts=opts.TitleOpts(title="Line -  Basic example ")))line.render_notebook()

 picture

Again , It's like a bar chart , Line chart can also add multiple y Axis record .

line = (    Line()    .add_xaxis(x)    .add_yaxis('china', data_china)    .add_yaxis('russis', data_russia)    .set_global_opts(title_opts=opts.TitleOpts(title="Line -  Double line chart ")))line.render_notebook()

 picture

Ladder line chart is also supported .

line = (    Line()    .add_xaxis(x)    .add_yaxis('china', data_china, is_step=True)    .set_global_opts(title_opts=opts.TitleOpts(title="Line -  Step line ")))line.render_notebook()

 picture

Scatter plot
scatter = (    EffectScatter()    .add_xaxis(x)    .add_yaxis("", data_china)    .set_global_opts(title_opts=opts.TitleOpts(title="EffectScatter -  Basic example ")))scatter.render_notebook()

 picture


The data comparison is not very clear , We can grid the scatter plot , So that each point corresponds to the y Axis data is more visible .

scatter = (    EffectScatter()    .add_xaxis(x)    .add_yaxis("china", data_china, symbol=SymbolType.ARROW)    .set_global_opts(        title_opts=opts.TitleOpts(title="EffectScatter -  Show split lines "),        xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),        yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),    ))scatter.render_notebook()

 picture

meanwhile , We can specify the shape of the point , You can also add more than one on a scatter plot y Axis record . These configurations are like building blocks , Stack at will .

scatter = (    EffectScatter()    .add_xaxis(x)    .add_yaxis("china", [x + 30 for x in data_russia],symbol=SymbolType.ARROW)    .add_yaxis("russia", data_russia, symbol=SymbolType.TRIANGLE)     .set_global_opts(        title_opts=opts.TitleOpts(title="EffectScatter -  Show split lines "),        xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),        yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),    ))scatter.render_notebook()

 picture

Chart merging

occasionally , We need to put many kinds of pictures on one picture to show them in a centralized way ,pyechars It also takes into account . The basic step is to generate the graphs of each category separately , And then use Grid Combine the two .

For example, we want to put bar charts and line charts together , First, generate them separately Bar and Line, Then combine the two .

from pyecharts.charts import Grid
bar = (    Bar()    .add_xaxis(x)    .add_yaxis('china', data_china)    .add_yaxis("sussia", data_russia)    .set_global_opts(        title_opts=opts.TitleOpts(title="Grid - Multi graph merging "),    ))
line = (    Line()    .add_xaxis(x_data)    .add_yaxis(" Evaporation capacity ", [x + 50 for x in data_china]    ))
bar.overlap(line)grid = Grid()grid.add(bar, opts.GridOpts(pos_left="5%", pos_right="5%"), is_control_axis_index=True)grid.render_notebook()

 picture

The word cloud

Again , Powerful pyechars It also supports the word cloud , What's more, there's no problem with Chinese at all , There will be no garbled code .

import pyecharts.options as optsfrom pyecharts.charts import WordCloud
data = [(" Living resources ", "999"),(" Heating management ", "888"),(" Gas supply quality ", "777"),(" Domestic water management ", "688"),(" Primary water supply ", "588"),(" The transportation ", "516"),(" Urban transportation ", "515"),(" Environmental protection ", "483"),(" Real estate management ", "462"),(" Urban and rural construction ", "449"),(" Social security and welfare ", "429"),(" Social security ", "407"),(" Style and education management ", "406"),(" Public safety ", "406"),(" Public transport management ", "386"),(" Taxi operation management ", "385"),(" Heating management ", "375"),(" City appearance and sanitation ", "355"),(" Natural resource management ", "355"),(" Dust pollution ", "335"),(" Noise pollution ", "324"),(" land resource management ", "304"),(" Property service and management ", "304"),(" medical and health work ", "284"),(" Fly ash pollution ", "284"),(" Jeeves ", "284"),(" Heating development ", "254"),(" Rural land planning and management ", "254"),(" Life noise ", "253"),(" Influence of heating units ", "253"),(" Urban power supply ", "223"),(" Housing quality and safety ", "223"),(" air pollution ", "223"),(" House safety ", "223"),(" Cultural Activity ", "223"),(" Demolition management ", "223"),(" communal facilities ", "223"),(" Gas supply quality ", "223"),(" Power management ", "223"),(" Gas management ", "152"),(" education management ", "152"),(" Medical disputes ", "152"),(" Law enforcement supervision ", "152"),(" Safety equipment ", "152"),(" Government affairs construction ", "152"),(" Counties and districts 、 area for development ", "152"),(" macroeconomic ", "152"),(" education management ", "112"),(" Social security ", "112"),(" Domestic water management ", "112"),(" Property service and management ", "112"),(" List of categories ", "112"),(" Agricultural production ", "112"),(" Secondary water supply ", "112"),(" Urban public facilities ", "92"),(" Demolition policy consultation ", "92"),(" Property services ", "92"),(" estate management ", "92"),(" Social security insurance management ", "92"),(" Minimum living insurance management ", "92"),(" Cultural and entertainment market management ", "72"),(" Urban traffic order management ", "72"),(" Law enforcement disputes ", "72"),(" Commercial smoke pollution ", "72"),(" Pile up on the road ", "71"),(" Above ground facilities ", "71"),(" The water quality ", "71"),(" anhydrous ", "71"),(" Influence of heating units ", "71"),(" Sidewalk Management ", "71"),(" Main network reasons ", "71"),(" Central heating ", "71"),(" Passenger transport management ", "71"),(" State owned public transport ( Bus ) management ", "71"),(" Industrial dust pollution ", "71"),(" Security cases ", "71"),(" Pressure vessel safety ", "71"),(" ID card management ", "71"),(" Mass fitness ", "41"),(" Industrial pollution ", "41"),(" Destruction of forest resources ", "41"),(" Market charge ", "41"),(" Production funds ", "41"),(" Production noise ", "41"),(" Rural minimum living insurance ", "41"),(" Labor disputes ", "41"),(" Labor contract dispute ", "41"),(" Labor remuneration and welfare ", "41"),(" malpractice ", "21"),(" Stop supplying ", "21"),(" elementary education ", "21"),(" Vocational education ", "21"),(" Property qualification management ", "21"),(" Compensation for demolition ", "21"),(" Facility maintenance ", "21"),(" Market spillover ", "11"),(" Occupy the road to manage ", "11"),(" Tree management ", "11"),(" Rural infrastructure ", "11"),(" anhydrous ", "11"),(" Gas supply quality ", "11"),(" Stop breathing ", "11"),(" Municipal government departments ( Including department management organization 、 Direct subordinate units )", "11"),(" Gas management ", "11"),(" City appearance and sanitation ", "11"),(" News media ", "11"),(" Talent recruitment ", "11"),(" market environment ", "11"),(" Administrative charges ", "11"),(" Food safety and hygiene ", "11"),(" Urban transportation ", "11"),(" Real estate development ", "11"),(" Housing matching problems ", "11"),(" Property services ", "11"),(" estate management ", "11"),(" Jeeves ", "11"),(" Landscaping ", "11"),(" Registered residence management and identity card ", "11"),(" Public transport management ", "11"),(" highway ( waterway ) traffic ", "11"),(" The drawing doesn't conform to the house ", "11"),(" Cable TV ", "11"),(" public security ", "11"),(" Forestry resources ", "11"),(" Other administrative charges ", "11"),(" Operating charges ", "11"),(" Food safety and hygiene ", "11"),(" Sports ", "11"),(" CATV installation, debugging and maintenance ", "11"),(" Minimum living insurance management ", "11"),(" Labor disputes ", "11"),(" Social welfare and services ", "11"),(" Primary water supply ", "11"),]
wordCloud = (    WordCloud()    .add(series_name=" Hot spot analysis ", data_pair=data, word_size_range=[6, 66])    .set_global_opts(        title_opts=opts.TitleOpts(            title=" Hot spot analysis ", title_textstyle_opts=opts.TextStyleOpts(font_size=23)        ),        tooltip_opts=opts.TooltipOpts(is_show=True),    ))
wordCloud.render_notebook()

 picture

Map

Last , Let's see pyecharts Support for maps and charts .

Sometimes we want to show the data on the map , For example, the population data of all provinces in China , Distribution of wechat friends in different provinces, etc .

provinces = [' guangdong ', ' Beijing ', ' Shanghai ', ' hunan ', ' Chongqing ', ' xinjiang ', ' Henan ', ' heilongjiang ', ' Zhejiang ', ' Taiwan '] values = [random.randint(1, 1024) for x in range(len(provinces))]
map = (    Map()    .add("", [list(z) for z in zip(provinces, values)], "china")    .set_global_opts(        title_opts=opts.TitleOpts(title="map - Basic example "),        visualmap_opts=opts.VisualMapOpts(max_=1024, is_piecewise=True),    )
)map.render_notebook()

 picture

summary

Today we analyzed pyecharts Several charts in common use , As the saying goes, a picture is worth a thousand words , Data analysis cannot do without data visualization , Especially when reporting to leaders , The chart can express the result more clearly .

The basic steps of generating a chart can be roughly divided into three steps , Prepare relevant data 、 Use chain call method to set data and related configuration 、 call render_notebook() perhaps render() Function generation chart .

in addition ,pyecharts Also support a lot of fun 3D Chart , You may refer to the official documents