python+matplotlib绘制饼图散点图实例代码

yipeiwu_com5年前Python基础

本文是从matplotlib官网上摘录下来的一个实例,实现的功能是Python+matplotlib绘制自定义饼图作为散点图的标记,具体如下。

首先看下演示效果

实例代码:

import numpy as np
import matplotlib.pyplot as plt

# first define the ratios
r1 = 0.2    # 20%
r2 = r1 + 0.4 # 40%

# define some sizes of the scatter marker
sizes = np.array([60, 80, 120])

# calculate the points of the first pie marker
#
# these are just the origin (0,0) +
# some points on a circle cos,sin
x = [0] + np.cos(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
y = [0] + np.sin(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
xy1 = list(zip(x, y))
s1 = np.max(xy1)

x = [0] + np.cos(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
y = [0] + np.sin(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
xy2 = list(zip(x, y))
s2 = np.max(xy2)

x = [0] + np.cos(np.linspace(2 * np.pi * r2, 2 * np.pi, 10)).tolist()
y = [0] + np.sin(np.linspace(2 * np.pi * r2, 2 * np.pi, 10)).tolist()
xy3 = list(zip(x, y))
s3 = np.max(xy3)

fig, ax = plt.subplots()
ax.scatter(range(3), range(3), marker=(xy1, 0),
      s=s1 ** 2 * sizes, facecolor='blue')
ax.scatter(range(3), range(3), marker=(xy2, 0),
      s=s2 ** 2 * sizes, facecolor='green')
ax.scatter(range(3), range(3), marker=(xy3, 0),
      s=s3 ** 2 * sizes, facecolor='red')

plt.show()

总结

以上就是本文关于python+matplotlib绘制饼图散点图实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

Python实现网页截图(PyQT5)过程解析

方案说明 功能要求:实现网页加载后将页面截取成长图片 涉及模块:PyQT5 PIL 逻辑说明: 1:完成窗口设置,利用PyQT5 QWebEngineView加载网页地址,待网页加...

python语言基本语句用法总结

python语句与语法 1.python简单语句的基本介绍 >>> while True: #简单的while循环 ... reply = input('Ente...

python实现简单加密解密机制

本文使用python实现一个简单的加密解密机制。 描述:结合26个字母、以一个单词作为秘钥,使用python实现简单的加密解密机制 秘钥:大写的英文字符串 明文:包含空格、大小写字母、数...

Python数据可视化库seaborn的使用总结

Python数据可视化库seaborn的使用总结

seaborn是python中的一个非常强大的数据可视化库,它集成了matplotlib,下图为seaborn的官网,如果遇到疑惑的地方可以到官网查看。http://seaborn.py...

python队列原理及实现方法示例

本文实例讲述了python队列原理及实现方法。分享给大家供大家参考,具体如下: 队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。 队列是一种先进先出的(Fi...