python基础知识(十二):matplotlib的基本用法二

1. 散点图

import matplotlib.pyplot as plt  # 导入matplotlib库画图
import numpy as np  # 导入numpy库产生数据
n = 1024
X = np.random.normal(0, 1, n)  # 生成1024个标准正太分布点
Y = np.random.normal(0, 1, n)  # 生成1024个标准正太分布点
T = np.arctan2(Y, X)  # 给Y,X上色
plt.scatter(X, Y, s=75, c=T, alpha=0.5)
plt.xlim((-1.5, 1.5))
plt.ylim((-1.5, 1.5))
plt.xticks(())
plt.yticks(())

plt.show()

在这里插入图片描述

2. 三维图

import matplotlib.pyplot as plt  # 导入matplotlib库画图
import numpy as np  # 导入numpy库产生数据
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()  # 画一张图
ax = Axes3D(fig)  # 采用3D坐标
X = np.arange(-4, 4, 0.25)  # 产生x数据
Y = np.arange(-4, 4, 0.25)  # 产生y数据
X, Y = np.meshgrid(X, Y)  # 生成网格
R = np.sqrt(X ** 2 + Y ** 2)  # 产生R数据
Z = np.sin(R)  # 产生Z数据

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
# 画图,设置横纵跨度和颜色
ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap='rainbow')
# 画等高线
ax.set_zlim3d(-2, 2)
# 限制Z坐标范围
plt.show()

在这里插入图片描述

3. 子图

import matplotlib.pyplot as plt  # 导入matplotlib库画图
plt.figure(1)

plt.subplot(2, 2, 1)  # 将图分为2*2,其为第1个
plt.plot([0, 1], [0, 1])

plt.subplot(2, 2, 2)  # 将图分为2*2,其为第2个
plt.plot([0, 1], [0, 2])

plt.subplot(2, 2, 3)  # 将图分为2*2,其为第3个
plt.plot([0, 1], [0, 3])

plt.subplot(2, 2, 4)  # 将图分为2*2,其为第4个
plt.plot([0, 1], [0, 4])

plt.figure(2)

plt.subplot(2, 1, 1)  # 将图分为2*1,其为第1个
plt.plot([0, 1], [0, 1])

plt.subplot(2, 3, 4)  # 将图分为2*3,其为第4个
plt.plot([0, 1], [0, 2])

plt.subplot(2, 3, 5)  # 将图分为2*3,其为第5个
plt.plot([0, 1], [0, 3])

plt.subplot(2, 3, 6)  # 将图分为2*3,其为第6个
plt.plot([0, 1], [0, 4])

plt.show()

在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜yuan~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值