python三维坐标轴单位长度_在三维坐标轴上绘制二维图像

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure()

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

# Plot a sin curve using the x and y axes.

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='curve in (x,y)')

# Plot scatterplot data (20 2D points per colour) on the x and z axes.

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

# Fixing random state for reproducibility

np.random.seed(19680801)

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

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

c_list = []

for c in colors:

c_list.extend([c] * 20)

# By using zdir='y', the y value of these points is fixed to the zs value 0

# and the (x,y) points are plotted on the x and z axes.

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

# Make legend, set axes limits and labels

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')

# Customize the view angle so it's easier to see that the scatter points lie

# on the plane y=0

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

plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将二维图片绕Z轴旋转30度并在三维坐标中显示,您可以使用Python的matplotlib和numpy库。以下是一个示例代码: ``` import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # 读取图片并将其转换为numpy数组 img = plt.imread('image.png') data = np.array(img) # 定义旋转矩阵 theta = np.radians(30) c, s = np.cos(theta), np.sin(theta) rotate_matrix = np.array([[c, -s, 0], [s, c, 0], [0, 0, 1]]) # 将二维图像坐标转换为三维坐标 x, y = np.meshgrid(np.arange(data.shape[1]), np.arange(data.shape[0])) z = np.zeros_like(x) coordinates = np.stack((x, y, z), axis=-1) rotated_coordinates = np.matmul(coordinates, rotate_matrix) # 创建三维坐标系并将旋转后的图像绘制到其中 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(rotated_coordinates[..., 0], rotated_coordinates[..., 1], rotated_coordinates[..., 2], facecolors=data/255) plt.show() ``` 在这个示例代码中,我们首先读取了一张名为“image.png”的图片,并将其转换为numpy数组。然后,我们定义了一个旋转矩阵,用于将二维图像坐标转换为三维坐标,并将其绕z轴旋转30度。接下来,我们使用numpy的meshgrid函数创建二维网格,并将其与一个全零的数组组合成三维坐标。然后,我们将这些坐标与旋转矩阵相乘,得到旋转后的坐标。最后,我们使用matplotlib的plot_surface函数创建一个三维坐标系,并将旋转后的图像绘制到其中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值