2

This question is related to this

I want to plot the following 4 column data

X  Y  Z  V(X,Y,Z)
1  1  1  1.1
1  1  2  1.0
1  1  3  1.0
1  2  1  0.9
1  2  2  1
1  2  3  1.8
1  3  1  0.9
1  3  2  1
1  3  3  1.1

2  1  1  1.1
2  1  2  1.0
2  1  3  1.0
2  2  1  0.5
2  2  2  1.7
2  2  3  1.2
2  3  1  0.9
2  3  2  1
2  3  3  1.1

3  1  1  1.3
3  1  2  1.0
3  1  3  1.2
3  2  1  0.9
3  2  2  1
3  2  3  1.1
3  3  1  0.8
3  3  2  1.2
3  3  3  1.4

So, basically I want to plot a figure with 3 axes and the values of the function with colors. For, plotting {X,Y,Z(X,Y)} there are plenty of examples,for instance. Being not a programmer, I have tired to extend this example to create a meshgrid(X,Y,Z).(I am not sure whether this even make any sense or not!) with no sucess. Most of the question related to 4D plot in python is either unanswered or not suitable for my requirement. Please help.

N.B. This is what I would like to generate(generated in Mathematica)enter image description here

And this is what I have tried to do in python(!)

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
from scipy.interpolate import griddata

data = np.loadtxt('mydata.dat')

X = data[:,0]
Y = data[:,1]
Z = data[:,2]
val= data[:3]


xi = np.linspace(X.min(), X.max(), 3)


yi = np.linspace(Y.min(), Y.max(), 3)

zi = np.linspace(Y.min(), Y.max(), 3)

vali = griddata((X,Y,Z), val, (xi[None,:], yi[:,None], zi[:,None]))



fig = plt.figure(figsize=plt.figaspect(0.5))
ax = Axes3D(fig)


xig, yig, zig = np.meshgrid(xi, yi, zi)




 surf = ax.plot_surface(xig, yig, zig, vali,cmap=cm.jet, linewidth=0, 
 antialiased=False, shade=True,alpha=1.0)
Archimedes
  • 365
  • 3
  • 15
  • I think you want a scatter plot. – Julien Feb 15 '18 at 06:36
  • I have tired to make a `scatter(x, y, z, c=c) ` kind of thing given in the first example, however, for this type of grid data, couldn't manage to do. – Archimedes Feb 15 '18 at 06:47
  • Could you please post the code that you tried, the result that you got and then explain what you want to be different? Otherwise there is going to be a lot of guessing before you finally get what you are after. – Thomas Kühn Feb 15 '18 at 07:13
  • I have edited accordingly. – Archimedes Feb 15 '18 at 07:23
  • 1
    It is in general hard to visualize 4D data on a 3D plot. Visualizing 3D data on a 2D plot is rather easy because you only have a single layer and can use color to encode the third dimension. With 4D data on a 3D plot this works for a subset of the whole space, i.e. a surface plot as shown in many examples. What you are asking for here is to fill the entire 3D space with data points. But that would lead to most points being hidden by others. – ImportanceOfBeingErnest Feb 15 '18 at 09:28
  • @ImportanceOfBeingErnest, I agree that visualizing a 4D data on 3D plot is more flashy than imformative! But I am just trying to check this possibility on python-matplotlib. – Archimedes Feb 15 '18 at 11:05
  • The trick used by the mathematica is to use partial transparency to allow for a kind of see-through effect. This is not possible with matplotlib. Other options of visualization could be used instead, e.g. using voxels, or several surface plots or several contour plots or a scatter plot. – ImportanceOfBeingErnest Feb 15 '18 at 11:23
  • With a broken heart, I have to admit that scatter now seems to be a good option in matplotlib. I hope there are better alternaives in python one day I will stuble upon! – Archimedes Feb 15 '18 at 11:44
  • Note that I'm talking about matplotlib here, there might be other python libraries like mayavi, which will allow to produce such plots. – ImportanceOfBeingErnest Feb 15 '18 at 12:07
  • I am trying to install mayaVi with anaconda. So far, without any success! – Archimedes Feb 15 '18 at 12:21

1 Answers1

0

Matplotlib is not very good in plotting 3d graphs.

You could give mayavi a try, it is a visualisation software specialised on 3d plotting, which can also colour-code a forth dimension.

Here is an example galery. Maybe the atomic orbital example is what you want. It encodes the phase as colours on 3 dimensional surfaces.

LSchueler
  • 1,414
  • 12
  • 23
  • I have tried installing mayavi within anaconda, but it's not installing! So, right now I am not able to try mayavi. – Archimedes Feb 15 '18 at 11:06
  • @Archimedes I have never used anaconda, I'm working on a Linux system and I never had problems using pip for installation. Sorry, I can't help you there. – LSchueler Feb 15 '18 at 14:50
  • After a little struggle, I have installed mayaVi with anaconda in ubuntu. As this is the first time I am trying mayaVi, I have not been able to plot it yet. Can you please show how to plot with mayaVi in the above example? – Archimedes Feb 15 '18 at 15:42