用python画环形图

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


# 获取绿色的个数,standardRedIndex为要将那个颜色改为红色
def listGreen(number, standardRedIndex):
    colors = []
    for i in range(number):
        if i == standardRedIndex - 1:
            colors.append('r')
        else:
            colors.append('#6CAD4F')
    return colors


# 画环形图
def circularGraph(outerData, innerData, labels, standardRedIndex):
    data = pd.DataFrame([outerData, innerData], columns=labels)
    # 设置字体这样才可以显示中文
    plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
    plt.rcParams['axes.unicode_minus'] = False
    plt.figure(figsize=(8, 5))
    colors = gradual(len(labels))
    # 数据内环
    plt.pie(data.iloc[1, :], radius=0.65, wedgeprops=dict(width=0.3, edgecolor='w'), colors=colors)
    # 数据外环
    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)

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值