歡迎關(guān)注 ,專注 Python、數(shù)據(jù)分析、數(shù)據(jù)挖掘、好玩工具!
如果要問:Python 中有那些可視化工具庫?我想很多人都能想起來 matplotlib,這是一款初學(xué)者繞不開的庫,但隨著對數(shù)據(jù)可視化的要求越來越高,matplotlib 已無法滿足了。
今天我將和大家詳細(xì)講解 Pyecharts 模塊,說到它我們就不得不提 Echarts,它是一個由百度開源的數(shù)據(jù)可視化,結(jié)合巧妙的交互性,精巧的圖表設(shè)計,得到了開發(fā)者的認(rèn)可。而 Python 是一門富有表達(dá)力的語言,很適合用于數(shù)據(jù)處理。分析遇上數(shù)據(jù)可視化時,pyecharts 誕生了。歡迎收藏學(xué)習(xí),喜歡點贊支持。 文末提供技術(shù)交流群。
Pyecharts 具有如下特點:
- 簡潔的API 設(shè)計,使用如絲滑般的照片,支持鏈?zhǔn)秸{(diào)用
- 囊括了30+種常見圖表,應(yīng)有盡有
- 支持主流 Notebook 環(huán)境,Jupyter Notebook 和 JupyterLab
- 可輕松集成至Flask,Django等主流Web框架
- 高度智能的配置項,可輕松搭配出精美的圖表
- 詳細(xì)的文檔和示例,幫助開發(fā)者快速的上手項目
- 多達(dá) 400+ 地圖文件以及自己的百度地圖,為地理數(shù)據(jù)擴(kuò)展提供用戶支持
官方Github鏈接:https://github.com/pyecharts/pyecharts/
下面我們來詳細(xì)講解它
01 安裝與導(dǎo)入模塊
說到安裝模塊,我們可以這樣來進(jìn)行,
使用 Pyecharts 創(chuàng)建圖形的基本步驟是
1. 準(zhǔn)備數(shù)據(jù)
2. 設(shè)計圖形的樣式、背景顏色
3. Pyecharts 繪圖
4. 設(shè)計圖表的標(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é)果是
02 數(shù)據(jù)準(zhǔn)備
import pandas as pd
import numpy as np
data = pd.DataFrame({'x':np.arange(1,101),'y':["隨機(jī)生成的數(shù)字"]})
df = pd.read_excel("你的文件的路徑")
03 Pycharts還提供內(nèi)置的數(shù)據(jù)集
Pyecharts內(nèi)部還提供了一些數(shù)據(jù)集,主要包含類別數(shù)據(jù)、時間數(shù)據(jù)、顏色數(shù)據(jù)、地理數(shù)據(jù)、世界人口數(shù)據(jù)等等,通過choose()方法來隨機(jī)選擇使用哪個
def choose(self) -> list:
return random.choice(
[
self.clothes,
self.drinks,
self.phones,
self.fruits,
self.animal,
self.dogs,
self.week,
]
)
04 圖形的樣式
說到圖形的樣式,大概都這么幾種
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"
06 設(shè)置標(biāo)題、副標(biāo)題
設(shè)置標(biāo)題以及副標(biāo)題的代碼如下
set_global_opts(title_opts=opts.TitleOpts(title="這是主標(biāo)題",subtitle="這是副標(biāo)題"))
07 設(shè)置圖例與位置
legend_opts=opts.LegendOpts(type_="scroll", orient="vertical",pos_top="15%",pos_left="7%")) # 圖裂的位置
label_opts=opts.LabelOpts(formatter=": {c}") # 結(jié)果的展現(xiàn)形式
08 導(dǎo)出結(jié)果
render("test.html")
# 如果是在jupyter notebook當(dāng)中
render_notebook()
09 Pyecharts繪圖
柱狀圖
同個品類不同類目的柱子可以堆疊起來呈現(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")
)
有時候橫坐標(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")
)
我們也可以這樣放柱形圖
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")
)
我們也可以通過拖動里面的柱子來實現(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")
)
在柱狀圖當(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")
)
箱型圖
箱型圖更加有利于我們來觀察數(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ù)情況,例如展示超市全年的銷售額,從而看出具體某個月份或者某個星期的銷售額比較低
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="詞云圖實例", data_pair=data, word_size_range=[5, 100])
.set_global_opts(
title_opts=opts.TitleOpts(
title="詞云圖實例", 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 = (
EffectScatter()
.add_xaxis(Faker.choose())
.add_yaxis("商家1", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="漣漪散點圖-基本示例"))
.render("effectscatter_test.html")
)
地圖
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.faker import Faker
from pyecharts.globals import ChartType
c = (
Geo()
.add_schema(maptype="china")
.add(
"geo",
[list(z) for z in zip(Faker.provinces, Faker.values())],
type_=ChartType.EFFECT_SCATTER,
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="Geo-EffectScatter"))
.render("geo_effectscatter.html")
)
關(guān)系圖
from pyecharts import options as opts
from pyecharts.charts import Graph
nodes_data = [
opts.GraphNode(name="結(jié)點1", symbol_size=10),
opts.GraphNode(name="結(jié)點2", symbol_size=20),
opts.GraphNode(name="結(jié)點3", symbol_size=30),
opts.GraphNode(name="結(jié)點4", symbol_size=40),
opts.GraphNode(name="結(jié)點5", symbol_size=50),
opts.GraphNode(name="結(jié)點6", symbol_size=60),
]
links_data = [
opts.GraphLink(source="結(jié)點1", target="結(jié)點2", value=2),
opts.GraphLink(source="結(jié)點2", target="結(jié)點3", value=3),
opts.GraphLink(source="結(jié)點3", target="結(jié)點4", value=4),
opts.GraphLink(source="結(jié)點4", target="結(jié)點5", value=5),
opts.GraphLink(source="結(jié)點5", target="結(jié)點6", value=6),
opts.GraphLink(source="結(jié)點6", target="結(jié)點1", value=7),
]
c = (
Graph()
.add(
"",
nodes_data,
links_data,
repulsion=4000,
edge_label=opts.LabelOpts(
is_show=True, position="middle", formatter=" 的數(shù)據(jù) {c}"
),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Graph-GraphNode-GraphLink-WithEdgeLabel")
)
.render("graph_with_edge_options.html")
)
技術(shù)交流
歡迎轉(zhuǎn)載、收藏、有所收獲點贊支持一下!
目前開通了技術(shù)交流群,群友已超過2000人,添加時最好的備注方式為:來源+興趣方向,方便找到志同道合的朋友文章來源:http://www.zghlxwxcb.cn/news/detail-403144.html
- 方式①、發(fā)送如下圖片至微信,長按識別,后臺回復(fù):加群;
- 方式②、添加微信號:dkl88191,備注:來自CSDN
- 方式③、微信搜索公眾號:Python學(xué)習(xí)與數(shù)據(jù)挖掘,后臺回復(fù):加群
文章來源地址http://www.zghlxwxcb.cn/news/detail-403144.html
到了這里,關(guān)于Python可視化神器:pyecharts,輕松繪制 30+ 種超實用精美圖表!的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!