Python繪制柱狀圖可視化神器pyecharts
pyecharts介紹
pyecharts是python與echarts鏈接,一個(gè)用于生成Echarts圖標(biāo)的第三方庫(kù),pyecharts分為v0.5.X和v1兩個(gè)大版本,兩者互不兼容,v1是一個(gè)全新的版本,經(jīng)研發(fā)團(tuán)隊(duì)決定,前者將不再進(jìn)行更新維護(hù)。下面是我個(gè)人整理的關(guān)于pyecharts繪制柱狀圖的案例大全,收集整理不易,多多支持!
特性:
- 簡(jiǎn)潔的 API 設(shè)計(jì),使用如絲滑般流暢,支持鏈?zhǔn)秸{(diào)用
- 囊括了 30+ 種常見(jiàn)圖表,應(yīng)有盡有
- 支持主流 Notebook 環(huán)境,Jupyter Notebook 和 JupyterLab
- 可輕松集成至 Flask,Django 等主流 Web 框架
- 高度靈活的配置項(xiàng),可輕松搭配出精美的圖表
- 詳細(xì)的文檔和示例,幫助開(kāi)發(fā)者更快的上手項(xiàng)目
- 多達(dá) 400+ 地圖文件以及原生的百度地圖,為地理數(shù)據(jù)可視化提供強(qiáng)有力的支持
優(yōu)勢(shì)
pyecharts可以輸出網(wǎng)頁(yè)版的鏈接,直接調(diào)用資源渲染圖表,方便快捷,輸出不是圖片,而是一個(gè)可以調(diào)節(jié)的頁(yè)面,動(dòng)態(tài),炫酷,都是它的天地!它可以支持在手機(jī)端瀏覽界面,也可以修改相關(guān)參數(shù),總的來(lái)說(shuō)方便至極,而且主題都可以隨意搭配,顏色自己調(diào)。適用于公司可視化報(bào)表,企業(yè)展示,日常辦公,由于圖表過(guò)于炫酷,不大適合做科研論文展示,后期會(huì)介紹另外一個(gè)庫(kù),可以作為科研黨的首選——matplotlib
展示
總之pyecharts科研繪制很多一般繪制不了的圖形,作為一個(gè)可視化神器它的便利之處,只有用過(guò)的小伙伴才知道,什么叫 “工欲善其事必先利其器”的道理。
柱狀圖模板系列
水晶柱狀圖
水晶柱狀圖適用于幾個(gè)數(shù)據(jù)的對(duì)比,生成的柱狀圖具有一種玲瓏剔透,清水出芙蓉的美感和清新,最適合做可視化展示。
# -*- coding : utf-8 -*- # @Software : PyCharm # @File : 水晶柱狀圖.py # @CSDN : https://blog.csdn.net/weixin_47723732 from pyecharts import options as opts from pyecharts.charts import Bar from pyecharts.commons.utils import JsCode data_x = ['可樂(lè)', '雪碧', '橙汁', '綠茶', '奶茶', '百威', '青島'] data_y = [147, 53, 27, 123, 94, 118, 48] c = ( Bar() .add_xaxis(data_x) .add_yaxis("商家A", data_y, category_gap="60%") .set_series_opts( itemstyle_opts={ "normal": { "color": JsCode( """new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgba(0, 244, 255, 1)' }, { offset: 1, color: 'rgba(0, 77, 167, 1)' }], false)""" ), "barBorderRadius": [30, 30, 30, 30], "shadowColor": "rgb(0, 160, 221)", } } ) .set_global_opts(title_opts=opts.TitleOpts(title="標(biāo)題"), xaxis_opts=opts.AxisOpts( name='類別', name_location='middle', name_gap=30, # 標(biāo)簽與軸線之間的距離,默認(rèn)為20,最好不要設(shè)置20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標(biāo)簽字體大小 )), yaxis_opts=opts.AxisOpts( name='數(shù)量', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), # toolbox_opts=opts.ToolboxOpts() # 工具選項(xiàng) ) .render("水晶柱狀圖.html") )
解決X軸標(biāo)簽過(guò)長(zhǎng)的柱狀圖
有時(shí)候我們?cè)诶L制柱狀圖的時(shí)候,X軸標(biāo)簽過(guò)長(zhǎng)導(dǎo)致圖形顯示的不正常,修改字?jǐn)?shù)之后又怕展示效果不夠明顯。此圖例解決了這個(gè)難題,適合展示X軸標(biāo)簽過(guò)長(zhǎng)的問(wèn)題。
# -*- coding : utf-8 -*- # @Software : PyCharm # @File : 解決X軸標(biāo)簽過(guò)長(zhǎng)的問(wèn)題.py from pyecharts import options as opts from pyecharts.charts import Bar c = ( Bar() .add_xaxis( [ "名字很長(zhǎng)的X軸標(biāo)簽1", "名字很長(zhǎng)的X軸標(biāo)簽2", "名字很長(zhǎng)的X軸標(biāo)簽3", "名字很長(zhǎng)的X軸標(biāo)簽4", "名字很長(zhǎng)的X軸標(biāo)簽5", "名字很長(zhǎng)的X軸標(biāo)簽6", ] ) .add_yaxis("商家A", [10, 20, 30, 40, 50, 40]) .add_yaxis("商家B", [20, 10, 40, 30, 40, 50]) .set_global_opts( xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15), # name='類型', # name_location='middle', # name_gap=30, # 標(biāo)簽與軸線之間的距離,默認(rèn)為20,最好不要設(shè)置20 # name_textstyle_opts=opts.TextStyleOpts( # font_family='Times New Roman', # font_size=16 # 標(biāo)簽字體大小 # ) ), yaxis_opts=opts.AxisOpts( name='數(shù)量', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), title_opts=opts.TitleOpts(title="標(biāo)題1", subtitle="標(biāo)題2"), # toolbox_opts=opts.ToolboxOpts() # 工具選項(xiàng) ) .render("解決X軸標(biāo)簽過(guò)長(zhǎng)問(wèn)題.html") ) print("圖表已生成!請(qǐng)查收!")
自定義平均刻度標(biāo)簽(方便查看超出范圍)
有時(shí)候數(shù)據(jù)過(guò)于多,柱狀圖過(guò)于密集,我們知道一個(gè)平均數(shù),需要快速的查看那些數(shù)據(jù)超過(guò)了這個(gè)閾值,那些數(shù)據(jù)低于平均值,這時(shí)候我們可以自己定義一個(gè)刻度標(biāo)簽,方便我們理解。
# -*- coding : utf-8 -*- # @Software : PyCharm # @File : 自定義平均刻度.py from pyecharts import options as opts from pyecharts.charts import Bar from pyecharts.globals import ThemeType data_x=['周一', '周二', '周三', '周四', '周五', '周六', '周日'] data_y_1=[40, 119, 79, 83, 107, 133, 95] data_y_2=[20, 143, 74, 97, 92, 53, 66] c = ( Bar({"theme": ThemeType.MACARONS}) .add_xaxis(data_x) .add_yaxis("商家A", data_y_1) .add_yaxis("商家B", data_y_2) .set_series_opts( label_opts=opts.LabelOpts(is_show=False), markline_opts=opts.MarkLineOpts( data=[opts.MarkLineItem(y=50, name="yAxis=50")] ), ) .set_global_opts(title_opts=opts.TitleOpts(title="標(biāo)題"), xaxis_opts=opts.AxisOpts( name='類別', name_location='middle', name_gap=30, # 標(biāo)簽與軸線之間的距離,默認(rèn)為20,最好不要設(shè)置20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標(biāo)簽字體大小 )), yaxis_opts=opts.AxisOpts( name='數(shù)量', name_location='middle', name_gap=40, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), # datazoom_opts=opts.DataZoomOpts(type_="inside"), #鼠標(biāo)可以滑動(dòng)控制 # toolbox_opts=opts.ToolboxOpts() # 工具選項(xiàng) # brush_opts=opts.BrushOpts() #可以保存選擇 ) .render("顯示平均刻度.html") ) print("圖表已生成!請(qǐng)查收!")
翻轉(zhuǎn)X Y軸柱狀圖
直觀的展示柱狀圖過(guò)于單調(diào),有時(shí)候我們需要適當(dāng)?shù)恼{(diào)整一下這個(gè)主題,把xy軸翻轉(zhuǎn)一下,這樣更能直觀的對(duì)比顯示,適用多個(gè)數(shù)據(jù)類別進(jìn)行比較。
# -*- coding : utf-8 -*- # @Software : PyCharm # @File : 翻轉(zhuǎn)XY軸.p from pyecharts import options as opts from pyecharts.charts import Bar data_x = ['可樂(lè)', '雪碧', '橙汁', '綠茶', '奶茶', '百威', '青島'] data_y = [147, 53, 27, 123, 94, 118, 48] c = ( Bar() .add_xaxis(data_x) .add_yaxis("商家A", data_y) .add_yaxis("商家B", data_y) .reversal_axis() .set_series_opts(label_opts=opts.LabelOpts(position="right")) .set_global_opts(title_opts=opts.TitleOpts(title="標(biāo)題"), xaxis_opts=opts.AxisOpts( name='數(shù)量', name_location='middle', name_gap=30, # 標(biāo)簽與軸線之間的距離,默認(rèn)為20,最好不要設(shè)置20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標(biāo)簽字體大小 )), yaxis_opts=opts.AxisOpts( name='類別', name_location='middle', name_gap=40, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), # datazoom_opts=opts.DataZoomOpts(type_="inside"), #鼠標(biāo)可以滑動(dòng)控制 # toolbox_opts=opts.ToolboxOpts() # 工具選項(xiàng) # brush_opts=opts.BrushOpts() #可以保存選擇 ) .render("翻轉(zhuǎn)XY軸.html") ) print("圖表已生成!請(qǐng)查收!")
可以移動(dòng)的X軸柱狀圖(適合數(shù)據(jù)類別過(guò)多)
可以移動(dòng)的X軸,我們可以通過(guò)鼠標(biāo)的控制展示我們想要展示的X軸的維度,這個(gè)用于數(shù)據(jù)類別過(guò)多,一般的可視化無(wú)法展示的情況,比如展示一個(gè)的銷(xiāo)售額,我們可以用這個(gè),顯示30個(gè)數(shù)據(jù)類別。
# -*- coding : utf-8 -*- # @Software : PyCharm # @File : 可以變動(dòng)的X軸.py from pyecharts import options as opts from pyecharts.charts import Bar from pyecharts.globals import ThemeType data_x=['0天', '1天', '2天', '3天', '4天', '5天', '6天', '7天', '8天', '9天', '10天', '11天', '12天', '13天', '14天', '15天', '16天', '17天', '18天', '19天', '20天', '21天', '22天', '23天', '24天', '25天', '26天', '27天', '28天', '29天'] data_y=[5, 27, 27, 7, 13, 5, 1, 2, 29, 20, 21, 28, 5, 22, 23, 4, 20, 26, 25, 1, 3, 14, 23, 11, 4, 8, 2, 22, 13, 22] c = ( Bar({"theme": ThemeType.MACARONS}) .add_xaxis(data_x) .add_yaxis("商家A", data_y) .set_global_opts( title_opts=opts.TitleOpts(title="標(biāo)題"), datazoom_opts=opts.DataZoomOpts(), # 需要的時(shí)候可以加入,添加列表形式即可 # datazoom_opts=opts.DataZoomOpts(type_="inside") ) .render("變動(dòng)X軸柱狀圖.html") ) print("圖表已生成!請(qǐng)查收!")
可以移動(dòng)的Y軸柱狀圖(適合數(shù)據(jù)類別過(guò)多)
既然X軸可以,那么Y軸必然也可以,下面來(lái)看看這個(gè)效果如何。
# -*- coding : utf-8 -*- # @Software : PyCharm # @File : 可以變動(dòng)的Y軸.py from pyecharts import options as opts from pyecharts.charts import Bar from pyecharts.globals import ThemeType data_x = ['0天', '1天', '2天', '3天', '4天', '5天', '6天', '7天', '8天', '9天', '10天', '11天', '12天', '13天', '14天', '15天', '16天', '17天', '18天', '19天', '20天', '21天', '22天', '23天', '24天', '25天', '26天', '27天', '28天', '29天'] data_y = [5, 27, 27, 7, 13, 5, 1, 2, 29, 20, 21, 28, 5, 22, 23, 4, 20, 26, 25, 1, 3, 14, 23, 11, 4, 8, 2, 22, 13, 22] c = ( Bar({"theme": ThemeType.MACARONS}) .add_xaxis(data_x) .add_yaxis("商家A", data_y) .set_global_opts( title_opts=opts.TitleOpts(title="標(biāo)題"), datazoom_opts=opts.DataZoomOpts(orient="vertical"), ) .render("變動(dòng)Y軸柱狀圖.html") ) print("圖表已生成!請(qǐng)查收!")
二維簡(jiǎn)單柱狀圖(主題可選擇)
一個(gè)柱狀圖里面可以展示多種類別的數(shù)據(jù),主題可以選擇,便于我們對(duì)數(shù)據(jù)進(jìn)行直觀的對(duì)比和理解。
# -*- coding : utf-8 -*- # @Software : PyCharm # @File : 柱狀圖-主題可選擇.py # from pyecharts.charts import Bar # from pyecharts.faker import Faker from pyecharts.globals import ThemeType from pyecharts import options as opts from pyecharts.charts import Bar data_0=['周一', '周二', '周三', '周四', '周五', '周六', '周日'] data1=[23, 52, 108, 93, 110, 108, 48] data2=[97, 81, 118, 149, 134, 47, 66] c = ( Bar({"theme": ThemeType.MACARONS}) .add_xaxis(data_0) .add_yaxis("商家A", data1) #gap="0%" 這個(gè)可設(shè)置柱狀圖之間的距離 .add_yaxis("商家B", data2) #gap="0%" 這個(gè)可設(shè)置柱狀圖之間的距離 .set_global_opts(title_opts={"text": "B標(biāo)題1", "subtext": "標(biāo)題2"}, #該標(biāo)題的顏色跟隨主題 # 該標(biāo)題默認(rèn)為黑體顯示,一般作為顯示常態(tài) # title_opts=opts.TitleOpts(title="標(biāo)題") xaxis_opts=opts.AxisOpts( name='星期', name_location='middle', name_gap=30, # 標(biāo)簽與軸線之間的距離,默認(rèn)為20,最好不要設(shè)置20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標(biāo)簽字體大小 )), yaxis_opts=opts.AxisOpts( name='數(shù)量', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), # datazoom_opts=opts.DataZoomOpts(type_="inside"), #鼠標(biāo)可以滑動(dòng)控制 # toolbox_opts=opts.ToolboxOpts() # 工具選項(xiàng) # brush_opts=opts.BrushOpts() #可以保存選擇 ) .render("簡(jiǎn)單柱狀圖.html") ) print("圖表已生成!請(qǐng)查收!")
動(dòng)畫(huà)延遲柱狀圖
個(gè)人感覺(jué)這個(gè)就是設(shè)置的一個(gè)元素,延遲展示了一下,沒(méi)有什么太大的用處,當(dāng)然也可以用到需要的場(chǎng)景。
# -*- coding : utf-8 -*- # @Software : PyCharm # @File : 動(dòng)畫(huà)延遲.py from pyecharts import options as opts from pyecharts.charts import Bar from pyecharts.faker import Faker c = ( Bar( init_opts=opts.InitOpts( animation_opts=opts.AnimationOpts( animation_delay=1000, animation_easing="elasticOut" ) ) ) .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values()) .add_yaxis("商家B", Faker.values()) .set_global_opts(title_opts=opts.TitleOpts(title="標(biāo)題"), xaxis_opts=opts.AxisOpts( name='類別', name_location='middle', name_gap=30, # 標(biāo)簽與軸線之間的距離,默認(rèn)為20,最好不要設(shè)置20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標(biāo)簽字體大小 )), yaxis_opts=opts.AxisOpts( name='數(shù)量', name_location='middle', name_gap=40, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), # datazoom_opts=opts.DataZoomOpts(type_="inside"), #鼠標(biāo)可以滑動(dòng)控制 # toolbox_opts=opts.ToolboxOpts() # 工具選項(xiàng) # brush_opts=opts.BrushOpts() #可以保存選擇 ) .render("動(dòng)畫(huà)延遲.html") ) print('圖表已生成!請(qǐng)查收!')
直方圖按照顏色區(qū)分
直方圖更加的直觀的展示,這個(gè)案例更是可以運(yùn)用到統(tǒng)計(jì)里面,顏色的區(qū)分讓我們更加的快速理解。
# -*- coding : utf-8 -*- # @Software : PyCharm # @File : 直方圖.py from pyecharts import options as opts from pyecharts.charts import Bar from pyecharts.faker import Faker x = Faker.dogs + Faker.animal xlen = len(x) y = [] for idx, item in enumerate(x): if idx <= xlen / 2: y.append( opts.BarItem( name=item, value=(idx + 1) * 10, itemstyle_opts=opts.ItemStyleOpts(color="#749f83"), ) ) else: y.append( opts.BarItem( name=item, value=(xlen + 1 - idx) * 10, itemstyle_opts=opts.ItemStyleOpts(color="#d48265"), ) ) c = ( Bar() .add_xaxis(x) .add_yaxis("series0", y, category_gap=0, color=Faker.rand_color()) .set_global_opts(title_opts=opts.TitleOpts(title="Bar-直方圖(顏色區(qū)分)"), xaxis_opts=opts.AxisOpts( name='類別', name_location='middle', name_gap=30, # 標(biāo)簽與軸線之間的距離,默認(rèn)為20,最好不要設(shè)置20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標(biāo)簽字體大小 )), yaxis_opts=opts.AxisOpts( name='數(shù)量', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), # toolbox_opts=opts.ToolboxOpts() # 工具選項(xiàng) # brush_opts=opts.BrushOpts() #可以保存選擇 ) .render("直方圖.html") ) print("圖表已生成!請(qǐng)查收!")
到此這篇關(guān)于Python繪制柱狀圖可視化神器pyecharts的文章就介紹到這了,更多相關(guān)Python pyecharts內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python seek()和tell()函數(shù)的具體使用
本文主要介紹了Python seek()和tell()函數(shù)的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python使用Selenium模擬瀏覽器自動(dòng)操作功能
這篇文章主要介紹了Python使用Selenium模擬瀏覽器自動(dòng)操作功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09python 用pandas實(shí)現(xiàn)數(shù)據(jù)透視表功能
這篇文章主要介紹了python 用pandas實(shí)現(xiàn)數(shù)據(jù)透視表功能的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12python opencv檢測(cè)直線 cv2.HoughLinesP的實(shí)現(xiàn)
cv2.HoughLines()函數(shù)是在二值圖像中查找直線,本文結(jié)合示例詳細(xì)的介紹了cv2.HoughLinesP的用法,感興趣的可以了解一下2021-06-06