Mastering pyecharts Bar Charts: From Basics to Advanced Customizations
This article walks through installing pyecharts, explains its version differences, and demonstrates a series of bar‑chart techniques—including basic charts, axis labeling, multiple series, styling, horizontal orientation, mark lines/points, label rotation, and interactive zoom—complete with code snippets and visual examples.
1. Introduction to pyecharts
pyecharts renders charts in a web browser and supports many chart types such as line, bar, pie, funnel, map, and polar charts. It requires minimal code and produces aesthetically pleasing graphics.
Two major versions exist: 0.5.x (compatible with Python 2.7 and 3.4+, now discontinued) and 1.x (requires Python 3.6+). This tutorial uses pyecharts 1.7.1.
pip install pyecharts==1.7.12. Basic Bar Chart
from pyecharts import options as opts
from pyecharts.charts import Bar
l1 = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
l2 = [100,200,300,400,500,400,300]
bar = (
Bar()
.add_xaxis(l1)
.add_yaxis("Basic Bar", l2)
.set_global_opts(title_opts=opts.TitleOpts(title="Bar‑Basic Example", subtitle="Subtitle"))
)
bar.render_notebook()3. Adding Axis Names
from pyecharts import options as opts
from pyecharts.charts import Bar
l1 = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
l2 = [100,200,300,400,500,400,300]
bar = (
Bar()
.add_xaxis(l1)
.add_yaxis("Basic Bar", l2)
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar‑Basic Example"),
yaxis_opts=opts.AxisOpts(name="Visitors"),
xaxis_opts=opts.AxisOpts(name="Day"),
)
)
bar.render_notebook()4. Multiple Y‑Axes
from pyecharts import options as opts
from pyecharts.charts import Bar
l1 = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
l2 = [100,200,300,400,500,400,300]
l3 = [300,400,500,400,300,200,100]
bar = (
Bar()
.add_xaxis(l1)
.add_yaxis("Series A", l2)
.add_yaxis("Series B", l3)
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar‑Multiple Series", subtitle="Subtitle"),
toolbox_opts=opts.BrushOpts(),
)
)
bar.render_notebook()5. Adjusting Gap and Color
from pyecharts import options as opts
from pyecharts.charts import Bar
l1 = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
l2 = [100,200,300,400,500,400,300]
bar = (
Bar()
.add_xaxis(l1)
.add_yaxis("Series", l2, category_gap=0, color='#FFFF00')
.set_global_opts(title_opts=opts.TitleOpts(title="Bar‑Custom Gap & Color", subtitle="Subtitle"))
)
bar.render_notebook()6. Horizontal Bar Chart
from pyecharts import options as opts
from pyecharts.charts import Bar
l1 = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
l2 = [100,200,300,400,500,400,300]
l3 = [300,400,500,400,300,200,100]
bar = (
Bar()
.add_xaxis(l1)
.add_yaxis("Series A", l2)
.add_yaxis("Series B", l3)
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
.set_global_opts(title_opts=opts.TitleOpts(title="Horizontal Bar Chart"))
)
bar.render_notebook()7. Mark Lines for Max/Min/Average
from pyecharts import options as opts
from pyecharts.charts import Bar
l1 = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
l2 = [100,200,300,400,500,400,300]
bar = (
Bar()
.add_xaxis(l1)
.add_yaxis("Series", l2)
.set_global_opts(title_opts=opts.TitleOpts(title="Mark Line Bar"))
.set_series_opts(
label_opts=opts.LabelOpts(is_show=False),
markline_opts=opts.MarkLineOpts(data=[
opts.MarkLineItem(type_="min", name="Min"),
opts.MarkLineItem(type_="max", name="Max"),
opts.MarkLineItem(type_="average", name="Average"),
]),
)
)
bar.render_notebook()8. Mark Points for Max/Min/Average
from pyecharts import options as opts
from pyecharts.charts import Bar
l1 = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
l2 = [100,200,300,400,500,400,300]
bar = (
Bar()
.add_xaxis(l1)
.add_yaxis("Series", l2)
.set_global_opts(title_opts=opts.TitleOpts(title="Mark Point Bar"))
.set_series_opts(
label_opts=opts.LabelOpts(is_show=False),
markpoint_opts=opts.MarkPointOpts(data=[
opts.MarkPointItem(type_="min", name="Min"),
opts.MarkPointItem(type_="max", name="Max"),
opts.MarkPointItem(type_="average", name="Average"),
]),
)
)
bar.render_notebook()9. Rotating X‑Axis Labels
from pyecharts import options as opts
from pyecharts.charts import Bar
l1 = ['VeryLongLabel{}'.format(i) for i in range(10)]
l2 = [random.choice(range(10,100,10)) for _ in range(10)]
bar = (
Bar()
.add_xaxis(l1)
.add_yaxis("Series", l2)
.set_global_opts(
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
title_opts=opts.TitleOpts(title="Bar‑Rotated X‑Labels", subtitle="Handling Long Labels"),
)
)
bar.render_notebook()10. Data Zoom (Inside and Slider)
from pyecharts import options as opts
from pyecharts.charts import Bar
l1 = ['{} Day'.format(i) for i in range(1,31)]
l2 = [random.choice(range(100,3100,100)) for _ in range(1,31)]
# Inside zoom
bar_inside = (
Bar()
.add_xaxis(l1)
.add_yaxis("Series", l2)
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar with Inside Zoom"),
datazoom_opts=opts.DataZoomOpts(type_="inside"),
)
)
bar_inside.render_notebook()
# Slider zoom
bar_slider = (
Bar()
.add_xaxis(l1)
.add_yaxis("Series", l2)
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar with Slider Zoom"),
datazoom_opts=opts.DataZoomOpts(type_="slider"),
)
)
bar_slider.render_notebook()This tutorial covered the most common pyecharts bar‑chart forms and hinted at upcoming advanced usage; stay tuned for more.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
