当前位置:网站首页>matplotlib教程04---绘制常用的图形
matplotlib教程04---绘制常用的图形
2022-04-22 09:33:00 【KX-Lau】
欢迎关注公众号【Python开发实战】, 获取更多内容!
工具-matplotlib
使用matplotlib可以绘制出漂亮的图形。
极坐标图
绘制极坐标图,和创建子图时将projection属性设置为polar一样简单。
导入matplotlib
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
radius = 1
theta = np.linspace(0, 2*np.pi*radius, 1000)
plt.subplot(111, projection='polar')
plt.plot(theta, np.sin(5*theta), 'g-')
plt.plot(theta, 0.5*np.cos(20*theta), 'r-')
plt.show()

三维图
绘制三维图也非常简单。首先需要导入Axes3D, 用来注册3d投影。然后创建一个子图,并将projection属性设置为3d,这将返回一个Axes3DSubplot对象,使用它调用plot_surface()方法,并提供x、y、z坐标以及可选属性。
from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(x, y) # 根据坐标向量返回坐标矩阵
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
figure = plt.figure(1, figsize=(12, 4))
subplot3d = plt.subplot(111, projection='3d')
subplot3d.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=matplotlib.cm.coolwarm, linewidth=0.1)
plt.show()

等高线图
还可以通过等高线来绘制上面的数据。
plt.contourf(X, Y, Z, cmap=matplotlib.cm.coolwarm)
plt.colorbar()
plt.show()

散点图
绘制散点图,只需要调用scatter()函数,并提供点对应的x和y坐标即可。
from numpy.random import rand
x, y = rand(2, 100) # 返回一个形状为(2, 100)元素取值在[0,1)之间的数组 拆分为x和y
plt.scatter(x, y)
plt.show()

还可以选择提供每个点的大小。
x, y, scale = rand(3, 100)
scale = 500 * scale ** 5
plt.scatter(x, y, s=scale)
plt.show()

还有其他的属性可以设置,比如填充和边缘的颜色、alpha透明度等。
for color in ['red', 'green', 'blue']:
n= 100
x, y = rand(2, n)
scale = 500.0 * rand(n) ** 5
plt.scatter(x, y, s=scale, edgecolors='blue', color=color, alpha=0.3)
plt.grid(True)
plt.show()

画线
可以简单地使用plot()函数画线。在给定斜率和截距的情况下,可以很方便地创建一个函数来画一条无限长的线。还可以使用hlines和vlines函数来绘制水平和垂直线段。
from numpy.random import randn
def plot_line(axis, slope, intercept, **kwargs):
# slope斜率 intercept截距
xmin, xmax = axis.get_xlim()
plt.plot([xmin, xmax], [xmin*slope+intercept, xmax*slope+intercept], **kwargs)
x = randn(1000)
y = 0.5*x+5+randn(1000)*2
plt.axis([-2.5, 2.5, -5, 15]) # 坐标轴的范围
plt.scatter(x, y, alpha=0.2) # 散点图
plt.plot(1, 0, 'ro') # 绘制点
plt.vlines(1, -5, 0, color='red') # 垂直线段
plt.hlines(0, -2.5, 1, color='yellow') # 水平线段
plot_line(axis=plt.gca(), slope=0.5, intercept=5, color='green') # 画线
plt.grid(True)
plt.show()

直方图
data = [1, 1.1, 1.8, 2, 2.1, 3.2, 3, 3, 3, 3]
plt.subplot(211)
plt.hist(data, bins=10, rwidth=0.8) # bins表示绘制条柱的个数 默认为10 rwidth表示条柱宽度的百分比
plt.subplot(212)
plt.hist(data, bins=[1, 1.5, 2, 2.5, 3], rwidth=0.95)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()

data1 = randn(400)
data2 = randn(500) + 3
data3 = randn(450) + 6
data4a = randn(200) + 9
data4b = randn(100) + 10
plt.hist(data1, bins=5, color='g', alpha=0.75, label='bar hist')
plt.hist(data2, color='b', alpha=0.65, histtype='stepfilled', label='stepfilled hist')
plt.hist(data3, color='r', histtype='step', label='step hist')
plt.hist((data4a, data4b), color=('r', 'm'), alpha=0.55, histtype='barstacked', label=('barstacked a', 'barstacked b'))
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.legend()
plt.grid(True)
plt.show()

版权声明
本文为[KX-Lau]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38727995/article/details/124324946
边栏推荐
- uni-app项目之 商品列表的下拉刷新 与 上拉加载更多
- An analysis of the application of MOS tube series parallel drive - Kia MOS tube
- 【C语言进阶10——字符和字符串函数及其模拟实现(1)】
- L2-033 简单计算器 (25 分)
- 好用的记笔记软件
- 安装Navicat 15 详解及相关问题详解
- GS waveform analysis of depth resolved MOS transistor Kia MOS transistor
- LeetCode46-全排列
- 找出二维数组最大的一个数
- 【Flutter 专题】125 图解自传 ACE_ICON.ttf 图标库
猜你喜欢

ShardingSphere广播表和绑定表

Depth first search (I): middle order traversal of binary tree (force buckle)

Heap overflow of kernel PWN basic tutorial

泳道跨域问题

2022起重机司机(限桥式起重机)考试题库及在线模拟考试

LeetCode46-全排列

微搭低代码零基础入门课(第二课)

MOS tube and MOS tube driving circuit case analysis - Kia MOS tube

在销量压力下,国产手机开始降价了,但还没有放下最后的面子

深度学习遥感场景分类数据集整理
随机推荐
MOS tube and MOS tube driving circuit case analysis - Kia MOS tube
L3-007 天梯地图 (30 分)(条件dij
超越iTerm! 号称下一代终端神器,功能贼强大!
电脑拆机清灰及机械硬盘安装记录
openlayer中,svg图片无width如何修改大小
L2-030 Icelander (25 points) (nearest public ancestor)
Easy to use screenshot software
在线YAML转Properties工具
Kernel pwn 基础教程之 Heap Overflow
项目实训-读报僵尸
Softing dataFEED OPC Suite:赋予工业设备物联网连接能力
MOS管开关频率最高多少如何测算-KIA MOS管
深度优先搜索(一):二叉树的中序遍历(力扣)
Command ‘yum‘ not found, but can be installed with: apt install yum
Quantitative investment learning -- Introduction to orderflow
I/O知识点总结
Halo 开源项目学习(一):项目启动
在 colab 上使用 yolov4
Unicorn Bio筹集320万美元资金 将用于培养肉的原型设备变成商业产品
jdbc连不上mysql 报错为 Access denied for user 'root'@'***' (using password: YES)