python绘图matplotlib——使用记录2

本博文来自于网络收集,如有侵权请联系删除

1 三维散点图

import matplotlib.pyplot as plt
import numpy as np

from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
# ax = fig.gca(projection="3d")
ax = fig.add_axes(Axes3D(fig))

xs = np.random.rand(50)*10
ys = np.random.rand(50)*10+20
zs1 = np.random.rand(50)*10
zs2 = np.sqrt(xs**2+ys**2)

ax.scatter(xs, ys, zs=zs1, zdir="z", c="c", marker="o", s=30)
ax.scatter(xs, ys, zs=zs2, zdir="z", c="purple", marker="<", s=30)

ax.set(xlabel="X", ylabel="Y", zlabel="Z")

plt.show()

在这里插入图片描述

2 三维柱状图

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection="3d")

colorsList = ["r", "b", "y"]
yLayersList = [2, 1, 0]

for color, layer in zip(colorsList, yLayersList):
    x = np.arange(10)
    y = np.random.rand(10)
    ax.bar(x, y, zs=layer, zdir="y", color=color, alpha=.7)

ax.set(xlabel="X", ylabel="Y", zlabel="Z", yticks=yLayersList)

plt.show()

在这里插入图片描述

三维曲面

import matplotlib.pyplot as plt
import numpy as np

from matplotlib import cm
from matplotlib.ticker import LinearLocator,FormatStrFormatter

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection="3d")

x = np.arange(-3, 3, 0.25)
y = np.arange(-3, 3, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(np.power(x, 2) + np.power(y, 2))
z = np.sin(r)

surf = ax.plot_surface(x, y, z,
                       rstride=1,
                       cstride=1,
                       cmap=cm.coolwarm,
                       linewidth=0,
                       antialiased=False)

ax.set(zlim=(-1, 1))
ax.zaxis.set_major_locator(LinearLocator(7))
ax.zaxis.set_major_formatter(FormatStrFormatter("%3.2f"))

fig.colorbar(surf, shrink=0.6, aspect=10)

plt.show()

在这里插入图片描述

相关推荐

  1. 使用Matplotlib进行Python绘图

    2024-03-25 13:04:05       11 阅读
  2. Python Matplotlib 实现基础绘图

    2024-03-25 13:04:05       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-25 13:04:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-25 13:04:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-25 13:04:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-25 13:04:05       20 阅读

热门阅读

  1. 大门的选取

    2024-03-25 13:04:05       17 阅读
  2. List与String相互转化的几种方式

    2024-03-25 13:04:05       18 阅读
  3. 45. 跳跃游戏 II

    2024-03-25 13:04:05       18 阅读
  4. ARIMA

    ARIMA

    2024-03-25 13:04:05      15 阅读
  5. 自动驾驶技术中大模型的应用与挑战分析

    2024-03-25 13:04:05       21 阅读