pythonmatplotlibgridgnuplotwireframe

Plot Wireframe out of data not working


I try to write a script for matplotlib where data points are plotted as a wireframe. I did that in gnuplot once (which is working). Nevertheless I'm migrating some of my scripts to matplotlib because I'm more conviced of it by now.

I calculated some data in cylindrical coordinates. For fixed z's I calculated the x and y coordinates depending on the phi angle. When a full circle is drawn, the next z-step is calculcated etc. The data looks like this

  6.71570E+01  0.00000E+00  6.00000E+01  4.46028E+01
  6.70761E+01  3.29524E+00  6.00000E+01  4.46028E+01
  6.68336E+01  6.58254E+00  6.00000E+01  4.46028E+01
  6.64301E+01  9.85398E+00  6.00000E+01  4.46028E+01
  6.58666E+01  1.31017E+01  6.00000E+01  4.46028E+01
  6.51444E+01  1.63178E+01  6.00000E+01  4.46028E+01
  6.42653E+01  1.94947E+01  6.00000E+01  4.46028E+01
  6.32313E+01  2.26245E+01  6.00000E+01  4.46028E+01
  [...]
  6.68336E+01 -6.58254E+00  6.00000E+01  4.46028E+01
  6.70761E+01 -3.29524E+00  6.00000E+01  4.46028E+01
  6.71570E+01 -8.51512E-13  6.00000E+01  4.46028E+01

  6.34799E+01  0.00000E+00  5.70000E+01  4.21513E+01
  6.34035E+01  3.11481E+00  5.70000E+01  4.21513E+01
  6.31742E+01  6.22212E+00  5.70000E+01  4.21513E+01
  6.27928E+01  9.31444E+00  5.70000E+01  4.21513E+01
  6.22602E+01  1.23843E+01  5.70000E+01  4.21513E+01
  6.15775E+01  1.54244E+01  5.70000E+01  4.21513E+01
  6.07465E+01  1.84272E+01  5.70000E+01  4.21513E+01
  5.97691E+01  2.13857E+01  5.70000E+01  4.21513E+01
  5.86478E+01  2.42927E+01  5.70000E+01  4.21513E+01
  5.73852E+01  2.71412E+01  5.70000E+01  4.21513E+01
  5.59843E+01  2.99242E+01  5.70000E+01  4.21513E+01
  5.44485E+01  3.26352E+01  5.70000E+01  4.21513E+01
  5.27816E+01  3.52676E+01  5.70000E+01  4.21513E+01
  [...]

where blank lines are necessary to get gnuplot to plot the wireframe, described here (lowrank.net/gnuplot/datafile-e.html, see '3-dimensional data'). Simply calling 'splot' in gnuplot draws the correct wireframe of my data.

GnuPlot plot of my wireframe

When trying to plot it in matplotlib with the following code:

import matplotlib.pyplot as plt
import numpy as np

from mpl_toolkits.mplot3d import Axes3D

plt.close('all')
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')

file_in = open('wireframe_data.dat')
with file_in as file:
    data_mani = [[np.double(digit) for digit in line.split()] for line in file]
file_in.close()

data_mani = list(filter(None, data_mani)) # remove blank lines, fastest way
data_mani = np.array(data_mani, dtype='float64')

ax.plot_wireframe(data_mani[:, 0], data_mani[:, 1], data_mani[:, 2], rcount = 10, ccount = 10, linewidth=0.7)

It only draws the circles at fixed z coordinates and one line along the the circles in z-direction, i.e. not a fully wireframe. The result loooks like this

Matplotlib plot of my wireframe (different data, but same structure)

I'm trying to figure out how to plot it correctly in python and I cannot get it working with different meshgrid callings. At the moment I'm even unsure if I even need meshgrid, because the surface data is already there. Maybe somebody has an idea and can help me here? I'm really frustrated at the moment.

Looking forward to your answers.

Best wishes, TheOrangeman


Solution

  • Ok, I found a solution. I had to reshape my data the following way:

    [...]
    num_z = len(np.unique(data_mani[:, 2]))
    points_per_z = len(data_mani) / num_z
    
    XX = np.reshape(data_mani[:, 0], (num_z, points_per_z))
    YY = np.reshape(data_mani[:, 1], (num_z, points_per_z))
    ZZ = np.reshape(data_mani[:, 2], (num_z, points_per_z))
    
    ax.plot_wireframe(XX, YY, ZZ)
    

    Now it looks like this.