
今天CDA為大家分享:Pyecharts繪制22種超實(shí)用精美圖表
作者:俊欣
來源:關(guān)于數(shù)據(jù)分析與可視化
今天來給大家分享一下Pyecharts模塊,說到它我們就不得不提Echarts,它是由百度開源的一款使用JavaScript實(shí)現(xiàn)的開源可視化庫,涵蓋了各種圖表、滿足各類業(yè)務(wù)需求,而pyecharts也就是Python與Echarts結(jié)合之后的產(chǎn)物,封裝了Echarts各類圖表的基本操作,然后通過渲染機(jī)制,輸出一個(gè)包含JS代碼的HTML文件。
說到安裝模塊,我們可以這樣來進(jìn)行,
pip install pyecharts
使用Pyecharts創(chuàng)建圖形的基本步驟是
1. 準(zhǔn)備數(shù)據(jù)
2. 設(shè)計(jì)圖形的樣式、背景顏色
3. Pyecharts繪圖
4. 設(shè)計(jì)圖表的標(biāo)題或者圖例等屬性
5. 導(dǎo)出至html
from pyecharts import options as opts from pyecharts.charts import Bar from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家1", Faker.values())
.add_yaxis("商家2", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="這是主標(biāo)題", subtitle="這是副標(biāo)題"))
.render("bar_base.html")
)
出來的結(jié)果是
import pandas as pd import numpy as np data = pd.DataFrame({'x':np.arange(1,101), 'y':["隨機(jī)生成的數(shù)字"]})
df = pd.read_excel("你的文件的路徑")
Pyecharts內(nèi)部還提供了一些數(shù)據(jù)集,主要包含類別數(shù)據(jù)、時(shí)間數(shù)據(jù)、顏色數(shù)據(jù)、地理數(shù)據(jù)、世界人口數(shù)據(jù)等等,通過choose()方法來隨機(jī)選擇使用哪個(gè)
def choose(self) -> list: return random.choice(
[ self.clothes, self.drinks, self.phones, self.fruits, self.animal, self.dogs, self.week,
]
)
說到圖形的樣式,大概都這么幾種
class _ThemeType:
BUILTIN_THEMES = ["light", "dark", "white"]
LIGHT = "light" DARK = "dark" WHITE = "white" CHALK: str = "chalk" ESSOS: str = "essos" INFOGRAPHIC: str = "infographic" MACARONS: str = "macarons" PURPLE_PASSION: str = "purple-passion" ROMA: str = "roma" ROMANTIC: str = "romantic" SHINE: str = "shine" VINTAGE: str = "vintage" WALDEN: str = "walden" WESTEROS: str = "westeros" WONDERLAND: str = "wonderland" HALLOWEEN: str = "halloween"
設(shè)置標(biāo)題以及副標(biāo)題的代碼如下
set_global_opts(title_opts=opts.TitleOpts(title="這是主標(biāo)題",
subtitle="這是副標(biāo)題"))
legend_opts=opts.LegendOpts(type_="scroll", orient="vertical",
pos_top="15%",pos_left="7%")) # 圖裂的位置 label_opts=opts.LabelOpts(formatter=": {c}") # 結(jié)果的展現(xiàn)形式
render("test.html")
# 如果是在jupyter notebook當(dāng)中 render_notebook()
堆疊柱狀圖
同個(gè)品類不同類目的柱子可以堆疊起來呈現(xiàn),也就是堆疊的柱狀圖
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家1", Faker.values(), stack="stack1")
.add_yaxis("商家2", Faker.values(), stack="stack1")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-堆疊數(shù)據(jù)(全部)"))
.render("bar_stack_1212.html")
)
當(dāng)然我們也可以部分堆疊,而不是上面這種全部的堆疊
c = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(Faker.choose())
.add_yaxis("商家1", Faker.values(), stack="stack0")
.add_yaxis("商家2", Faker.values(), stack="stack0")
.add_yaxis("商家3", Faker.values())
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-堆疊數(shù)據(jù)(部分)"))
.render("bar_stack_part.html")
)
柱狀圖的橫坐標(biāo)傾斜一丟丟
有時(shí)候橫坐標(biāo)的標(biāo)識字?jǐn)?shù)較多,X軸上顯示全,我們可以將標(biāo)識的字體稍微傾斜一些
c = (
Bar()
.add_xaxis(
[ "名字相當(dāng)長的X軸標(biāo)簽1", "名字相當(dāng)長的X軸標(biāo)簽2", "名字相當(dāng)長的X軸標(biāo)簽3", "名字相當(dāng)長的X軸標(biāo)簽4", "名字相當(dāng)長的X軸標(biāo)簽5", "名字相當(dāng)長的X軸標(biāo)簽6", ]
)
.add_yaxis("商家1", Faker.values())
.add_yaxis("商家2", Faker.values())
.set_global_opts(
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=15)),
title_opts=opts.TitleOpts(title="Bar-旋轉(zhuǎn)X軸標(biāo)簽", subtitle="副標(biāo)題"),
)
.render("test.html")
)
柱狀圖可以自動縮放的
通過底下的滑塊來實(shí)現(xiàn)橫坐標(biāo)的縮放、范圍的調(diào)整等等
c = (
Bar()
.add_xaxis(Faker.days_attrs)
.add_yaxis("商家1", Faker.days_values)
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-數(shù)據(jù)縮放(拖快-水平)"),
datazoom_opts=opts.DataZoomOpts(),
)
.render("bar_datazoom_slider.html")
)
當(dāng)然滑塊也可以放在垂直的右側(cè)
c = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(Faker.days_attrs)
.add_yaxis("商家1", Faker.days_values, color=Faker.rand_color())
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-DataZoom(滑塊-垂直)"),
datazoom_opts=opts.DataZoomOpts(orient="vertical"),
)
.render("bar_datazoom_slider_vertical.html")
)
我們也可以通過拖動里面的柱子來實(shí)現(xiàn)數(shù)據(jù)縮放、范圍的改變
c = (
Bar()
.add_xaxis(Faker.days_attrs)
.add_yaxis("商家1", Faker.days_values)
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-DataZoom(內(nèi)置+外置)"),
datazoom_opts=[opts.DataZoomOpts(), opts.DataZoomOpts(type_="inside")],
)
.render("bar_datazoom_both.html")
)
柱狀圖給X軸Y軸命名的
c = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(Faker.choose())
.add_yaxis("商家1", Faker.values())
.add_yaxis("商家2", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-XY 軸名稱"),
yaxis_opts=opts.AxisOpts(name="這個(gè)是 Y 軸"),
xaxis_opts=opts.AxisOpts(name="這個(gè)是 X 軸"),
)
.render("bar_name_xyaxis.html")
)
柱狀圖柱間距離不相同的
在柱狀圖當(dāng)中,不同柱子之間的距離也可以不是相同的
c = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.WHITE))
.add_xaxis(Faker.choose())
.add_yaxis("商家1", Faker.values(), gap="0%")
.add_yaxis("商家2", Faker.values(), gap="0%")
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-柱間距離不同"))
.render("bar_different_series_gap.html")
)
柱狀圖水平狀態(tài)的
還有水平方向的柱狀圖
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家1", Faker.values())
.add_yaxis("商家2", Faker.values())
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-水平方向"))
.render("bar_reversal_axis.html")
)
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家1", Faker.values(), category_gap=0, color=Faker.rand_color())
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-直方圖"))
.render("bar_histogram.html")
)
箱型圖更加有利于我們來觀察數(shù)據(jù)的內(nèi)在分布
from pyecharts.charts import Boxplot v1 = [ [850, 740, 950, 1090, 930, 850, 950, 980, 1000, 880, 1000, 980], [980, 940, 960, 940, 900, 800, 850, 880, 950, 840, 830, 800], ] v2 = [ [890, 820, 820, 820, 800, 770, 760, 760, 750, 760, 950, 920], [900, 840, 800, 810, 760, 810, 790, 850, 820, 850, 870, 880], ] c = Boxplot() c.add_xaxis(["A", "B"]) c.add_yaxis("類目1", c.prepare_data(v1)) c.add_yaxis("類目2", c.prepare_data(v2)) c.set_global_opts(title_opts=opts.TitleOpts(title="箱型圖-基本示例")) c.render("boxplot_test.html")
日歷圖
日歷圖具體指按照日歷的布局,用顏色展現(xiàn)每一天的數(shù)據(jù),從而比較直觀地看到全年的數(shù)據(jù)情況,例如展示超市全年的銷售額,從而看出具體某個(gè)月份或者某個(gè)星期的銷售額比較低
c = ( Calendar(init_opts=opts.InitOpts(theme=ThemeType.INFOGRAPHIC)) .add("", data, calendar_opts=opts.CalendarOpts(range_="2020")) .set_global_opts( title_opts=opts.TitleOpts(title="日歷圖-2020年超市的銷售額"), visualmap_opts=opts.VisualMapOpts( max_=250000, min_=10000, orient="horizontal", is_piecewise=True, pos_top="230px", pos_left="100px", ), ) .render("calendar_test.html") )
K線圖
c = (
Kline(init_opts=opts.InitOpts(theme=ThemeType.ESSOS))
.add_xaxis(["2020/7/{}".format(i + 1) for i in range(31)])
.add_yaxis("kline", data)
.set_global_opts(
yaxis_opts=opts.AxisOpts(is_scale=True),
xaxis_opts=opts.AxisOpts(is_scale=True),
title_opts=opts.TitleOpts(title="K線圖-基本示例"),
)
.render("kline_test.html")
)
from pyecharts.charts import Funnel c = ( Funnel()
.add("類目", [list(z) for z in zip(Faker.choose(), Faker.values())])
.set_global_opts(title_opts=opts.TitleOpts(title="漏斗圖-基本示例"))
.render("funnel_test.html")
)
c = (
Line()
.add_xaxis(Faker.choose())
.add_yaxis("商家1", Faker.values())
.add_yaxis("商家2", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="折線圖-基本示例"))
.render("line_test.html")
)
水球圖
水球圖通常來顯示指標(biāo)的完成程度
from pyecharts.charts import Liquid c = ( Liquid()
.add("lq", [0.55, 0.75])
.set_global_opts(title_opts=opts.TitleOpts(title="Liquid-基本示例"))
.render("liquid_test.html")
)
c = (
WordCloud()
.add(series_name="詞云圖實(shí)例", data_pair=data, word_size_range=[5, 100])
.set_global_opts(
title_opts=opts.TitleOpts(
title="詞云圖實(shí)例", title_textstyle_opts=opts.TextStyleOpts(font_size=23)
),
tooltip_opts=opts.TooltipOpts(is_show=True),
)
.render("basic_wordcloud.html")
)
餅圖
c = ( Pie()
.add("類目", [list(z) for z in zip(Faker.choose(), Faker.values())])
.set_global_opts(title_opts=opts.TitleOpts(title="餅圖-基本示例"))
.set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}"))
.render("pie_test.html")
)
儀表盤圖
儀表盤的繪制也可以用來展示指標(biāo)的完成程度
from pyecharts.charts import Gauge c = ( Gauge()
.add("", [("完成率", 70)])
.set_global_opts(title_opts=opts.TitleOpts(title="儀表盤-基本示例"))
.render("gauge_test.html")
)
c = ( Map()
.add("商家1", [list(z) for z in zip(Faker.provinces, Faker.values())], "china")
.set_global_opts(title_opts=opts.TitleOpts(title="地圖-基本示例"))
.render("map_test.html")
)
c = (
EffectScatter()
.add_xaxis(Faker.choose())
.add_yaxis("商家1", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="漣漪散點(diǎn)圖-基本示例"))
.render("effectscatter_test.html")
)
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
LSTM 模型輸入長度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報(bào)考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動決策的時(shí)代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計(jì)的實(shí)用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強(qiáng)大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實(shí)施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價(jià)值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡稱 BI)深度融合的時(shí)代,BI ...
2025-07-10SQL 在預(yù)測分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢預(yù)判? ? 在數(shù)據(jù)驅(qū)動決策的時(shí)代,預(yù)測分析作為挖掘數(shù)據(jù)潛在價(jià)值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價(jià)值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點(diǎn),而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報(bào)考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢性檢驗(yàn):捕捉數(shù)據(jù)背后的時(shí)間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢性檢驗(yàn)如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時(shí)間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時(shí)間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實(shí)戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗(yàn):數(shù)據(jù)趨勢與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢變化以及識別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對策略? 長短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨(dú)特的門控機(jī)制,在 ...
2025-07-07統(tǒng)計(jì)學(xué)方法在市場調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場調(diào)研是企業(yè)洞察市場動態(tài)、了解消費(fèi)者需求的重要途徑,而統(tǒng)計(jì)學(xué)方法則是市場調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價(jià)值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03