python畫環(huán)形圖的方法
更新時間:2020年03月25日 10:26:31 作者:goacademic
這篇文章主要為大家詳細介紹了python畫環(huán)形圖的相關(guān)代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python畫環(huán)形圖的具體代碼,供大家參考,具體內(nèi)容如下
import os
import pandas as pd
import matplotlib.pyplot as plt
import random
import numpy as np
# 獲取漸變色的list
def gradual(number):
colors = []
h = 0.00001
gradual2 = random.uniform(0, 1)
r = gradual2
if gradual2 >= 0.5:
g = h
b = gradual2
else:
g = gradual2
b = h
colors.append((r, g, b, 1))
for i in range(number - 1):
# 大于0.5 則為綠色漸變,小于則為藍色漸變
if gradual2 >= 0.5:
g = 1 if ((1 - h) / number) > 1 else (g + (1 - h) / number)
else:
b = 1 if ((1 - h) / number) > 1 else (b + (1 - h) / number)
colors.append((r, g, b, 1))
return colors
# 獲取綠色的個數(shù),standardRedIndex為要將那個顏色改為紅色
def listGreen(number, standardRedIndex):
colors = []
for i in range(number):
if i == standardRedIndex - 1:
colors.append('r')
else:
colors.append('#6CAD4F')
return colors
# 畫環(huán)形圖
def circularGraph(outerData, innerData, labels, standardRedIndex):
data = pd.DataFrame([outerData, innerData], columns=labels)
# 設(shè)置字體這樣才可以顯示中文
plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
plt.rcParams['axes.unicode_minus'] = False
plt.figure(figsize=(8, 5))
colors = gradual(len(labels))
# 數(shù)據(jù)內(nèi)環(huán)
plt.pie(data.iloc[1, :], radius=0.65, wedgeprops=dict(width=0.3, edgecolor='w'), colors=colors)
# 數(shù)據(jù)外環(huán)
plt.pie(data.iloc[0, :], radius=1, wedgeprops=dict(width=0.3, edgecolor='w'),
colors=listGreen(len(labels), standardRedIndex))
# 獲取ax label
ax = plt.subplot(1, 1, 1)
# loc是位置,bbox_to_anchor是位置坐標,borderaxespad將圖例放外面 frameon=False去掉圖例邊框
# bbox_to_anchor 的y坐標
y = -1 / 40 * len(labels) + 0.5
ax.legend(labels, loc=4, bbox_to_anchor=(1.3, y), borderaxespad=0., frameon=False)
plt.show()
circularGraph([30, 30, 20, 40, 20, 20, 40, 20, 20, 40, 20], [30, 30, 20, 40, 20, 20, 40, 20, 20, 40, 20],
['甲硫桿菌', '霍爾德曼氏菌屬', 'Faecali菌屬', '瘤胃菌屬', 'Faecali菌屬', 'Faecali菌屬', '瘤胃菌屬', 'Faecali菌屬', 'Faecali菌屬', '瘤胃菌屬', 'Faecali菌屬'], 3)

更多精彩內(nèi)容請點擊專題: 《python圖片處理操作》
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
flask使用session保存登錄狀態(tài)及攔截未登錄請求代碼
這篇文章主要介紹了flask使用session保存登錄狀態(tài)及攔截未登錄請求代碼,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
python開發(fā)之基于thread線程搜索本地文件的方法
這篇文章主要介紹了python開發(fā)之基于thread線程搜索本地文件的方法,以完整實例形式分析了Python基于多線程處理搜索問題的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2015-11-11
pyecharts如何實現(xiàn)顯示數(shù)據(jù)為百分比的柱狀圖
這篇文章主要介紹了pyecharts如何實現(xiàn)顯示數(shù)據(jù)為百分比的柱狀圖,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
pygame實現(xiàn)俄羅斯方塊游戲(基礎(chǔ)篇1)
這篇文章主要為大家介紹了pygame實現(xiàn)俄羅斯方塊游戲基礎(chǔ)的第1篇,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10
一文詳解PyQt5中實現(xiàn)不規(guī)則窗口的顯示
這篇文章主要為大家詳細介紹了Python?PyQt5中實現(xiàn)不規(guī)則窗口的顯示的相關(guān)資料,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考一下2022-12-12
PyTorch中tensor.backward()函數(shù)的詳細介紹及功能實現(xiàn)
backward()?函數(shù)是PyTorch框架中自動求梯度功能的一部分,它負責(zé)執(zhí)行反向傳播算法以計算模型參數(shù)的梯度,這篇文章主要介紹了PyTorch中tensor.backward()函數(shù)的詳細介紹,需要的朋友可以參考下2024-02-02

