python画环形图的方法

作者:goacademic 时间:2023-02-12 09:54:24 

本文实例为大家分享了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)

python画环形图的方法

更多精彩内容请点击专题: 《python图片处理操作》

来源:https://blog.csdn.net/Carlsummer/article/details/86588716

标签:python,环形图
0
投稿

猜你喜欢

  • golang值类型转换成[]uint8类型的操作

    2024-04-25 15:04:52
  • python如何实现反向迭代

    2023-05-17 17:37:55
  • python区块及区块链的开发详解

    2023-07-05 16:26:36
  • javascript DOM实用学习资料第1/3页

    2024-04-25 13:09:44
  • 详解MySQL存储过程的创建和调用

    2024-01-24 18:06:21
  • Python当中的array数组对象实例详解

    2022-02-12 23:22:47
  • python中列表(list)和元组(tuple)的深入讲解

    2021-08-18 01:39:02
  • Python 普通最小二乘法(OLS)进行多项式拟合的方法

    2021-03-09 23:06:58
  • 使用Python编写Linux系统守护进程实例

    2022-06-09 16:50:52
  • Python实现对一个函数应用多个装饰器的方法示例

    2022-01-23 17:30:22
  • pandas 添加空列并赋空值案例

    2022-12-04 13:48:16
  • Javascript怎样使用SessionStorage和LocalStorage

    2023-09-01 03:49:49
  • PHP的PDO预定义常量讲解

    2023-06-09 13:35:54
  • sqlserver中重复数据值只取一条的sql语句

    2012-06-06 19:46:31
  • 一个有趣的SQL命题 用一条语句切换BIT型的真假值

    2024-01-24 04:57:38
  • 使用jQuery简化Ajax开发

    2010-04-11 21:09:00
  • Python函数实现学员管理系统

    2023-06-22 03:32:30
  • redis查看连接数及php模拟并发创建redis连接的方法

    2023-11-16 11:47:14
  • php通过获取头信息判断图片类型的方法

    2023-11-10 00:31:50
  • 如何使用Python Matplotlib绘制条形图

    2023-09-21 04:41:46
  • asp之家 网络编程 m.aspxhome.com