Python利用Bokeh進(jìn)行數(shù)據(jù)可視化的教程分享
介紹
Bokeh是 Python 中的數(shù)據(jù)可視化庫,提供高性能的交互式圖表和繪圖。Bokeh 輸出可以在筆記本、html 和服務(wù)器等各種媒體中獲得??梢栽?Django 和燒瓶應(yīng)用程序中嵌入散景圖。
Bokeh 為用戶提供了兩個可視化界面:
bokeh.models:為應(yīng)用程序開發(fā)人員提供高度靈活性的低級接口。
bokeh.plotting:用于創(chuàng)建視覺字形的高級界面。
要安裝 bokeh 包,請?jiān)诮K端中運(yùn)行以下命令:
pip install bokeh
用于生成散景圖的數(shù)據(jù)集是從Kaggle收集的。
代碼1.散點(diǎn)標(biāo)記
要創(chuàng)建散點(diǎn)圓標(biāo)記,使用 circle() 方法。
# 導(dǎo)入模塊 from bokeh.plotting import figure, output_notebook, show # 輸出到 notebook output_notebook() # 創(chuàng)建圖 p = figure(plot_width = 400, plot_height = 400) # 添加具有大小、顏色和 alpha 的圓形渲染器 p.circle([1, 2, 3, 4, 5], [4, 7, 1, 6, 3], size = 10, color = "navy", alpha = 0.5) # 顯示結(jié)果 show(p)
輸出 :
代碼2.單行
要創(chuàng)建單行,使用 line() 方法。
# 導(dǎo)入模塊 from bokeh.plotting import figure, output_notebook, show # 輸出到 notebook output_notebook() # 創(chuàng)建圖 p = figure(plot_width = 400, plot_height = 400) # 添加線渲染器 p.line([1, 2, 3, 4, 5], [3, 1, 2, 6, 5], line_width = 2, color = "green") # 顯示結(jié)果 show(p)
輸出 :
代碼3.條形圖
條形圖用矩形條顯示分類數(shù)據(jù)。條的長度與表示的值成比例。
# 導(dǎo)入必要的模塊 import pandas as pd from bokeh.charts import Bar, output_notebook, show # 輸出到 notebook output_notebook() # 讀取數(shù)據(jù)框中的數(shù)據(jù) df = pd.read_csv(r"D:/kaggle/mcdonald/menu.csv") # 創(chuàng)建欄 p = Bar(df, "Category", values = "Calories", title = "Total Calories by Category", legend = "top_right") # 顯示結(jié)果 show(p)
輸出 :
代碼4.箱線圖
箱線圖用于表示圖表上的統(tǒng)計(jì)數(shù)據(jù)。它有助于總結(jié)數(shù)據(jù)中存在的各種數(shù)據(jù)組的統(tǒng)計(jì)屬性。
# 導(dǎo)入必要的模塊 from bokeh.charts import BoxPlot, output_notebook, show import pandas as pd # 輸出到 notebook output_notebook() # 讀取數(shù)據(jù)框中的數(shù)據(jù) df = pd.read_csv(r"D:/kaggle / mcdonald / menu.csv") # 創(chuàng)建欄 p = BoxPlot(df, values = "Protein", label = "Category", color = "yellow", title = "Protein Summary (grouped by category)", legend = "top_right") # 顯示結(jié)果 show(p)
輸出 :
代碼5.直方圖
直方圖用于表示數(shù)值數(shù)據(jù)的分布。直方圖中矩形的高度與類間隔中值的頻率成正比。
# 導(dǎo)入必要的模塊 from bokeh.charts import Histogram, output_notebook, show import pandas as pd # 輸出到 notebook output_notebook() # 讀取數(shù)據(jù)框中的數(shù)據(jù) df = pd.read_csv(r"D:/kaggle / mcdonald / menu.csv") # 創(chuàng)建直方圖 p = Histogram(df, values = "Total Fat", title = "Total Fat Distribution", color = "navy") # 顯示結(jié)果 show(p)
輸出 :
代碼6.散點(diǎn)圖
散點(diǎn)圖用于繪制數(shù)據(jù)集中兩個變量的值。它有助于找到所選的兩個變量之間的相關(guān)性。
# 導(dǎo)入必要的模塊 from bokeh.charts import Scatter, output_notebook, show import pandas as pd # 輸出到 notebook output_notebook() # 讀取數(shù)據(jù)框中的數(shù)據(jù) df = pd.read_csv(r"D:/kaggle / mcdonald / menu.csv") # 創(chuàng)建散點(diǎn)圖 p = Scatter(df, x = "Carbohydrates", y = "Saturated Fat", title = "Saturated Fat vs Carbohydrates", xlabel = "Carbohydrates", ylabel = "Saturated Fat", color = "orange") # 顯示結(jié)果 show(p)
輸出:
以上就是Python利用Bokeh進(jìn)行數(shù)據(jù)可視化的教程分享的詳細(xì)內(nèi)容,更多關(guān)于Python Bokeh數(shù)據(jù)可視化的資料請關(guān)注腳本之家其它相關(guān)文章!
- Bokeh:Python交互式可視化的利器詳解
- Python?Bokeh實(shí)現(xiàn)實(shí)時數(shù)據(jù)可視化
- python使用Bokeh庫實(shí)現(xiàn)實(shí)時數(shù)據(jù)的可視化
- Python使用Bokeh庫實(shí)現(xiàn)炫目的交互可視化
- Python使用Bokeh進(jìn)行交互式數(shù)據(jù)可視化
- Python使用Bokeh實(shí)現(xiàn)交互式圖表的創(chuàng)建
- Python庫?Bokeh?數(shù)據(jù)可視化實(shí)用指南
- python基于Bokeh庫制作子彈圖及瀑布圖示例教程
- Python 交互式可視化的利器Bokeh的使用
相關(guān)文章
Pandas數(shù)據(jù)查詢的集中實(shí)現(xiàn)方法
本文主要介紹了Pandas數(shù)據(jù)查詢的集中實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python面向?qū)ο缶幊剃P(guān)鍵深度探索類與對象
這篇文章主要為大家介紹了Python面向?qū)ο缶幊剃P(guān)鍵深度探索類與對象示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Python生成隨機(jī)驗(yàn)證碼代碼實(shí)例解析
這篇文章主要介紹了Python生成隨機(jī)驗(yàn)證碼代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06