python畫環(huán)形圖的方法
本文實(shí)例為大家分享了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 則為綠色漸變,小于則為藍(lán)色漸變 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 # 獲取綠色的個(gè)數(shù),standardRedIndex為要將那個(gè)顏色改為紅色 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是位置坐標(biāo),borderaxespad將圖例放外面 frameon=False去掉圖例邊框 # bbox_to_anchor 的y坐標(biāo) 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)容請(qǐng)點(diǎn)擊專題: 《python圖片處理操作》
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一步一步教你用Python?pyglet仿制鴻蒙系統(tǒng)里的時(shí)鐘
pyglet是一個(gè)面向Python的跨平臺(tái)窗口、多媒體庫,它可以用于創(chuàng)建游戲和多媒體應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于如何一步一步教你用Python?pyglet仿制鴻蒙系統(tǒng)里的時(shí)鐘,需要的朋友可以參考下2024-03-03Python調(diào)用http-post接口的實(shí)現(xiàn)方式
這篇文章主要介紹了Python調(diào)用http-post接口的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08calendar在python3時(shí)間中常用函數(shù)舉例詳解
這篇文章主要介紹了calendar在python3時(shí)間中常用函數(shù)的相關(guān)文章,對(duì)此知識(shí)點(diǎn)有興趣的朋友們可以學(xué)習(xí)下。2020-11-11python?Pandas之DataFrame索引及選取數(shù)據(jù)
這篇文章主要介紹了python?Pandas之DataFrame索引及選取數(shù)據(jù),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07pip安裝py_zipkin時(shí)提示的SSL問題對(duì)應(yīng)
今天小編就為大家分享一篇關(guān)于pip安裝py_zipkin時(shí)提示的SSL問題對(duì)應(yīng),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12