0

I am trying to plot several 2D panels in order to form three boxes using Python but am having some trouble. What I have right now uses Matplotlib, but I'm open to using something else if it's easier.

Basically this is a MWE of what I got:

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

L = 400
dx=2*4
dz=1*4

# create the figure
fig, axes = plt.subplots(ncols=1, nrows=1, figsize=(16, 8), subplot_kw=dict(projection='3d', aspect="equal"))

ax=axes

for i in range(3):
    x, y, z = np.arange(dx/2, L, dx)+i*5*L/4, np.arange(dx/2, L, dx), np.arange(-240, dz, dz)
    xx, yy = np.meshgrid(x, y)
    A=np.random.rand(*xx.shape)

    #----
    # Plot surface
    X = xx
    Y = yy
    Z = np.zeros_like(xx)

    ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=plt.cm.viridis(A), shade=False)
    #----

    #----
    # Plot x-z cross-section
    xx, zz = np.meshgrid(x, z)
    Y=np.zeros_like(xx)
    X=xx
    Z=zz

    B=np.random.rand(*xx.shape)*2
    ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=plt.cm.viridis(B), shade=False)
    #----

    #----
    # Plot x-z cross-section
    yy, zz = np.meshgrid(y, z)
    X=np.zeros_like(xx)+xx.max()
    Y=yy
    Z=zz

    B=np.random.rand(*xx.shape)/2
    ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=plt.cm.viridis(B), shade=False)
    #----

#ax.set_axis_off()
# make the panes transparent
ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# make the grid lines transparent
ax.xaxis._axinfo["grid"]['color'] =  (1,1,1,0)
ax.yaxis._axinfo["grid"]['color'] =  (1,1,1,0)
ax.zaxis._axinfo["grid"]['color'] =  (1,1,1,0)

This code is based on this answer and it produces this:

enter image description here

Two things are wrong with this picture:

  • the aspect ratio is off
  • the boxes are always too small compared to the size of the picture

I think I can somewhat take care of the aspect ratio problem with some hacks, even though it would be nice to deal with it in a proper way. However, I have tried like crazy to zoom in the picture without success.

Again, I'm willing to use something other than pyplot, although, since I'm planing on using xarray, it would be nice to have something that blends in nicely with it.

TomCho
  • 3,204
  • 6
  • 32
  • 83
  • For 3d plotting at best stop using Matplotlib immediately. It is not made for that. [Mayavi](https://docs.enthought.com/mayavi/mayavi/) is the best tool for that. If you are on Windows get the packages from [Gohlke](https://www.lfd.uci.edu/~gohlke/pythonlibs/#mayavi). – Joe Aug 16 '18 at 06:25
  • @Joe I'm on Linux. I'd be glad to use Mayavi, but honestly I couldn't find a comprehensive example on that. I've never used it before and I have to do this plot soon, so I can't spend some days completely learning a new tool before using it – TomCho Aug 16 '18 at 14:36
  • Don't worry. Mayavi is very easy to use and there are a lot of examples. About installation on Linux, read [this](https://pypi.org/project/mayavi/) or [this](https://docs.enthought.com/mayavi/mayavi/installation.html). Are you using a distribution like Anaconda or Enthought? – Joe Aug 16 '18 at 16:39
  • @Joe Yes, I've read those. But I always run into issues installing Mayavi. Currently, this is my problem: https://stackoverflow.com/q/51880687/4506929 – TomCho Aug 16 '18 at 17:23

0 Answers0