In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Python pyecharts how to draw the bar chart related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Python pyecharts how to draw the bar chart article will have a harvest, let's take a look.
I. brief introduction
Pyecharts is an open source data visualization by Baidu. With its good interaction and exquisite chart design, it has been recognized by many developers. Python is an expressive language, which is very suitable for data processing. When data analysis meets data visualization, pyecharts is born. Echarts is written in JS, while we can use Python to call API in pyecharts.
Advantages:
Simple API design, smooth use and support for chain calls
Including 30 kinds of common charts, everything.
Support for mainstream Notebook environments, Jupyter Notebook and JupyterLab
Can be easily integrated into mainstream Web frameworks such as Flask,Django
Highly flexible configuration items can be easily matched with exquisite charts
As many as 400 + map files and native Baidu maps provide strong support for geographic data visualization.
2. Collate data
Installation:
Pip install pyecharts1, configuration topic Bar (init_opts=opts.InitOpts (theme=ThemeType.LIGHT)) # first kind of Bar ({"theme": ThemeType.MACARONS}) # second kind
2. Bar chart Bar-Bar_base_dict_configimport osfrom matplotlib import pyplot as plt from pyecharts import options as optsfrom pyecharts.charts import Barfrom pyecharts.commons.utils import JsCodefrom pyecharts.globals import ThemeTypelist1=cnbodfsort ['REGION'] .tolist () list2=cnbodfsort [' PRICE'] .tolist () list3=cnbodfsort ['PERSONS'] .tolist () c = (Bar (init_opts=opts.InitOpts (theme=ThemeType.LIGHT)) .add _ xaxis (list1) .add _ yaxis ("fare", list2, stack= "stack1" Category_gap= "50%") .add _ yaxis ("person-times", list3, stack= "stack1", category_gap= "50%") .set _ series_opts (label_opts=opts.LabelOpts (position= "right", formatter=JsCode ("function (x) {return Number (x.data) .tofixed (2)) ) .set _ global_opts (xaxis_opts=opts.AxisOpts (axislabel_opts=opts.LabelOpts (rotate=-15)), title_opts=opts.TitleOpts (title=' Chinese Film Box Office) Subtitle=' compare fares by region with passengers') # c.render ("cnbo1.html") # generate html images # os.system ("cnbo01.html") # pop up pictures directly after code execution c.render_notebook () # display pictures directly in the code area
3. Sample data Faker.choose ()
Using this code randomly calls the sample parameters of the system:
.add _ xaxis (Faker.choose ())
From pyecharts.faker import Fakerlist1=cnbodfsort ['REGION'] .tolist () list2=cnbodfsort [' PRICE'] .tolist () list3=cnbodfsort ['PERSONS'] .tolist () c = (Bar ({"theme": ThemeType.MACARONS}) # configure a nice chart theme! .add _ xaxis (Faker.choose ()) # this sentence means to use random background sample data. Add _ yaxis ("fare", list2, stack= "stack1", category_gap= "50%") .add _ yaxis ("person-times", list3, stack= "stack1", category_gap= "50%") .set _ series_opts (label_opts=opts.LabelOpts (position= "right") Formatter=JsCode ("function (x) {return Number (x.data) .tofixed (2)) ) .set _ global_opts (xaxis_opts=opts.AxisOpts (axislabel_opts=opts.LabelOpts (rotate=-15)), title_opts= {"text": "sample data" "subtext": "use Faker.choose ()"}) c.render ("cnbo1.html") # generate html image # os.system ("cnbo1.html") # pop up the picture directly after executing the code c.render_notebook () # display the picture 4, scroll bar Bar-Bar_datazoom_sliderdatazoom_opts=opts.DataZoomOpts () directly in the code area
Represents a scroll bar that can be slid:
List1=cnbodfsort ['REGION'] .tolist () list2=cnbodfsort [' PRICE'] .tolist () list3=cnbodfsort ['PERSONS'] .tolist () c = (Bar (init_opts=opts.InitOpts (theme=ThemeType.LIGHT)) .add _ xaxis (list1) .add _ yaxis ("fare", list2, stack= "stack1", category_gap= "50%") .add _ yaxis ("person-times", list3, stack= "stack1") Category_gap= "50%") .set _ series_opts (label_opts=opts.LabelOpts (position= "right", formatter=JsCode ("function (x) {return Number (x.data) .tofixed (2)) } "),) .set _ global_opts (xaxis_opts=opts.AxisOpts (axislabel_opts=opts.LabelOpts (rotate=-15)), title_opts=opts.TitleOpts (title=' Chinese Film Box Office', subtitle=' compares ticket prices with people by region'), brush_opts=opts.BrushOpts () # use this to add some tools datazoom_opts=opts.DataZoomOpts () to the upper right corner of the picture, and # to add scroll bars at the bottom)) c.render ("cnbo2.html") # generate html images # os.system ("cnbo01.html") # pop up pictures directly after code execution c.render_notebook () # display pictures directly in the code area
5. Mouse movement effect Bar-Bar_datazoom_inside
Zoom in and out according to the mouse:
From pyecharts import options as optsfrom pyecharts.charts import Barfrom pyecharts.faker import Fakerc = (Bar () .add _ xaxis (Faker.days_attrs) .add _ yaxis ("merchant A", Faker.days_values, color=Faker.rand_color ()) .set _ global_opts (title_opts=opts.TitleOpts (title= "Bar-DataZoom (inside)"), datazoom_opts=opts.DataZoomOpts (type_= "inside"),) .render ("bar_datazoom_inside.html"))
6. Display the most value Bar-Bar_markpoint_typelist1=cnbodfsort ['REGION'] .tolist () list2=cnbodfsort [' PRICE'] .tolist () list3=cnbodfsort ['PERSONS'] .tolist () c = (Bar (init_opts=opts.InitOpts (theme=ThemeType.HALLOWEEN)) .add _ xaxis (list1) .add _ yaxis ("fare", list2, stack= "stack1", category_gap= "50%") .add _ yaxis ("person-time", list3, stack= "stack1") Category_gap= "50%") .set _ series_opts (label_opts=opts.LabelOpts (position= "right", formatter=JsCode ("function (x) {return Number (x.data) .tofixed (2)) } "), markpoint_opts=opts.MarkPointOpts (# data= [opts.MarkPointItem (type_=" max ", name=" maximum "), opts.MarkPointItem (type_=" min ", name=" minimum "), opts.MarkPointItem (type_=" average ", name=" average "),])) #) .set _ global_opts (xaxis_opts=opts.AxisOpts (axislabel_opts=opts.LabelOpts (rotate=-15)), title_opts=opts.TitleOpts (title=' Chinese Cinema Box Office', subtitle=' compares ticket prices with people by region'), brush_opts=opts.BrushOpts (), # use this to add some tools to the upper right corner of the image, datazoom_opts=opts.DataZoomOpts (orient='vertical') ) c.render ("cnbo2.html") # generate html image # os.system ("cnbo01.html") # pop up the picture directly after executing the code c.render_notebook () # display the picture directly in the code area
7. Change the scroll bar Bar-Bar_datazoom_slider_verticallist1=cnbodfsort ['REGION'] .tolist () list2=cnbodfsort [' PRICE'] .tolist () list3=cnbodfsort ['PERSONS'] .tolist () c = (Bar (init_opts=opts.InitOpts (theme=ThemeType.CHALK)) .add _ xaxis (list1) .add _ yaxis ("fare", list2, stack= "stack1", category_gap= "50%") .add _ yaxis ("person-times", list3, stack= "stack1") Category_gap= "50%") .set _ series_opts (label_opts=opts.LabelOpts (position= "right", formatter=JsCode ("function (x) {return Number (x.data) .tofixed (2)) } "),) .set _ global_opts (xaxis_opts=opts.AxisOpts (axislabel_opts=opts.LabelOpts (rotate=-15)), title_opts=opts.TitleOpts (title=' Chinese Film Box Office', subtitle=' compares ticket prices with people by region'), brush_opts=opts.BrushOpts () # use this to add some tools to the upper right corner of the image datazoom_opts=opts.DataZoomOpts (orient='vertical'),) c.render ("cnbo2.html") # generate html images # os.system ("cnbo01.html") # pop up pictures directly after code execution c.render_notebook () # display pictures directly in the code area
8. Multiple Y-axis colors= ['# 5793f3 'list4=cnbodfsort [' 5793f3']] legend_list= ['box office', 'person-times', 'price', 'evaluation'] list1=cnbodfsort ['REGION'] .tolist () list2=cnbodfsort [' PRICE'] .tolist () list3=cnbodfsort ['PERSONS'] .tolist () list4=cnbodfsort [' BO'] .tolist () list5=cnbodfsort ['points'] .tolist () c = (Bar (init_opts=opts.InitOpts (theme=ThemeType.CHALK,width= "1600px")) Height= "600px") .add _ xaxis (list1) .add _ yaxis ("rating", list5,yaxis_index=0,category_gap= "50%", color=colors [2]) .add _ yaxis ("fare", list2,yaxis_index=0,category_gap= "50%", color=colors [0]) .add _ yaxis ("person-time", list3,yaxis_index=0,category_gap= "50%" Color=colors [1]) .set _ series_opts (label_opts=opts.LabelOpts (position= "top", formatter=JsCode ("function (x) {return Number (x.data) .tofixed (2)) } "), markpoint_opts=opts.MarkPointOpts (data= [opts.MarkPointItem (type_=" max ", name=" maximum "), opts.MarkPointItem (type_=" min ", name=" minimum "), opts.MarkPointItem (type_=" average ", name=" average "),])) ) .extend _ axis (yaxis=opts.AxisOpts (name= "box office", type_= "value", min_=1000, max_=150000, interval=10000, position= "right", axislabel_opts=opts.LabelOpts (formatter= "{value} million")) .extend _ axis (yaxis=opts.AxisOpts (name= "rating", type_= "value") Min_=0, max_=11, interval=1, position= "left", axislabel_opts=opts.LabelOpts (formatter= "{value} Point"), axisline_opts=opts.AxisLineOpts (linestyle_opts=opts.LineStyleOpts (color=colors [2])), splitline_opts=opts.SplitLineOpts (is_show=True,linestyle_opts=opts.LineStyleOpts (opacity=1)) ) .set _ global_opts (yaxis_opts=opts.AxisOpts (type_= "value", name= "fare", min_=10, max_=70, position= "right", offset=80) Axisline_opts=opts.AxisLineOpts (linestyle_opts=opts.LineStyleOpts (color=colors [0])), axislabel_opts=opts.LabelOpts (formatter= "{value} yuan"),), tooltip_opts=opts.TooltipOpts (trigger= "axis", axis_pointer_type= "cross"), datazoom_opts=opts.DataZoomOpts (orient='vertical'), toolbox_opts=opts.ToolboxOpts (pos_left='120%') Legend_opts=opts.LegendOpts (is_show=False),) line = (Line () .add _ xaxis (xaxis_data=x_data) .add _ yaxis (series_name= "Box Office", yaxis_index=1, y_axis=list4, label_opts=opts.LabelOpts (is_show=False),) c.render_notebook () # display the picture directly in the code area
Double Y axis:
9. Histogram Bar-Bar_histogram# Bar-Bar_histogramfrom pyecharts.options.global_options import ThemeTypefrom pyecharts import options as optsfrom pyecharts.charts import Barfrom pyecharts.faker import Fakerc = (Bar ({"theme": ThemeType.DARK}) .add _ xaxis (cnboregiongb.index.tolist ()) .add _ yaxis ("quantity", cnboregiongb.values.tolist (), category_gap=0 Color=Faker.rand_color () .set _ global_opts (title_opts=opts.TitleOpts (title= "Bar- histogram")) c.render_notebook ()
This is the end of the article on "how to draw a bar chart in Python pyecharts". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to draw a bar chart in Python pyecharts". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.