Python數(shù)據(jù)可視化之環(huán)形圖
1.引言
環(huán)形圖(圓環(huán))在功能上與餅圖相同,整個(gè)環(huán)被分成不同的部分,用各個(gè)圓弧來表示每個(gè)數(shù)據(jù)所占的比例值。但其中心的空白可用于顯示其他相關(guān)數(shù)據(jù)展示,相比于標(biāo)準(zhǔn)餅圖提供了更豐富的數(shù)據(jù)信息輸出。
在本文中,我們將介紹 Matplolib
中繪制圓環(huán)圖的兩種方法。使用餅圖和參數(shù)wedgeprops 的簡單方法,以及使用極軸和水平條形圖的復(fù)雜方法。
2.方式一:餅圖形式
在 Matplotlib
中沒有繪制圓環(huán)圖的直接方法,但我們可以使用餅圖中的參數(shù)wedgeprops
來快速地將餅圖轉(zhuǎn)換為環(huán)形圖。
首先我們先來畫一個(gè)簡單的餅圖:
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é)果如下:
這很簡單?,F(xiàn)在我們可以使用中心的空間來使我們的數(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è)簡單的比較需要顯示時(shí),圓環(huán)圖特別有用。在我看來,使用它們的最佳方式是像一個(gè)圓形進(jìn)度條,比如我們有一個(gè)單一的比例要突出顯示的例子。
當(dāng)然,我們可以進(jì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è)簡單的例子開始,代碼如下:
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é)果如下:
接著我們來處理角度,我們必須先轉(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)度條、定義它們之間的距離來使得可視化效果更加豐富。
代碼如下:
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python數(shù)據(jù)可視化之使用matplotlib繪制簡單圖表
- Python?數(shù)據(jù)可視化神器Pyecharts繪制圖像練習(xí)
- Python利用matplotlib模塊數(shù)據(jù)可視化繪制3D圖
- Python?數(shù)據(jù)可視化實(shí)現(xiàn)5種炫酷的動態(tài)圖
- python數(shù)據(jù)可視化Seaborn繪制山脊圖
- python數(shù)據(jù)可視化Seaborn畫熱力圖
- python數(shù)據(jù)可視化Pyecharts庫sankey修改桑葚圖顏色
- Python數(shù)據(jù)可視化Pyecharts庫實(shí)現(xiàn)桑葚圖效果
- Python?數(shù)據(jù)可視化超詳細(xì)講解折線圖的實(shí)現(xiàn)
相關(guān)文章
django中上傳圖片分頁三級聯(lián)動效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了django中上傳圖片分頁三級聯(lián)動效果的實(shí)現(xiàn)代碼,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-08-08Python常用配置文件ini、json、yaml讀寫總結(jié)
用的配置文件格式有ini、json、yaml等,下面簡單給大家介紹下,Python如何讀寫這幾種格式的文件,對Python讀寫ini、json、yaml配置文件相關(guān)知識感興趣的朋友一起看看吧2021-07-07python的numpy模塊安裝不成功簡單解決方法總結(jié)
這篇文章主要介紹了python的numpy模塊安裝不成功簡單解決方法總結(jié),分享了四種python模塊導(dǎo)入不成功的解決方法,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12Python使用import導(dǎo)入本地腳本及導(dǎo)入模塊的技巧總結(jié)
這篇文章主要介紹了Python使用import導(dǎo)入本地腳本及導(dǎo)入模塊的技巧,結(jié)合實(shí)例形式總結(jié)分析了Python使用import導(dǎo)入本地腳本及導(dǎo)入模塊的使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-08-08使用python?pywin32模塊創(chuàng)建windows服務(wù)實(shí)例探究
這篇文章主要為大家介紹了使用python?pywin32模塊創(chuàng)建windows服務(wù)實(shí)現(xiàn)實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Python中sklearn實(shí)現(xiàn)交叉驗(yàn)證示例分析
這篇文章主要介紹了Python中sklearn實(shí)現(xiàn)交叉驗(yàn)證,本文python的版本為3.8,各個(gè)版本之間函數(shù)名字略有不同,但是原理都是一樣的,集成開發(fā)環(huán)境使用的是Anaconda的Spyder,需要的朋友可以參考下2023-08-08Python利用FlashText算法實(shí)現(xiàn)替換字符串
FlashText算法是由?Vikash?Singh?于2017年發(fā)表的大規(guī)模關(guān)鍵詞替換算法,比正則表達(dá)式替換快M倍以上,這個(gè)M是需要替換的關(guān)鍵詞數(shù)量,關(guān)鍵詞越多,F(xiàn)lashText算法的優(yōu)勢就越明顯。本文將詳細(xì)這一算法,需要的可以參考一下2022-03-03Python實(shí)現(xiàn)基于標(biāo)記的分水嶺分割算法
分水嶺技術(shù)是一種眾所周知的分割算法,特別適用于提取圖片中的相鄰或重疊對象。本文將用Python實(shí)現(xiàn)基于標(biāo)記的分水嶺分割算法,感興趣的可以了解一下2022-07-07Python?pass函數(shù)使用及其應(yīng)用的詳解
Python中,pass是一種空語句,即不做任何事情,只起到占位符的作用,本文主要介紹了Python?pass函數(shù)使用及其應(yīng)用的詳解,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07