数据可视化chapter07_大数据2003_02

matplotlib绘制Axes3D的两种方法:

  1. Axes3D(fig,rect=None)
  2. 该方法的参数所属画布,rect表示确定三维坐标系为值的元组
  3. 创建方式
  4. add_subplot()
  5. 添加绘图区域是传入projection=’3d’
  6. 创建方式

代码示例

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Axes3D()
fig = plt.figure()
ax = Axes3D(fig)
add_subplot()
ax1 = fig.add_subplot(111,projection='3d')

绘制3D线框图

  • plot_wireframe(x,y,z)
  • 其他参数
  • rcount,ccount:表示每个坐标轴每个坐标轴方向所使用的最大样本量,默认50
  • rstride,cstride:表示采样密度
  • 注意:以上两个参数互斥,不能同时使用

代码示例

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride = 10, cstride = 10)
plt.show()

数据可视化chapter07_大数据2003_02

绘制3D曲面图

  • plot_surface(x,y,z,)
  • rcount,ccount:表示每个坐标轴每个坐标轴方向所使用的最大样本量,默认50
  • rstride,cstride:表示采样密度
  • color:曲面颜色
  • cmap:表示曲面映射表
  • shade:表示是否对曲面进行着色

代码示例

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm
import numpy as np
x1 = np.arange(-5, 5, 0.25)
y1 = np.arange(-5, 5, 0.25)
x1, y1 = np.meshgrid(x1, y1)
r1 = np.sqrt(x1**2 + y1 **2)
z1 = np.sin(r1)
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')

ax.plot_surface(x1,y1,z1,cmap=cm.coolwarm, linewidth=0,antialiased=False)
ax.set_zlim(-1.01, 1.01)
plt.show()

数据可视化chapter07_大数据2003_02

animation绘制动图

  1. FuncAnimation类:通过重复调用一个函数来制作动画
  2. FuncAnimation()
  3. 所含参数:*较为重要
  4. fig:表时动画所在的画布
  5. func:表示每帧动画所调用的函数
  6. frames:表示动画的长度(一次动画包含的帧数)
  7. Init_func:表示开始绘制帧的函数,绘制第一帧前被调用
  8. interval:动画更新的频率,毫秒为单位,默认200
  9. blit:是否更新所有点,官方推荐True,但MacOS推荐False否则无法显示动画

代码示例:此处无法显示图像,可以自行运行查看

%matplotlib qt5
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
x = np.arange(0, 2*np.pi, 0.01)
fig, ax = plt.subplots()
line, = ax.plot(x, np.sin(x))
def animate(i):
    line.set_ydata(np.sin(x + i/10.0))
    return line
def init():
    line.set_ydata(np.sin(x))
    return line
ani = FuncAnimation(fig = fig, func = animate, frames = 100, init_func=init, interval=20, blit=False)
plt.show()
  1. ArtistAnimation类:是基于一组Artist随想的动画类,通过一帧一帧的数据制作动画
  2. ArtistAnimation()
  3. 参数如下:
  4. fig:所在的画布
  5. artist:一组Artist对象
  6. interval:动画更新的频率,毫秒为单位,默认200
  7. repeat_delay:表示再次播放动画之前延迟的时长
  8. repeat:表示是否重复播放动画

代码示例:此处无法显示图像,可以自行运行查看

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation
x = np.arange(0, 2*np.pi, 0.01)
fig, ax = plt.subplots()
arr = []
for i in range(5):
    line = ax.plot(x, np.sin(x + i))
    arr.append(line)
ani = ArtistAnimation(fig = fig, artists= arr)
plt.show()

Original: https://blog.csdn.net/up_up_up_up_/article/details/123959865
Author: up_up_up_up_
Title: 数据可视化chapter07_大数据2003_02

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/767670/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球