2

I created a matplotlib 3D graph with ax = plt.axes(projection = '3d') and ax.plot_surface(...), now I need to view the graph from top and get a 2D cross section image.

description

I've searched a lot about creating cross section in matplotlib and here're some similar questions 1 2. But I'm not quite familiar with matplotlib so I still failed to adapt them into my code. Could you provide some tips?

Original code posted here:

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

ecGlobalRedius = 0.25

def getElliptic(redius = 1, height = 5, offset = (0, 0, 0), devision = 20, xExtendRate = 1, yExtendRate = 1):
    theta = np.linspace(0, 2*np.pi, devision)
    cz = np.array([-0.2, height])
    cx = np.array([xExtendRate * redius * np.cos(theta)])
    cx, cz = np.meshgrid(cx, cz)
    cy = np.array([yExtendRate * redius * np.sin(theta)] * 2)
    return offset[0] + cx, offset[1] + cy, offset[2] + cz

def newCy(ax, pos, height, xExtendRate, yExtendRate):
    global ecGlobalRedius
    cx, cy, cz = getElliptic(offset = (pos[0],pos[1],0), devision = 40,\
        height = height, redius = ecGlobalRedius, xExtendRate = xExtendRate, yExtendRate = yExtendRate)
    ax.plot_surface(cx, cy, cz, rstride = 1, cstride = 1,\
        linewidth = 0, alpha = 0.5)

def drawCylinder(CyList):
    fig = plt.figure(figsize = (11, 10))
    ax = plt.axes(projection = '3d')
    for pos, hei, extX, extY in CyList:
        newCy(ax, pos, hei, extX, extY)
    ax.set_xlim(-5, 5)
    ax.set_ylim(-5, 5)
    ax.set_zlim(0, 10)
    plt.show()

if __name__ == '__main__':
    ecNum = int(input('Elliptic num ? '))
    randGen = lambda : random.uniform(-5, 5)
    randGenH = lambda : random.uniform(0, 10)
    randGenR = lambda : random.gauss(0.8, 1) * 2 + 0.1
    ecList = [((randGen(), randGen()), randGenH(), randGenR(), randGenR()) for i in range(ecNum)]
    drawCylinder(ecList)

Thanks a lot for your help.

recolic
  • 554
  • 4
  • 18

0 Answers0