0

I've been using Python/NumPy for a lot of things for a while now, but I am still confused about creating 3D plots.

In a "traditional" data analysis program (Origin, SigmaPlot, Excel...), if you want to make a 3D plot or a contour plot, you usually have your data in (X,Y,Z) format, that is, for each pair of X and Y you have one value of Z.

As opposed to this, all Python plotting guides I find use numpy.meshgrid for plotting -and I don't fully understand the connection to the traditional plotting software.

Let's say I have the following code:

axes_range = np.linspace(-5, 5, num=25)

alphas = []

for xcoord in axes_range:
    for ycoord in axes_range:
        alphas.append(f(xcoord,ycoord))
        

What's the best way of making a plot of (xcoord, ycoord, alphas)?

Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42
  • Possible duplicate [Simplest way to plot 3d surface given 3d points](https://stackoverflow.com/questions/12423601/simplest-way-to-plot-3d-surface-given-3d-points) – AJ Biffl May 19 '22 at 08:27
  • Does this answer your question? [Simplest way to plot 3d surface given 3d points](https://stackoverflow.com/questions/12423601/simplest-way-to-plot-3d-surface-given-3d-points) – AJ Biffl May 19 '22 at 08:28

1 Answers1

0

With matplotlib you simply need

import matplotlib.pylab as plt
import numpy as np
from matplotlib import cm

X, Y = np.meshgrid(xcoord, ycoord)
plt.contourf(X, Y, alphas.T, levels=20, cmap=cm.jet)
plt.show()

I think you need to transpose alphas as I do here.