SoFunction
Updated on 2024-11-21

Python pyecharts plotting bar charts

First, pyecharts draw bar graph syntax introduction

Column/bar charts that represent the size of the data by the height of the column/width of the bar.

() Method Signature:

add(name, x_axis, y_axis,
is_stack=False,
bar_category_gap='20%', **kwargs)

  • name->str Legend name
  • x_axis->list xAxis data
  • y_axis->list yAxis data
  • is_stack->bool Data stacking, series on the same class axis configured with the samestackValues can be stacked and placed
  • bar_category_gap->int/str Distance between the bars of the category axis, when set to 0 the bars are next to each other (histogram type), default is '20%'.
  • mark_point mark_point Takes the following values.average min max
  • mark_line mark_line Takes the following values.average min max
  • is_convert=True The x-axis and y-axis are exchanged
  • is_label_show=True Show data labels
  • xaxis_rotate=30, yaxis_rotate=30 Rotate the x-axis or y-axis labels
  • label_color Setting the Column Color

II. Plotting ordinary bar charts

from pyecharts import Bar
#Note: Global configuration items should be set on the last add(), otherwise the settings will be flushed.

attr = ["Shirt.", "Wool sweater.", "Chiffon shirt.", "Pants.", "High heels.", "Socks.","Mattress."]
v1 = [5, 20, 36, 10, 75, 90 , 30]
v2 = [10, 25, 8, 60, 20, 80 , 50]
bar = Bar("Example of Bar Chart Data Stacking") #Set Title
("Merchant A", attr, v1,mark_point=["average"],is_label_show=True)
("Merchant B", attr, v2,mark_line=["min", "max"],is_label_show=True)
(r'C:\Users\ASUS\Desktop\Restart\Python Plotting with pyecharts\Histogram') # Generate HTML files


III. Drawing stacked bar charts

from pyecharts import Bar

#Note: Global configuration items should be set on the last add(), otherwise the settings will be flushed.
#is_stack->bool Data stacking, series configured with the same stack value on the same class axis can be stacked for placement
attr = ["Shirt.", "Wool sweater.", "Chiffon shirt.", "Pants.", "High heels.", "Socks.","Mattress."]
v1 = [5, 20, 36, 10, 75, 90 , 30]
v2 = [10, 25, 8, 60, 20, 80 , 50]
bar = Bar("Example of Bar Chart Data Stacking") #Set Title
("Merchant A", attr, v1,mark_point=["average"],is_label_show=True,is_stack=True)
("Merchant B", attr, v2,mark_line=["min", "max"],is_label_show=True,is_stack=True)
(r'C:\Users\ASUS\Desktop\Restart\Python Plotting with pyecharts\Histogram') # Generate HTML files

IV. Plotting horizontal bar charts

from pyecharts import Bar

#is_convert=True x-axis and y-axis swap
#Note: Global configuration items should be set on the last add(), otherwise the settings will be flushed.

attr = ["Shirt.", "Wool sweater.", "Chiffon shirt.", "Pants.", "High heels.", "Socks.","Mattress."]
v1 = [5, 20, 36, 10, 75, 90 , 30]
v2 = [10, 25, 8, 60, 20, 80 , 50]
bar = Bar("Example of Bar Chart Data Stacking") #Set Title
("Merchant A", attr, v1,mark_point=["average"],is_label_show=True,is_convert=True)
("Merchant B", attr, v2,mark_line=["min", "max"],is_label_show=True,is_convert=True)
(r'C:\Users\ASUS\Desktop\Restart\Python Plotting with pyecharts\Histogram') # Generate HTML files

V. pyecharts bar chart datazoom case

Six, corresponding to pyecharts bar chart datazoom case code I put together with page

#coding=utf-8
from __future__ import unicode_literals
from pyecharts import Bar
from pyecharts import Page
import random
page = Page()
#dataZoom effect, 'slider' type
attr = ["{}sky".format(i) for i in range(30)]
v1 = [(1, 30) for _ in range(30)]
bar = Bar("Bar - datazoom - slider example")
("", attr, v1, is_label_show=True, is_datazoom_show=True,xaxis_rotate=30, yaxis_rotate=30)
(bar)

#dataZoom effect, 'inside' type
attr = ["{}sky".format(i) for i in range(30)]
v1 = [(1, 30) for _ in range(30)]
bar2 = Bar("Bar - datazoom - inside example")
(
    "",
    attr,
    v1,
    is_datazoom_show=True,
    datazoom_type="inside",
    datazoom_range=[1, 50],
)
(bar2)
#dataZoom effect, 'both' type
attr = ["{}sky".format(i) for i in range(30)]
v1 = [(1, 30) for _ in range(30)]
bar3 = Bar("Bar - datazoom - both examples")
(
    "",
    attr,
    v1,
    is_datazoom_show=True,
    datazoom_type="both",
    datazoom_range=[1, 40],
    label_color=["#749f83"]
)
(bar3)
days = ["{}sky".format(i) for i in range(30)]
days_v1 = [(1, 30) for _ in range(30)]
bar4 = Bar("Bar - datazoom - xaxis/yaxis example")
(
    "",
    days,
    days_v1,
    # Defaults to X-axis, landscape
    is_datazoom_show=True,
    datazoom_type="slider",
    datazoom_range=[1, 50],
    # Add an additional dataZoom control bar, vertical
    is_datazoom_extra_show=True,
    datazoom_extra_type="slider",
    datazoom_extra_range=[1, 50],
    is_toolbox_show=False,
)
(bar4)
(r'C:\Users\ASUS\Desktop\Restart\Python Plotting with pyecharts\Histogram')

Up to this point this article onPython pyechartsDrawing a bar graph article is introduced to this, more related to draw a bar graph content please search for my previous posts or continue to browse the following related articles I hope you will support me more in the future!

This article is excerpted from/#/zh-cn/charts_base?id=bar%ef%bc%88%e6%9f%b1%e7%8a%b6%e5%9b%be%e6%9d%a1%e5%bd%a2%e5%9b%be%ef%bc%89