Detailed knowledge of data analysis pyecharts histogram in python (a must-see for novices)

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only, and do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it by yourself

Python free learning materials, codes and exchange answers click to join


1. Introduction to pyecharts

pyecharts is mainly displayed based on a web browser, and there are many graphics drawn, including line charts, histograms, pie charts, funnel charts, polar coordinates, and so on. The amount of drawing code using pyecharts is very small, but the graphics drawn are more beautiful.

pyecharts is divided into two major versions, v0.5.X and v1, v0.5.X and v1 are not compatible, v1 is a brand new version v0.5.X supports Python2.7, 3.4+.

The development team decided that the 0.5.x version will no longer be maintained, the 0.5.x version code is located in the 05x branch, v1 only supports Python 3.6+, and the new version series will start from v1.0.0.

This article is mainly based on pyecharts 1.7.1 version to display the installation commands as follows:


pip install pyecharts==1.7.1

Two, pyecharts histogram/bar chart full solution

1. Basic bar chart/bar chart

'''
如有需要Python学习资料的小伙伴可以加群领取:1136201545
'''


from pyecharts import options as opts
from pyecharts.charts import Bar
l1=['星期一','星期二','星期三','星期四','星期五','星期七','星期日']
l2=[100,200,300,400,500,400,300]
bar = (
    Bar()
    .add_xaxis(l1)
    .add_yaxis("基本柱状图", l2)
    .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
)
bar.render_notebook()

 

Parameter introduction:
add_xaxis: add the abscissa, need to pass in the list add_yaxis: add the ordinate, need to pass in the list, cut the list element as a value

2. Add axis name


from pyecharts import options as opts
from pyecharts.charts import Bar
l1=['星期一','星期二','星期三','星期四','星期五','星期七','星期日']
l2=[100,200,300,400,500,400,300]
bar = (
    Bar()
    .add_xaxis(l1)
    .add_yaxis("基本柱状图", l2)
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Bar-基本示例"),
        yaxis_opts=opts.AxisOpts(name="人流量"),
        xaxis_opts=opts.AxisOpts(name="星期"),)
)
bar.render_notebook()

3. Histogram/bar graph with multiple ordinates


from pyecharts import options as opts
from pyecharts.charts import Bar
l1=['星期一','星期二','星期三','星期四','星期五','星期七','星期日']
l2=[100,200,300,400,500,400,300]
l3=[300,400,500,400,300,200,100]
bar = (
    Bar()
    .add_xaxis(l1)
    .add_yaxis("l2", l2)
    .add_yaxis("l3", l3)
    .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"),
                    toolbox_opts=opts.BrushOpts(),)
)
bar.render_notebook()

 

opts.BrushOpts() is the circle selection tool, as shown in the upper right corner of the figure

4. Set the interval and color of the histogram


from pyecharts import options as opts
from pyecharts.charts import Bar
l1=['星期一','星期二','星期三','星期四','星期五','星期七','星期日']
l2=[100,200,300,400,500,400,300]
bar = (
    Bar()
    .add_xaxis(l1)
    .add_yaxis("l2",l2,category_gap=0, color='#FFFF00')
    .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
)
bar.render_notebook()

 

category_gap: set interval

color: set the color of the histogram

5. Horizontal histogram


from pyecharts import options as opts
from pyecharts.charts import Bar
l1=['星期一','星期二','星期三','星期四','星期五','星期七','星期日']
l2=[100,200,300,400,500,400,300]
l3=[300,400,500,400,300,200,100]
bar = (
    Bar()
    .add_xaxis(l1)
    .add_yaxis("l2", l2)
    .add_yaxis("l3", l3)
    .reversal_axis()
    .set_series_opts(label_opts=opts.LabelOpts(position="right"))
    .set_global_opts(title_opts=opts.TitleOpts(title="横向柱状图"))
)
bar.render_notebook()

 

reversal_axis reverses the graph

position="right" means to display the value on the right side of the graph, similarly, left and center mean the left and middle respectively

6. Display the maximum, minimum and average values

a. Marking line


from pyecharts import options as opts
from pyecharts.charts import Bar
import random
l1=['星期一','星期二','星期三','星期四','星期五','星期七','星期日']
l2=[100,200,300,400,500,400,300]
bar = (
    Bar()
    .add_xaxis(l1)
    .add_yaxis("l2", l2)
    .set_global_opts(title_opts=opts.TitleOpts(title="标记线柱状图"))
    .set_series_opts(
        label_opts=opts.LabelOpts(is_show=False),
        markline_opts=opts.MarkLineOpts(
            data=[
                opts.MarkLineItem(type_="min", name="最小值"),
                opts.MarkLineItem(type_="max", name="最大值"),
                opts.MarkLineItem(type_="average", name="平均值"),
            ]
        ),
    )
)
bar.render_notebook()

 

b. Marking point


from pyecharts import options as opts
from pyecharts.charts import Bar
import random
l1=['星期一','星期二','星期三','星期四','星期五','星期七','星期日']
l2=[100,200,300,400,500,400,300]
bar = (
    Bar()
    .add_xaxis(l1)
    .add_yaxis("l2", l2)
    .set_global_opts(title_opts=opts.TitleOpts(title="标记线柱状图"))
    .set_series_opts(
        label_opts=opts.LabelOpts(is_show=False),
        markpoint_opts=opts.MarkPointOpts(
            data=[
                opts.MarkPointItem(type_="min", name="最小值"),
                opts.MarkPointItem(type_="max", name="最大值"),
                opts.MarkPointItem(type_="average", name="平均值"),
            ]
        ),
    )
)
bar.render_notebook()

7. Rotate the x-axis coordinate


from pyecharts import options as opts
from pyecharts.charts import Bar
import random
l1=['很长很长很长很长很长的坐标轴{}'.format(i) for i in range(10)]
l2=[random.choice(range(10,100,10)) for i in range(10)]
bar = (
    Bar()
    .add_xaxis(l1)
    .add_yaxis("l2", l2)
    .set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
                     title_opts=opts.TitleOpts(title="Bar-旋转X轴标签", subtitle="解决标签名字过长的问题"))
)
bar.render_notebook()

 

rotate=-15 means to rotate the coordinate axis counterclockwise by 15 degrees

8. Abscissa zoom

a. Overall zoom (type_="inside")


from pyecharts import options as opts
from pyecharts.charts import Bar
import random
l1=['{}日'.format(i) for i in range(1,31)]
l2=[random.choice(range(100,3100,100)) for i in range(1,31)]
bar = (
    Bar()
    .add_xaxis(l1)
    .add_yaxis("l2", l2)
    .set_global_opts(title_opts=opts.TitleOpts(title="区域缩放柱状图"),
                     datazoom_opts=opts.DataZoomOpts(type_="inside"))
)
bar.render_notebook()

 

b. Swipe left and right to zoom


from pyecharts import options as opts
from pyecharts.charts import Bar
import random
l1=['{}日'.format(i) for i in range(1,31)]
l2=[random.choice(range(100,3100,100)) for i in range(1,31)]
bar = (
    Bar()
    .add_xaxis(l1)
    .add_yaxis("l2", l2)
    .set_global_opts(title_opts=opts.TitleOpts(title="区域缩放柱状图"),
                     datazoom_opts=opts.DataZoomOpts(type_="slider"))
)
bar.render_notebook()

 

This time I mainly introduce the common forms of pyecharts histogram, and the advanced usage of pyecharts histogram will come out later, so stay tuned!

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/114928387