Python制作可視化報(bào)表的示例詳解
大家好,我是小F~
在數(shù)據(jù)展示中使用圖表來(lái)分享自己的見(jiàn)解,是個(gè)非常常見(jiàn)的方法。
這也是Tableau、Power BI這類(lèi)商業(yè)智能儀表盤(pán)持續(xù)流行的原因之一,這些工具為數(shù)據(jù)提供了精美的圖形解釋。
當(dāng)然了,這些工具也有著不少缺點(diǎn),比如不夠靈活,無(wú)法讓你自己創(chuàng)建設(shè)計(jì)。
當(dāng)你對(duì)圖表展示要求定制化時(shí),編程也許就比較適合你,比如Echarts、D3.js。
今天小F給大家介紹一個(gè)用Python制作可視化報(bào)表的案例,主要是使用到Dash+Tailwindcss。
可視化報(bào)表效果如下,水果銷(xiāo)售情況一覽~
Dash是基于Plotly搭建的Dashbord框架,支持Python、R和Julia。使用Dash,你可以創(chuàng)建自定義響應(yīng)式儀表板。
相關(guān)文檔
說(shuō)明:https://dash.plotly.com/introduction
案例:https://dash.gallery/Portal/
Tailwindcss則是一個(gè)實(shí)用程序優(yōu)先的CSS框架,用于快速構(gòu)建自定義界面。
“這種框架只適用于那種只會(huì)實(shí)現(xiàn)頁(yè)面布局美化元素而不關(guān)心實(shí)現(xiàn)業(yè)務(wù)邏輯的前端”。
看看別人對(duì)它的評(píng)價(jià),對(duì)于無(wú)交互的圖表,完全足夠了。
相關(guān)文檔
說(shuō)明:https://www.tailwindcss.cn/docs
下面就給大家講解下如何通過(guò)Dash+Tailwindcss搭建可視化報(bào)表~
首先安裝相關(guān)的Python庫(kù),然后導(dǎo)入。
import dash import pandas as pd import plotly.express as px from dash import dcc, html
使用到了Pandas、Plotly、dash這三個(gè)Python庫(kù)。
我們需要把Tailwindcss的CDN作為external_script,并將其傳遞給我們的應(yīng)用程序?qū)嵗@樣我們才可以成功使用Tailwindcss。
# 導(dǎo)入tailwindcss的CDN external_script = ["https://tailwindcss.com/", {"src": "https://cdn.tailwindcss.com"}] # 創(chuàng)建Dash實(shí)例 app = dash.Dash( __name__, external_scripts=external_script, ) app.scripts.config.serve_locally = True
使用Pandas創(chuàng)建水果銷(xiāo)售數(shù)據(jù),隨便虛構(gòu)了一個(gè)。
# 創(chuàng)建數(shù)據(jù) df = pd.DataFrame( { "Fruit": ["蘋(píng)果", "橙子", "香蕉", "蘋(píng)果", "橙子", "香蕉"], "Amount": [4.2, 1.0, 2.1, 2.32, 4.20, 5.0], "City": ["北京", "北京", "北京", "上海", "上海", "上海"], } ) print(df)
結(jié)果如下,3列6行,包含水果、銷(xiāo)售額、城市列。
處理一下相關(guān)的數(shù)據(jù),水果單數(shù)、銷(xiāo)售總額、城市單數(shù)、變量數(shù)。
# 水果單數(shù) fruit_count = df.Fruit.count() # 銷(xiāo)售總額 total_amt = df.Amount.sum() # 城市單數(shù) city_count = df.City.count() # 變量數(shù) variables = df.shape[1]
創(chuàng)建圖表實(shí)例,一個(gè)柱狀圖、一個(gè)箱型圖。
# 柱狀圖1, 不同水果不同城市的銷(xiāo)售額 fig = px.bar(df, x="Fruit", y="Amount", color="City", barmode="group") # 箱型圖1, 不同城市的銷(xiāo)售額分布情況 fig1 = px.box(df, x="City", y="Amount", color="City")
效果如下。
剩下就是文字模塊啦,文字+CSS樣式。
其中排版布局美化,通過(guò)Tailwindcss來(lái)實(shí)現(xiàn)。
app.layout = html.Div( html.Div( children=[ html.Div( children=[ html.H1(children="水果銷(xiāo)售--可視化報(bào)表", className=" py-3 text-5xl font-bold text-gray-800"), html.Div( children="""Python with Dash = ?? .""", className="text-left prose prose-lg text-2xl py-3 text-gray-600", ), ], className="w-full mx-14 px-16 shadow-lg bg-white -mt-14 px-6 container my-3 ", ), html.Div( html.Div( children=[ html.Div( children=[ f"¥{total_amt}", html.Br(), html.Span("總銷(xiāo)售額", className="text-lg font-bold ml-4"), ], className=" shadow-xl py-4 px-14 text-5xl bg-[#76c893] text-white font-bold text-gray-800", ), html.Div( children=[ fruit_count, html.Br(), html.Span("水果數(shù)量", className="text-lg font-bold ml-4"), ], className=" shadow-xl py-4 px-24 text-5xl bg-[#1d3557] text-white font-bold text-gray-800", ), html.Div( children=[ variables, html.Br(), html.Span("變量", className="inline-flex items-center text-lg font-bold ml-4"), ], className=" shadow-xl py-4 px-24 text-5xl bg-[#646ffa] text-white font-bold text-gray-800", ), html.Div( children=[ city_count, html.Br(), html.Span("城市數(shù)量", className="text-lg font-bold ml-4"), ], className="w-full shadow-xl py-4 px-24 text-5xl bg-[#ef553b] text-white font-bold text-gray-800", ), ], className="my-4 w-full grid grid-flow-rows grid-cols-1 lg:grid-cols-4 gap-y-4 lg:gap-[60px]", ), className="flex max-w-full justify-between items-center ", ), html.Div( children=[ html.Div( children=[ dcc.Graph(id="example-graph", figure=fig), ], className="shadow-xl w-full border-3 rounded-sm", ), html.Div( children=[ dcc.Graph(id="example-graph1", figure=fig1), ], className="w-full shadow-2xl rounded-sm", ), ], className="grid grid-cols-1 lg:grid-cols-2 gap-4", ), ], className="bg-[#ebeaee] flex py-14 flex-col items-center justify-center ", ), className="bg-[#ebeaee] container mx-auto px-14 py-4", )
效果如下。
最后啟動(dòng)程序代碼。
if __name__ == '__main__': # debug模式, 端口7777 app.run_server(debug=True, threaded=True, port=7777) # 正常模式, 網(wǎng)頁(yè)右下角的調(diào)試按鈕將不會(huì)出現(xiàn) # app.run_server(port=7777)
這樣就能在本地看到可視化大屏頁(yè)面,瀏覽器打開(kāi)如下地址。
http://127.0.0.1:7777
以后制作的圖表不僅能在線展示,還能實(shí)時(shí)更新,屬實(shí)不錯(cuò)~
到此這篇關(guān)于Python制作可視化報(bào)表的示例詳解的文章就介紹到這了,更多相關(guān)Python可視化報(bào)表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python使用tqdm庫(kù)實(shí)現(xiàn)循環(huán)打印進(jìn)度條
tqdm是一個(gè)用于在Python中添加進(jìn)度條的庫(kù),它可以很容易地集成到while循環(huán)中,這篇文章主要介紹了python循環(huán)打印進(jìn)度條,需要的朋友可以參考下2023-05-05python人工智能tensorflow函數(shù)tf.nn.dropout使用方法
這篇文章主要為大家介紹了python人工智能tensorflow函數(shù)tf.nn.dropout使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python調(diào)用Pandas實(shí)現(xiàn)Excel讀取
這篇文章主要為大家介紹了在Python中如何調(diào)用Pandas實(shí)現(xiàn)Excel文件的讀取,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-04-04django中間件及自定義中間件的實(shí)現(xiàn)方法
中間件就是在目標(biāo)和結(jié)果之間進(jìn)行的額外處理過(guò)程,在Django中就是request和response之間進(jìn)行的處理,相對(duì)來(lái)說(shuō)實(shí)現(xiàn)起來(lái)比較簡(jiǎn)單,這篇文章主要介紹了django中間件以及自定義中間件?,需要的朋友可以參考下2023-06-06詳解Python中使用base64模塊來(lái)處理base64編碼的方法
8bit的bytecode經(jīng)常會(huì)被用base64編碼格式保存,Python中自帶base64模塊對(duì)base64提供支持,這里我們就來(lái)詳解Python中使用base64模塊來(lái)處理base64編碼的方法,需要的朋友可以參考下2016-07-07Python腳本按照當(dāng)前日期創(chuàng)建多級(jí)目錄
今天小編就為大家分享一篇關(guān)于Python腳本按照當(dāng)前日期創(chuàng)建多級(jí)目錄,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03