欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python繪制柱狀圖可視化神器pyecharts

 更新時間:2022年09月29日 16:56:43   作者:王小王_123???????  
這篇文章主要介紹了Python繪制柱狀圖可視化神器pyecharts,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下

pyecharts介紹

pyecharts是python與echarts鏈接,一個用于生成Echarts圖標的第三方庫,pyecharts分為v0.5.X和v1兩個大版本,兩者互不兼容,v1是一個全新的版本,經(jīng)研發(fā)團隊決定,前者將不再進行更新維護。下面是我個人整理的關于pyecharts繪制柱狀圖的案例大全,收集整理不易,多多支持!

特性:

  • 簡潔的 API 設計,使用如絲滑般流暢,支持鏈式調用
  • 囊括了 30+ 種常見圖表,應有盡有
  • 支持主流 Notebook 環(huán)境,Jupyter Notebook 和 JupyterLab
  • 可輕松集成至 Flask,Django 等主流 Web 框架
  • 高度靈活的配置項,可輕松搭配出精美的圖表
  • 詳細的文檔和示例,幫助開發(fā)者更快的上手項目
  • 多達 400+ 地圖文件以及原生的百度地圖,為地理數(shù)據(jù)可視化提供強有力的支持

優(yōu)勢

pyecharts可以輸出網(wǎng)頁版的鏈接,直接調用資源渲染圖表,方便快捷,輸出不是圖片,而是一個可以調節(jié)的頁面,動態(tài),炫酷,都是它的天地!它可以支持在手機端瀏覽界面,也可以修改相關參數(shù),總的來說方便至極,而且主題都可以隨意搭配,顏色自己調。適用于公司可視化報表,企業(yè)展示,日常辦公,由于圖表過于炫酷,不大適合做科研論文展示,后期會介紹另外一個庫,可以作為科研黨的首選——matplotlib

展示

總之pyecharts科研繪制很多一般繪制不了的圖形,作為一個可視化神器它的便利之處,只有用過的小伙伴才知道,什么叫 “工欲善其事必先利其器”的道理。

柱狀圖模板系列

水晶柱狀圖

水晶柱狀圖適用于幾個數(shù)據(jù)的對比,生成的柱狀圖具有一種玲瓏剔透,清水出芙蓉的美感和清新,最適合做可視化展示。

# -*- 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 = ['可樂', '雪碧', '橙汁', '綠茶', '奶茶', '百威', '青島']
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="標題"),
xaxis_opts=opts.AxisOpts(
name='類別',
name_location='middle',
name_gap=30, # 標簽與軸線之間的距離,默認為20,最好不要設置20
name_textstyle_opts=opts.TextStyleOpts(
font_family='Times New Roman',
font_size=16 # 標簽字體大小
)),
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() # 工具選項
)
.render("水晶柱狀圖.html")
)

解決X軸標簽過長的柱狀圖

有時候我們在繪制柱狀圖的時候,X軸標簽過長導致圖形顯示的不正常,修改字數(shù)之后又怕展示效果不夠明顯。此圖例解決了這個難題,適合展示X軸標簽過長的問題。

# -*- coding : utf-8 -*-

# @Software : PyCharm
# @File : 解決X軸標簽過長的問題.py
from pyecharts import options as opts
from pyecharts.charts import Bar
c = (
Bar()
.add_xaxis(
[
"名字很長的X軸標簽1",
"名字很長的X軸標簽2",
"名字很長的X軸標簽3",
"名字很長的X軸標簽4",
"名字很長的X軸標簽5",
"名字很長的X軸標簽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, # 標簽與軸線之間的距離,默認為20,最好不要設置20
# name_textstyle_opts=opts.TextStyleOpts(
# font_family='Times New Roman',
# font_size=16 # 標簽字體大小
# )
),
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="標題1", subtitle="標題2"),
# toolbox_opts=opts.ToolboxOpts() # 工具選項
)
.render("解決X軸標簽過長問題.html")
)
print("圖表已生成!請查收!")

自定義平均刻度標簽(方便查看超出范圍)

有時候數(shù)據(jù)過于多,柱狀圖過于密集,我們知道一個平均數(shù),需要快速的查看那些數(shù)據(jù)超過了這個閾值,那些數(shù)據(jù)低于平均值,這時候我們可以自己定義一個刻度標簽,方便我們理解。

# -*- 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="標題"),
xaxis_opts=opts.AxisOpts(
name='類別',
name_location='middle',
name_gap=30, # 標簽與軸線之間的距離,默認為20,最好不要設置20
name_textstyle_opts=opts.TextStyleOpts(
font_family='Times New Roman',
font_size=16 # 標簽字體大小
)),
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"), #鼠標可以滑動控制
# toolbox_opts=opts.ToolboxOpts() # 工具選項
# brush_opts=opts.BrushOpts() #可以保存選擇
)
.render("顯示平均刻度.html")
)
print("圖表已生成!請查收!")

翻轉X Y軸柱狀圖

直觀的展示柱狀圖過于單調,有時候我們需要適當?shù)恼{整一下這個主題,把xy軸翻轉一下,這樣更能直觀的對比顯示,適用多個數(shù)據(jù)類別進行比較。

# -*- coding : utf-8 -*-
# @Software : PyCharm
# @File : 翻轉XY軸.p
from pyecharts import options as opts
from pyecharts.charts import Bar
data_x = ['可樂', '雪碧', '橙汁', '綠茶', '奶茶', '百威', '青島']
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="標題"),
xaxis_opts=opts.AxisOpts(
name='數(shù)量',
name_location='middle',
name_gap=30, # 標簽與軸線之間的距離,默認為20,最好不要設置20
name_textstyle_opts=opts.TextStyleOpts(
font_family='Times New Roman',
font_size=16 # 標簽字體大小
)),
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"), #鼠標可以滑動控制
# toolbox_opts=opts.ToolboxOpts() # 工具選項
# brush_opts=opts.BrushOpts() #可以保存選擇
)
.render("翻轉XY軸.html")
)
print("圖表已生成!請查收!")

可以移動的X軸柱狀圖(適合數(shù)據(jù)類別過多)

可以移動的X軸,我們可以通過鼠標的控制展示我們想要展示的X軸的維度,這個用于數(shù)據(jù)類別過多,一般的可視化無法展示的情況,比如展示一個的銷售額,我們可以用這個,顯示30個數(shù)據(jù)類別。

# -*- coding : utf-8 -*-
# @Software : PyCharm
# @File : 可以變動的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="標題"),
datazoom_opts=opts.DataZoomOpts(),
# 需要的時候可以加入,添加列表形式即可
# datazoom_opts=opts.DataZoomOpts(type_="inside")

)
.render("變動X軸柱狀圖.html")
)
print("圖表已生成!請查收!")

可以移動的Y軸柱狀圖(適合數(shù)據(jù)類別過多)

既然X軸可以,那么Y軸必然也可以,下面來看看這個效果如何。

# -*- coding : utf-8 -*-
# @Software : PyCharm
# @File : 可以變動的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="標題"),
datazoom_opts=opts.DataZoomOpts(orient="vertical"),

)

.render("變動Y軸柱狀圖.html")
)
print("圖表已生成!請查收!")

二維簡單柱狀圖(主題可選擇)

一個柱狀圖里面可以展示多種類別的數(shù)據(jù),主題可以選擇,便于我們對數(shù)據(jù)進行直觀的對比和理解。

# -*- 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%" 這個可設置柱狀圖之間的距離
.add_yaxis("商家B", data2) #gap="0%" 這個可設置柱狀圖之間的距離
.set_global_opts(title_opts={"text": "B標題1", "subtext": "標題2"}, #該標題的顏色跟隨主題
# 該標題默認為黑體顯示,一般作為顯示常態(tài)
# title_opts=opts.TitleOpts(title="標題")
xaxis_opts=opts.AxisOpts(
name='星期',
name_location='middle',
name_gap=30, # 標簽與軸線之間的距離,默認為20,最好不要設置20
name_textstyle_opts=opts.TextStyleOpts(
font_family='Times New Roman',
font_size=16 # 標簽字體大小
)),
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"), #鼠標可以滑動控制
# toolbox_opts=opts.ToolboxOpts() # 工具選項
# brush_opts=opts.BrushOpts() #可以保存選擇
)

.render("簡單柱狀圖.html")
)
print("圖表已生成!請查收!")

動畫延遲柱狀圖

個人感覺這個就是設置的一個元素,延遲展示了一下,沒有什么太大的用處,當然也可以用到需要的場景。

# -*- coding : utf-8 -*-
# @Software : PyCharm
# @File : 動畫延遲.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="標題"),
xaxis_opts=opts.AxisOpts(
name='類別',
name_location='middle',
name_gap=30, # 標簽與軸線之間的距離,默認為20,最好不要設置20
name_textstyle_opts=opts.TextStyleOpts(
font_family='Times New Roman',
font_size=16 # 標簽字體大小
)),
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"), #鼠標可以滑動控制
# toolbox_opts=opts.ToolboxOpts() # 工具選項
# brush_opts=opts.BrushOpts() #可以保存選擇
)
.render("動畫延遲.html")
)
print('圖表已生成!請查收!')

直方圖按照顏色區(qū)分

直方圖更加的直觀的展示,這個案例更是可以運用到統(tǒng)計里面,顏色的區(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, # 標簽與軸線之間的距離,默認為20,最好不要設置20
name_textstyle_opts=opts.TextStyleOpts(
font_family='Times New Roman',
font_size=16 # 標簽字體大小
)),
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() # 工具選項
# brush_opts=opts.BrushOpts() #可以保存選擇
)

.render("直方圖.html")
)
print("圖表已生成!請查收!")

到此這篇關于Python繪制柱狀圖可視化神器pyecharts的文章就介紹到這了,更多相關Python pyecharts內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python seek()和tell()函數(shù)的具體使用

    Python seek()和tell()函數(shù)的具體使用

    本文主要介紹了Python seek()和tell()函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • Python使用Selenium模擬瀏覽器自動操作功能

    Python使用Selenium模擬瀏覽器自動操作功能

    這篇文章主要介紹了Python使用Selenium模擬瀏覽器自動操作功能,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Python簡明入門教程

    Python簡明入門教程

    這篇文章主要介紹了Python簡明入門教程,較為詳細的分析了Python的基本概念及語法基礎,有助于Python初學者更好的掌握Python的基本語法與使用技巧,需要的朋友可以參考下
    2015-08-08
  • web.py在模板中輸出美元符號的方法

    web.py在模板中輸出美元符號的方法

    這篇文章主要介紹了web.py在模板中輸出美元符號的方法,即在web.py的模板中輸出$符號的方法,需要的朋友可以參考下
    2014-08-08
  • python 用pandas實現(xiàn)數(shù)據(jù)透視表功能

    python 用pandas實現(xiàn)數(shù)據(jù)透視表功能

    這篇文章主要介紹了python 用pandas實現(xiàn)數(shù)據(jù)透視表功能的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • 詳解pygame中Rect對象

    詳解pygame中Rect對象

    Rect是pygame中的一個創(chuàng)建矩形的對象,它包含一些屬性主要是兩塊:坐標和長寬,Pygame?通過?Rect?對象存儲和操作矩形區(qū)域,這篇文章主要介紹了pygame中Rect對象,需要的朋友可以參考下
    2022-07-07
  • 寫好Python代碼的幾條重要技巧

    寫好Python代碼的幾條重要技巧

    好的代碼具有易理解、可擴展、易維護的特點,簡直是人見人愛。本文就將介紹寫好python代碼的多個技巧
    2021-05-05
  • python opencv檢測直線 cv2.HoughLinesP的實現(xiàn)

    python opencv檢測直線 cv2.HoughLinesP的實現(xiàn)

    cv2.HoughLines()函數(shù)是在二值圖像中查找直線,本文結合示例詳細的介紹了cv2.HoughLinesP的用法,感興趣的可以了解一下
    2021-06-06
  • python使用正則來處理各種匹配問題

    python使用正則來處理各種匹配問題

    這篇文章主要介紹了python使用正則來處理各種匹配問題,本文通過實例代碼給大家講解的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • 提升Python編程水平必不可少的重構技巧

    提升Python編程水平必不可少的重構技巧

    在Python中,編寫可讀性強且Pythonic的代碼是至關重要的,重構技巧是指通過調整代碼結構和風格,使其更符合Python的慣例和標準,從而提高代碼的可讀性、簡潔性和可維護性,本文將深入探討八項重構技巧,幫助您編寫更Pythonic的代碼
    2024-01-01

最新評論