如何在 Python 中以相同比例在同一图形上绘制矩阵的两个 3D 图 [英] How to plot two 3D plots of matrices on the same figure with same scale in Python

查看:36
本文介绍了如何在 Python 中以相同比例在同一图形上绘制矩阵的两个 3D 图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个矩阵,我想在同一个图上的两个子图上有它们对应的两个 3D 图,具有相同的 z 轴.

I have two matrices and I would like to have their corresponding two 3D plots on two subplots on the same figure, with the same z axis.

这是我目前的代码:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D

def myplot(matrix1, matrix2):
    mymin = np.min(np.array([np.min(matrix1), np.min(matrix2)]))
    mymax = np.max(np.array([np.max(matrix1), np.max(matrix2)]))

    xsize, ysize = matrix1.shape
    x = np.arange(0, ysize, 1)
    y = np.arange(0, xsize, 1)

    xs, ys = np.meshgrid(x, y)
    z1 = matrix1
    z2 = matrix2

    fig, (ax1, ax2) = plt.subplots(1, 2)
    ax1 = Axes3D(fig)
    ax1.plot_surface(xs, ys, z1, rstride=1, cstride=1)
    ax2 = Axes3D(fig)
    ax2.plot_surface(xs, ys, z2, rstride=1, cstride=1)
    plt.tight_layout
    plt.show()

mat1 = np.random.random(size = (10, 10))
mat2 = np.random.random(size = (10, 10))

myplot(mat1, mat2)

  • 为什么我只能看到一个 3D 绘图?
  • 如何在两个图中使用相同的 z 轴?
  • 推荐答案

    我认为你需要生成子图

    I think you need to generate the sub plots

    见下图(我也改变了颜色)

    See plot below (I've changed the colouring too)

    def myplot(matrix1, matrix2):
        mymin = np.min(np.array([np.min(matrix1), np.min(matrix2)]))
        mymax = np.max(np.array([np.max(matrix1), np.max(matrix2)]))
    
        xsize, ysize = matrix1.shape
        x = np.arange(0, ysize, 1)
        y = np.arange(0, xsize, 1)
    
        xs, ys = np.meshgrid(x, y)
        z1 = matrix1
        z2 = matrix2
        fig=plt.figure()
        ax1 = fig.add_subplot(2, 1, 1, projection='3d')
        ax1.plot_surface(xs, ys, z1,color="blue",alpha=0.5,rstride=1, cstride=1)
        ax2 = fig.add_subplot(2, 1, 2, projection='3d')
        ax2.plot_surface(xs, ys, z2,color="green",alpha=0.5, rstride=1, cstride=1)
        plt.tight_layout
        plt.show()
    
    mat1 = np.random.random(size = (10, 10))
    mat2 = np.random.random(size = (10, 10))
    
    myplot(mat1, mat2)
    

    要将 myminmymax 作为两个 z 轴的限制,请使用

    To impose mymin and mymax as limits of the two z axes, use

    ax1.set_zlim(mymin, mymax)
    ax2.set_zlim(mymin, mymax)
    

    这篇关于如何在 Python 中以相同比例在同一图形上绘制矩阵的两个 3D 图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆