python画环形图的方法

所属分类: 脚本专栏 / python 阅读数: 1090
收藏 0 赞 0 分享

本文实例为大家分享了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图片处理操作》

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

pandas的qcut()方法详解

这篇文章主要介绍了pandas的qcut()方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

从列表或字典创建Pandas的DataFrame对象的方法

这篇文章主要介绍了从列表或字典创建Pandas的DataFrame对象的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas.DataFrame的pivot()和unstack()实现行转列

这篇文章主要介绍了pandas.DataFrame的pivot()和unstack()实现行转列,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

pandas中的series数据类型详解

这篇文章主要介绍了pandas中的series数据类型详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas 时间格式转换的实现

这篇文章主要介绍了pandas 时间格式转换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python中时间、日期、时间戳的转换的实现方法

这篇文章主要介绍了python中时间、日期、时间戳的转换的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas进行时间数据的转换和计算时间差并提取年月日

这篇文章主要介绍了pandas进行时间数据的转换和计算时间差并提取年月日,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法

这篇文章主要介绍了详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python和c语言的主要区别总结

在本篇文章里小编给各位整理了关于python和c语言的主要区别的相关知识帖内容,有需要的朋友们学习阅读下。
收藏 0 赞 0 分享

选择Python写网络爬虫的优势和理由

在本篇文章里小编给各位整理了一篇关于选择Python写网络爬虫的优势和理由以及相关代码实例,有兴趣的朋友们阅读下吧。
收藏 0 赞 0 分享
查看更多