2

I have many measurements of several quantities in an array, like this:

m = array([[2, 1, 3, 2, 1, 4, 2], # measurements for quantity A
[8, 7, 6, 7, 5, 6, 8], # measurements for quantity B
[0, 1, 2, 0, 3, 2, 1], # measurements for quantity C
[5, 6, 7, 5, 6, 5, 7]] # measurements for quantity D
)

The quantities are correlated and I need to plot various contour plots. Like "contours of B vs. D x A".

It is true that in the general case the functions might be not well defined -- for example in the above data, columns 0 and 3 show that for the same (D=5,A=2) point there are two distinct values for B (B=8 and B=7). But still, for some combinations I know there is a functional dependence, which I need plotted.

The contour() function from MatPlotLib expects three arrays: X and Y can be 1D arrays, and Z has to be a 2D array with corresponding values. How should I prepare/extract these arrays from m?

1 Answer 1

2

You will probably want to use something like scipy.interpolate.griddata to prepare your Z arrays. This will interpolate your data to a regularly spaced 2D array, given your input X and Y, and a set of sorted, regularly spaced X and Y arrays which you will need for eventual plotting. For example, if X and Y contain data points between 1 and 10, then you need to construct a set of new X and Y with a step size that makes sense for your data, e.g.

Xout = numpy.linspace(1,10,10)
Yout = numpy.linspace(1,10,10)

To turn your Xout and Yout arrays into 2D arrays you can use numpy.meshgrid, e.g.

Xout_2d, Yout_2d = numpy.meshgrid(Xout,Yout)

Then you can use those new regularly spaced arrays to construct your interpolated Z array that you can use for plotting, e.g.

Zout = scipy.interpolate.griddata((X,Y),Z,(Xout_2d,Yout_2d))

This interpolated 2D Zout should be usable for a contour plot with Xout_2d and Yout_2d.

Extracting your arrays from m is simple, you just do something like this:

A, B, C, D = (row for row in m)
Sign up to request clarification or add additional context in comments.

1 Comment

@Amenhotep I expanded my original answer to give a simple example on how meshgrid and griddata work to give you a new Z that you can use for contour plotting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.