Python數(shù)據(jù)可視化之環(huán)形圖
1.引言
環(huán)形圖(圓環(huán))在功能上與餅圖相同,整個(gè)環(huán)被分成不同的部分,用各個(gè)圓弧來(lái)表示每個(gè)數(shù)據(jù)所占的比例值。但其中心的空白可用于顯示其他相關(guān)數(shù)據(jù)展示,相比于標(biāo)準(zhǔn)餅圖提供了更豐富的數(shù)據(jù)信息輸出。

在本文中,我們將介紹 Matplolib中繪制圓環(huán)圖的兩種方法。使用餅圖和參數(shù)wedgeprops 的簡(jiǎn)單方法,以及使用極軸和水平條形圖的復(fù)雜方法。
2.方式一:餅圖形式
在 Matplotlib 中沒(méi)有繪制圓環(huán)圖的直接方法,但我們可以使用餅圖中的參數(shù)wedgeprops來(lái)快速地將餅圖轉(zhuǎn)換為環(huán)形圖。
首先我們先來(lái)畫一個(gè)簡(jiǎn)單的餅圖:
import matplotlib.pyplot as plt plt.pie([87,13], startangle=90, colors=['#5DADE2', '#515A5A']) plt.show()
結(jié)果如下:

接著我們添加參數(shù)wedgeprops 并定義環(huán)形圖邊緣的寬度,代碼如下:
fig, ax = plt.subplots(figsize=(6, 6))
?
ax.pie([87,13],?
? ? ? ?wedgeprops={'width':0.3},?
? ? ? ?startangle=90,?
? ? ? ?colors=['#5DADE2', '#515A5A'])
plt.show()結(jié)果如下:

這很簡(jiǎn)單。現(xiàn)在我們可以使用中心的空間來(lái)使我們的數(shù)據(jù)更加明顯。
代碼如下:
fig, ax = plt.subplots(figsize=(6, 6))
wedgeprops = {'width':0.3, 'edgecolor':'black', 'linewidth':3}
ax.pie([87,13], wedgeprops=wedgeprops, startangle=90, colors=['#5DADE2', '#515A5A'])
plt.title('Worldwide Access to Electricity', fontsize=24, loc='left')
plt.text(0, 0, "87%", ha='center', va='center', fontsize=42)
plt.text(-1.2, -1.2, "Source: ourworldindata.org/energy-access", ha='left', va='center', fontsize=12)
plt.show()結(jié)果如下:

當(dāng)我們有一個(gè)簡(jiǎn)單的比較需要顯示時(shí),圓環(huán)圖特別有用。在我看來(lái),使用它們的最佳方式是像一個(gè)圓形進(jìn)度條,比如我們有一個(gè)單一的比例要突出顯示的例子。
當(dāng)然,我們可以進(jìn)一步簡(jiǎn)化上圖圖表。
代碼如下:
fig, ax = plt.subplots(figsize=(6, 6))
data = [87, 13]
wedgeprops = {'width':0.3, 'edgecolor':'black', 'lw':3}
patches, _ = ax.pie(data, wedgeprops=wedgeprops, startangle=90, colors=['#5DADE2', 'white'])
patches[1].set_zorder(0)
patches[1].set_edgecolor('white')
plt.title('Worldwide Access to Electricity', fontsize=24, loc='left')
plt.text(0, 0, f"{data[0]}%", ha='center', va='center', fontsize=42)
plt.text(-1.2, -1.3, "Source: ourworldindata.org/energy-access", ha='left', va='top', fontsize=12)
plt.show()結(jié)果如下:
3.方式二:條形圖形式
盡管此解決方案比前一個(gè)解決方案更復(fù)雜,但它為定制提供了一些令人興奮的選項(xiàng)。
我們還是從一個(gè)簡(jiǎn)單的例子開始,代碼如下:
from math import pi
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection':'polar'})
data = 87?
startangle = 90
x = (data * pi *2)/ 100 # convert x data from percentage
left = (startangle * pi *2)/ 360 # convert start from angle
ax.barh(1, x, left=left, height=1, color='#5DADE2')
plt.ylim(-3, 3)
plt.show()結(jié)果如下:

接著我們來(lái)處理角度,我們必須先轉(zhuǎn)換每個(gè)元素的 x 坐標(biāo),然后再將其添加到軸上。
代碼如下:
from math import pi
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection':'polar'})
data = 87
startangle = 90
x = (data * pi *2)/ 100
left = (startangle * pi *2)/ 360 #this is to control where the bar starts
plt.xticks([])
plt.yticks([])
ax.spines.clear()
ax.barh(1, x, left=left, height=1, color='#5DADE2')?
plt.ylim(-3, 3)
plt.text(0, -3, "87%", ha='center', va='center', fontsize=42)
plt.show()結(jié)果如下:

使用此方法我們也達(dá)到了上述同樣的效果;當(dāng)然此時(shí)我們添加多個(gè)進(jìn)度條、定義它們之間的距離來(lái)使得可視化效果更加豐富。
代碼如下:
from math import pi import numpy as np from matplotlib.patches import Patch from matplotlib.lines import Line2D fig, ax = plt.subplots(figsize=(6, 6)) ax = plt.subplot(projection='polar') data = [82, 75, 91] startangle = 90 colors = ['#4393E5', '#43BAE5', '#7AE6EA'] xs = [(i * pi *2)/ 100 for i in data] ys = [-0.2, 1, 2.2] left = (startangle * pi *2)/ 360 #this is to control where the bar starts # plot bars and points at the end to make them round for i, x in enumerate(xs): ? ? ax.barh(ys[i], x, left=left, height=1, color=colors[i]) ? ? ax.scatter(x+left, ys[i], s=350, color=colors[i], zorder=2) ? ? ax.scatter(left, ys[i], s=350, color=colors[i], zorder=2) ? ?? plt.ylim(-4, 4) # legend legend_elements = [Line2D([0], [0], marker='o', color='w', label='Group A', markerfacecolor='#4393E5', markersize=10), ? ? ? ? ? ? ? ? ? Line2D([0], [0], marker='o', color='w', label='Group B', markerfacecolor='#43BAE5', markersize=10), ? ? ? ? ? ? ? ? ? Line2D([0], [0], marker='o', color='w', label='Group C', markerfacecolor='#7AE6EA', markersize=10)] ax.legend(handles=legend_elements, loc='center', frameon=False) # clear ticks, grids, spines plt.xticks([]) plt.yticks([]) ax.spines.clear() plt.show()
結(jié)果如下:

到此這篇關(guān)于Python數(shù)據(jù)可視化之環(huán)形圖的文章就介紹到這了,更多相關(guān)Python數(shù)據(jù)可視化環(huán)形圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python數(shù)據(jù)可視化之使用matplotlib繪制簡(jiǎn)單圖表
- Python?數(shù)據(jù)可視化神器Pyecharts繪制圖像練習(xí)
- Python利用matplotlib模塊數(shù)據(jù)可視化繪制3D圖
- Python?數(shù)據(jù)可視化實(shí)現(xiàn)5種炫酷的動(dòng)態(tài)圖
- python數(shù)據(jù)可視化Seaborn繪制山脊圖
- python數(shù)據(jù)可視化Seaborn畫熱力圖
- python數(shù)據(jù)可視化Pyecharts庫(kù)sankey修改桑葚圖顏色
- Python數(shù)據(jù)可視化Pyecharts庫(kù)實(shí)現(xiàn)桑葚圖效果
- Python?數(shù)據(jù)可視化超詳細(xì)講解折線圖的實(shí)現(xiàn)
相關(guān)文章
keras 獲取某層輸出 獲取復(fù)用層的多次輸出實(shí)例
這篇文章主要介紹了keras 獲取某層輸出 獲取復(fù)用層的多次輸出實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python實(shí)現(xiàn)變量數(shù)值交換及判斷數(shù)組是否含有某個(gè)元素的方法
這篇文章主要介紹了Python實(shí)現(xiàn)變量數(shù)值交換及判斷數(shù)組是否含有某個(gè)元素的方法,涉及Python字符串與數(shù)組的相關(guān)賦值、判斷操作技巧,需要的朋友可以參考下2017-09-09
Python Pytorch深度學(xué)習(xí)之自動(dòng)微分
今天小編就為大家分享一篇關(guān)于Pytorch自動(dòng)微分的文章,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-10-10
Python高級(jí)技巧之利用psutil和subprocess實(shí)現(xiàn)程序監(jiān)控與管理
本文介紹了如何使用Python的psutil和subprocess模塊監(jiān)控程序運(yùn)行狀態(tài),并提供了一個(gè)案例腳本,用于監(jiān)控目標(biāo)程序并在停止時(shí)自動(dòng)重啟,詳細(xì)介紹了subprocess模塊的基本用法和psutil模塊的系統(tǒng)信息獲取、進(jìn)程管理及資源監(jiān)控功能,需要的朋友可以參考下2024-09-09
python獲取網(wǎng)頁(yè)狀態(tài)碼示例
這篇文章主要介紹了python獲取網(wǎng)頁(yè)狀態(tài)碼示例,只需要2行代碼就可實(shí)現(xiàn)想要的功能,需要的朋友可以參考下2014-03-03

