用python 绘制茎叶图和复合饼图

作者:哆啦梦乐园 时间:2023-08-04 10:34:54 

茎叶图


from itertools import groupby
nums2=[225, 232,232,245,235,245,270,225,240,240,217,195,225,185,200,
   220,200,210,271,240,220,230,215,252,225,220,206,185,227,236]
for k, g in groupby(sorted(nums2), key=lambda x: int(x) // 10):
 print (k, list(g))
 # print('k', k)
 # print('g', list(g))
 lst = map(str, [int(y) % 10 for y in list(g)])
 print (k, '|', ' '.join(lst))

输出:


18 | 5 5
19 | 5
20 | 0 0 6
21 | 0 5 7
22 | 0 0 0 5 5 5 5 7
23 | 0 2 2 5 6
24 | 0 0 0 5 5
25 | 2
27 | 0 1

说明:

1./ 就表示 浮点数除法,返回浮点结果; // 表示整数除法。

2.itertools.groupby 按照分组函数的值对元素进行分组。


>>> from itertools import groupby
>>> x = groupby(range(10), lambda x: x < 5 or x > 8)
>>> for condition, numbers in x:
print(condition, list(numbers))
输出:
True [0, 1, 2, 3, 4]
False [5, 6, 7, 8]
True [9]

>>> [k for k, g in groupby('AAAABBBCCDAABBB')]
['A', 'B', 'C', 'D', 'A', 'B']
>>> [list(g) for k, g in groupby('AAAABBBCCD')]
[['A', 'A', 'A', 'A'], ['B', 'B', 'B'], ['C', 'C'], ['D']]

3.map(function, iterable, ...) 根据提供的函数对指定序列做映射。第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。
4.循环加处理的例子


>>> [int(y) % 10 for y in [22,73,34,92,45]]
[2, 3, 4, 2, 5]

复合饼图


import numpy as np
import matplotlib as mpl
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch

# 使图表元素中正常显示中文
mpl.rcParams['font.sans-serif'] = 'SimHei'
# 使坐标轴刻度标签正常显示负号
mpl.rcParams['axes.unicode_minus'] = False

#制画布
fig = plt.figure(figsize=(9,5.0625), facecolor='cornsilk')
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

# 调整子区布局
fig.subplots_adjust(wspace=0)

# 大饼图的制作
labels = ['成都','武汉','昆明','贵阳','西安','其它']
size = [802,530,477,256,233,307]
# 分裂距离
explode=(0,0,0,0,0,0.1)
ax1.pie(size,        # 数据
   autopct='%1.1f%%',  # 锲形块的数据标签格式
   startangle=30,    # 锲形块开始角度
   labels=labels,
   colors=cm.Blues(range(10, 300, 50)),
   explode=explode)

#小饼图的制作
labels2 = ['西宁','拉萨','乌鲁木齐','兰州']
size2 = [102,79, 76, 50]
width=0.2
ax2.pie(size2,
   autopct='%1.1f%%',
   startangle=90,
   labels=labels2,
   colors=cm.Blues(range(10, 300, 50)),
   radius=0.5,
   shadow=False)

#使用ConnectionPatch画出两个饼图的间连线
#先得到饼图边缘的数据
theta1, theta2 = ax1.patches[-1].theta1, ax1.patches[-1].theta2
center, r   = ax1.patches[-1].center, ax1.patches[-1].r
#画出上边缘的连线
x = r*np.cos(np.pi/180*theta2)+center[0]
y = np.sin(np.pi/180*theta2)+center[1]
con1 = ConnectionPatch(xyA=(0, 0.5),
           xyB=(x,y),
           coordsA=ax2.transData,
           coordsB=ax1.transData,
           axesA=ax2,axesB=ax1)
print(-width/2, 0.5)
print(x,y)

#画出下边缘的连线
x = r*np.cos(np.pi/180*theta1) + center[0]
y = np.sin(np.pi/180*theta1) + center[1]
con2 = ConnectionPatch(xyA=(-0.1, -0.49),
           xyB=(x,y),
           coordsA='data',
           coordsB='data',
           axesA=ax2,axesB=ax1)

# 添加连接线
for con in [con1, con2]:
 con.set_color('gray')
 ax2.add_artist(con)
 con.set_linewidth(1)

plt.show()

输出:

用python 绘制茎叶图和复合饼图

来源:https://www.cnblogs.com/feily/p/14429244.html

标签:python,绘图,复合饼图,茎叶图
0
投稿

猜你喜欢

  • go语言编程学习实现图的广度与深度优先搜索

    2024-05-22 10:16:34
  • Mysql数据库百万级数据测试索引效果

    2024-01-24 01:30:03
  • 详解python中各种文件打开模式

    2023-08-30 09:10:30
  • MSSQL木马修复,中木马后的处理方法

    2024-01-21 10:47:13
  • Python 微信公众号文章爬取的示例代码

    2021-11-06 22:39:20
  • python数据化运营的重要意义

    2021-05-05 21:02:27
  • go语言中sort包的实现方法与应用详解

    2024-02-20 13:56:39
  • Python tkinter实现桌面软件流程详解

    2022-03-26 09:51:32
  • python 实现GUI(图形用户界面)编程详解

    2023-10-05 15:11:34
  • Python3 解决读取中文文件txt编码的问题

    2022-10-22 13:14:43
  • 如何利用Python实现简单C++程序范围分析

    2022-07-19 00:32:48
  • Python实现判断一个整数是否为回文数算法示例

    2022-05-13 12:01:07
  • tensorflow对图像进行拼接的例子

    2022-05-30 02:11:49
  • Python中每次处理一个字符的5种方法

    2023-09-26 02:49:13
  • Python 包含汉字的文件读写之每行末尾加上特定字符

    2022-02-01 13:13:46
  • vscode配置setting.json文件实现eslint自动格式代码

    2022-06-14 00:53:05
  • 正视WEB标准,一本全面的标准参考书

    2009-05-30 16:36:00
  • ASP调试方法图文教程

    2008-01-26 19:32:00
  • ASP动态页服务器端的处理原理

    2007-09-14 10:07:00
  • Python实现图形用户界面计算器

    2022-09-02 06:07:22
  • asp之家 网络编程 m.aspxhome.com