1

I'm working with a data structure analogus to:

x = [1.6,1.9,2.6,2.8,3.1,3.6,1.4,2.7]
y = [98.2,165.2,174.3,109.5,132.5,162.4,95.3,98.5]
z = [100.2,121.5,173.5,89.9,154.7,127.4,147.2,155.9]
m = [-0.2,-1.4,0.3,3.1,-4.2,-2.6,1.7,-1.9] 

Here, x,y,z are all independently variable and may be represented as m=f(x,y,z)
I'm trying to achieve a 4D surface plot(fitted to a polynomial aiming to generate an equation), with m representing the color gradient of the 3D surface.
Although a very nice example(representing z=f(x,y)) is provided: https://stackoverflow.com/a/18648210/97160.

But I'm getting nowhere on how perform such operation with matplotlib in 4D, where m=f(x.y,z).

A little help or suggestions regarding this concern would be highly appreciated.

Community
  • 1
  • 1
diffracteD
  • 758
  • 3
  • 10
  • 32

1 Answers1

2

You are asking two different questions:

  1. how to make the fitting of your data; and
  2. how to visualize the function obtained from the fitting process.

For the first part you can do a fit using some of the built-in functions like curve-fit or minimize from scipy.

For example

import numpy as np
from scipy.optimize import minimize

x = np.array([1.6, 1.9, 2.6, 2.8, 3.1, 3.6, 1.4, 2.7])
y = np.array([98.2, 165.2, 174.3, 109.5, 132.5, 162.4, 95.3, 98.5])
z = np.array([100.2, 121.5, 173.5, 89.9, 154.7, 127.4, 147.2, 155.9])
m = np.array([-0.2, -1.4, 0.3, 3.1, -4.2, -2.6, 1.7, -1.9])

def func(coef, x, y, z, m):
    a = coef[0]
    b = coef[1]
    c = coef[2]
    return np.linalg.norm(m - a*x**2 + b*y**2 + c*z**2)

res = minimize(func, [1, 1, 1], args=(x, y, z, m))
a, b, c = res.x

You will need to define your desired model and fit it.

For the second part, you are looking for scalar visualization over a 3D domain, I think that matplotlib is not the best option. I would use Mayavi instead.

Following an example

import numpy as np
from mayavi import mlab

x, y, z = np.mgrid[-5:5:65j, -5:5:65j, -5:5:65j]
scalars = x * x * 0.5 + y * y + z * z * 2.0

cont = np.linspace(np.min(scalars), np.max(scalars), 8)
mlab.contour3d(x, y, z, scalars, contours=list(cont), colormap='YlGnBu')
mlab.show()

That gives some contours over the domain. You can also try to use a volume render technique.

enter image description here

nicoguaro
  • 3,629
  • 1
  • 32
  • 57
  • It's really nice. But I'm actually searching for a fitted 4d `surface/wireframe` model. Any suggestions on how should I choose a model ? In case of 3d(excluding `z`), I tried a quadratic surface model. But need some serious references regarding 4d. – diffracteD Aug 04 '15 at 17:02
  • That's not written in your question nor Matplotlib should be a tag then. You are interested in how to define the model or use something like symbolic regression. Maybe even the question is off-topic here and should be asked in Computational Science StackExchange. – nicoguaro Aug 04 '15 at 17:04
  • I actually need to fit the data in a surface, as you explained. But I was asking few references in the context of choosing a good model. – diffracteD Aug 04 '15 at 17:15
  • I understand that, and that does not sound as a question for StackOverflow. – nicoguaro Aug 04 '15 at 17:17
  • `scalars = x * x * 0.5 + y * y + z * z * 2.0` is it just an example equation you've given here. I was trying to prefer a polynomial-fit instead. Although I got few understanding issue, but answer is much appreciated. – diffracteD Aug 04 '15 at 17:34
  • That's the visualization part. For the fitting part I used this equation `a*x**2 + b*y**2 + c*z**2`, that is, indeed, a polynomial of `(x, y, z)`. You can just add more parameters and get something like ``c_{3i-2) + 1}x^i + c_{3i - 1}y^i + c_{3i}z^i`` or another one that includes cross terms... it's your choice. – nicoguaro Aug 04 '15 at 17:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85163/discussion-between-diffracted-and-nicoguaro). – diffracteD Aug 05 '15 at 03:50
  • from this code how can I be able to print a fitted equation ? – diffracteD Sep 05 '15 at 09:42