【PLT】3D Plot

plot 2D data on 3D plot

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')

# sin curve on X-Y plane
x = np.linspace(0, 1, 100)
y = np.sin(x*2*np.pi)/2+0.5
ax.plot(x, y, zs=0, zdir='z', label='sin curve')

colors = ('r', 'g', 'b', 'k') # all colors

np.random.seed(10)

x = np.random.sample(20*len(colors))
y = np.random.sample(20*len(colors))

c_lst = []
for c in colors:
    c_lst.extend([c]*20)

ax.scatter(x, y, zs=0, zdir='y', c=c_lst, label='points in (x, z)')

ax.legend()
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

ax.view_init(elev=20, azim=-35)
plt.show()

plot

3D bar charts

def bar_charts_3D():
    fig = plt.figure(figsize=(8, 3))
    ax1 = fig.add_subplot(121, projection='3d')
    ax2 = fig.add_subplot(122, projection='3d')

    # fake data
    _x, _y = np.arange(4), np.arange(5)
    _xx, _yy = np.meshgrid(_x, _y)
    x, y = _xx.ravel(), _yy.ravel()

    top = x+y
    bottom = np.zeros_like(top)
    width = depth = 1
    ax1.bar3d(x, y, bottom, width, depth, top, shade=True)
    ax1.set_title('Shaded')

    ax2.bar3d(x, y, bottom, width, depth, top, shade=False)
    ax2.set_title('No Shaded')

    plt.show()

Shade

2D bar garphs in different planes

def graphics_in_planes():
    np.random.seed(10)
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    colors = ['r', 'g', 'b', 'y']
    yticks = [3, 2, 1, 0]

    for (c, k) in zip(colors, yticks):
        xs = np.arange(20)
        ys = np.random.rand(20)

        cs = [c]*len(xs)
        cs[0] = 'c'

        ax.bar(xs, ys, zs=k, zdir='y', color=cs, alpha=0.8)

    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')

    ax.set_yticks(yticks)

    plt.show()

Plot

猜你喜欢

转载自blog.csdn.net/qq_18822147/article/details/114067221
今日推荐