簡體   English   中英

如何在Python中以相同比例繪制兩個圖形的兩個3D矩陣圖

[英]How to plot two 3D plots of matrices on the same figure with same scale in Python

我有兩個矩陣,我想在同一圖的兩個子圖上具有相同的兩個3D圖,並具有相同的z軸。

到目前為止,這是我的代碼:

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軸?

我認為您需要生成子圖

參見下圖(我也更改了顏色)

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軸的限制,請使用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM