用matplotlib 做出的第一张3d图

matplotlib是一个绘图库,它可以绘制3d和2d的图,今天,我就带着大家用matplotlib绘制第一张3d图。

我们今天就画一个csdn的立体文本吧!

用matplotlib 做出的第一张3d图_第1张图片

一、安装matplotlib

如果你还没有安装matplotlib,就跟着教程安装一下吧。

1.使用快捷键Windows+R打开“运行”程序

用matplotlib 做出的第一张3d图_第2张图片

2.输入"cmd",按"Enter"或"确定".

 用matplotlib 做出的第一张3d图_第3张图片

 3.在打开的命令行里输入语句"pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple"(加速)

二、基础用法

在安装matplotlib之后,自动安装有 mpl_toolkits.mplot3d。

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['font.family'] = ['Microsoft YaHei']
fig = plt.figure()  # 创建3D对象
ax = plt.axes(projection="3d")

ax.set_xlabel('X 轴')
ax.set_ylabel('Y 轴')
ax.set_zlabel('Z 轴')
plt.show()

用matplotlib 做出的第一张3d图_第4张图片

三、生成方块函数

我们写一个block函数,参数为x,y,z,功能是在坐标为x,y,z的位置生成一个方块。

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['font.family'] = ['Microsoft YaHei']
fig = plt.figure()  # 创建3D对象
ax = plt.axes(projection="3d")


def block(x, y, z):
    """
    在坐标为x,y,z的位置生成一个方块
    :param x: 
    :param y: 
    :param z: 
    """
    dx = dy = dz = 1
    xx = np.linspace(x, x + dx, 2)

    yy = np.linspace(y, y + dy, 2)

    zz = np.linspace(z, z + dz, 2)

    xx2, yy2 = np.meshgrid(xx, yy)

    ax.plot_surface(xx2, yy2, np.full_like(xx2, z))

    ax.plot_surface(xx2, yy2, np.full_like(xx2, z + dz))

    yy2, zz2 = np.meshgrid(yy, zz)

    ax.plot_surface(np.full_like(yy2, x), yy2, zz2)

    ax.plot_surface(np.full_like(yy2, x + dx), yy2, zz2)

    xx2, zz2 = np.meshgrid(xx, zz)

    ax.plot_surface(xx2, np.full_like(yy2, y), zz2)

    ax.plot_surface(xx2, np.full_like(yy2, y + dy), zz2)

四、生成立体字

我们要生成"CSDN"的立体字,想来列一张表,看看哪些地方需要放方块,哪些地方不需要放方块:

用matplotlib 做出的第一张3d图_第5张图片

想在我们再在python中创建一个列表,来储存这些值:

blocks = [[1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1],
          [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1],
          [1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1],
          [1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1],
          [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1]]

再遍历一下列表:

blocks = [[1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1],
          [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1],
          [1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1],
          [1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1],
          [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1]]
for i in range(5):
    for j in range(16):
        #  遍历每一个位置
        if blocks[i][j] == 1:  # 如果这个位置需要放方块
            block(j, 0, 5-i)  # 这里需要翻过来

大功告成!

五、完整代码

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['font.family'] = ['Microsoft YaHei']
fig = plt.figure()  # 创建3D对象
ax = plt.axes(projection="3d")


def block(x, y, z):
    """
    在坐标为x,y,z的位置生成一个方块
    :param x:
    :param y:
    :param z:
    """
    dx = dy = dz = 1
    xx = np.linspace(x, x + dx, 2)

    yy = np.linspace(y, y + dy, 2)

    zz = np.linspace(z, z + dz, 2)

    xx2, yy2 = np.meshgrid(xx, yy)

    ax.plot_surface(xx2, yy2, np.full_like(xx2, z))

    ax.plot_surface(xx2, yy2, np.full_like(xx2, z + dz))

    yy2, zz2 = np.meshgrid(yy, zz)

    ax.plot_surface(np.full_like(yy2, x), yy2, zz2)

    ax.plot_surface(np.full_like(yy2, x + dx), yy2, zz2)

    xx2, zz2 = np.meshgrid(xx, zz)

    ax.plot_surface(xx2, np.full_like(yy2, y), zz2)

    ax.plot_surface(xx2, np.full_like(yy2, y + dy), zz2)


blocks = [[1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1],
          [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1],
          [1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1],
          [1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1],
          [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1]]
for i in range(5):
    for j in range(16):
        #  遍历每一个位置
        if blocks[i][j] == 1:  # 如果这个位置需要放方块
            block(j, 0, 5-i)  # 这里需要翻过来


ax.set_xlabel('X 轴')
ax.set_ylabel('Y 轴')
ax.set_zlabel('Z 轴')
plt.show()

用matplotlib 做出的第一张3d图_第6张图片

也可以去掉网格:

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['font.family'] = ['Microsoft YaHei']
fig = plt.figure()  # 创建3D对象
ax = plt.axes(projection="3d")


def block(x, y, z):
    """
    在坐标为x,y,z的位置生成一个方块
    :param x:
    :param y:
    :param z:
    """
    dx = dy = dz = 1
    xx = np.linspace(x, x + dx, 2)

    yy = np.linspace(y, y + dy, 2)

    zz = np.linspace(z, z + dz, 2)

    xx2, yy2 = np.meshgrid(xx, yy)

    ax.plot_surface(xx2, yy2, np.full_like(xx2, z))

    ax.plot_surface(xx2, yy2, np.full_like(xx2, z + dz))

    yy2, zz2 = np.meshgrid(yy, zz)

    ax.plot_surface(np.full_like(yy2, x), yy2, zz2)

    ax.plot_surface(np.full_like(yy2, x + dx), yy2, zz2)

    xx2, zz2 = np.meshgrid(xx, zz)

    ax.plot_surface(xx2, np.full_like(yy2, y), zz2)

    ax.plot_surface(xx2, np.full_like(yy2, y + dy), zz2)


blocks = [[1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1],
          [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1],
          [1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1],
          [1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1],
          [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1]]
for i in range(5):
    for j in range(16):
        #  遍历每一个位置
        if blocks[i][j] == 1:  # 如果这个位置需要放方块
            block(j, 0, 5-i)  # 这里需要翻过来


plt.axis('off')
plt.show()

 用matplotlib 做出的第一张3d图_第7张图片

蟹蟹大家的浏览! 

你可能感兴趣的:(3d,matplotlib,python,numpy)