0
$\begingroup$

I am trying to mimic the python matplotlib contourf plot functionality in Mathematica. This is for discrete data, so ListContourPlot is appropriate. In python, the contour plot interface is: contourf(X, Y, Z) where X, Y and Z can be 2D with the same shape as Z. (Meshgrid is used in this case). Alternatively, and how I typically use it, X and Y are 1D, and the number of rows of Z equals the dimensions of Y, and the number of columns of Z equals the dimensions of X. Mathematica ListContourPlot takes two input forms:

{{f11, ..., f1n}, ..., {fm1, ..., fmn}}

and

{{x1, y1, f1}, ..., {xk, yk, fk}}

The first form is more general: X and Y can have different dimensions. In the second form, X and Y must have the same number of elements.

Focusing on the more general form, the problem with the Mathematica interface is that it's not clear how to set axes limits that are tied to the numerical quantities in X and Y. Since the input just a table of numbers, the axes limits in Mathematica are just defined by number of rows and number of columns in the table. In contrast, in the more general matplotlib interface, X and Y are specified separately from Z, so physical units can be associated with contour plot axes. Using matplotlib, I can limit the axes to particular ranges in the x and y directions, with units appropriate to the quantities in X and Y. By contrast, in Mathematica, I can only label the contour plot axes with row and column numbers, if I use the first interface. I could use the second interface to have physical units for the x and y axes, but that also requires the same number of elements in X and Y, which is not always the case.

Presumably this can be solved with some sort of transformation of the axes, but I don't know how to do that. It seems rather advanced.

Thanks for any help!

$\endgroup$
2
  • 3
    $\begingroup$ I think you're looking for the DataRange option. $\endgroup$
    – lericr
    Feb 5, 2023 at 7:41
  • 2
    $\begingroup$ And perhaps the checkmark sign to accept answers as well. Just sayin' $\endgroup$
    – bmf
    Feb 5, 2023 at 7:42

1 Answer 1

0
$\begingroup$

I found the solution, I think.

  1. Use the DataRange option to match exactly the range of the input X and Y lists in physical units.
  2. Use the PlotRange option to limit the axes to the physical unit ranges of choice.
x = {0.1, 0.2, 0.3, 0.4}
y = {0.2, 0.3, 0.4, 0.5, 0.6, 0.7}

Do[Do[zarr[[j, i]] = x[[i]]^2 + y[[j]]^2, {i, 1, Length[x]}], {j, 1, 
  Length[y]}]

ListContourPlot[zarr, DataRange -> {{0.1, 0.4}, {0.2, 0.7}}, 
 PlotLegends -> Automatic, PlotRange -> {{0.15, 0.35}, {0.3, 0.6}}]

One could use other values for DataRange, but then the connection to physical units is lost.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.