4
$\begingroup$

I'm not really familiar wih ListDensityPlot and for example I have the following code

ListDensityPlot[Table[N[Sin[x]*Cos[y]], {x, 0, 5}, {y, 2, 7}], PlotLegends -> Automatic, PlotRange -> Full, ColorFunction -> ColorData["SolarColors"]]

What is in the x and y axis? And why is the x axis numbered from 1 to 6 and not 0 to 5. The same for the y axis, why is it also numbered from 1 to 6 and not 2 to 7?

Thanks in advance

$\endgroup$
  • 1
    $\begingroup$ Try ListDensityPlot[Flatten[Table[{x,y,N[Sin[x]*Cos[y]]}, {x, 0, 5}, {y, 2, 7}],1], PlotLegends -> Automatic, PlotRange -> Full, ColorFunction -> ColorData["SolarColors"]] . You need to use a three column array for ListDensityPlot. $\endgroup$ – Sumit Nov 5 '17 at 13:59
  • 1
    $\begingroup$ @Sumit beat me to it, except I'd use Catenate rather than the more Flatten[..., 1]. You could also (less cleanly, IMO) use DataRange->{{0,5},{2,7}}. $\endgroup$ – jjc385 Nov 5 '17 at 14:01
7
$\begingroup$

In short ListDensityPlot like other ListPlot variants uses the index of the value of the array for the axes ticks unless it is otherwise specified.

If you wanted it to plot with the coordinates you passed there are several ways. One way is to add the option DataRange -> {{2, 7}, {0, 5}} to ListDensityPlot. Note that with how you have constructed your table, your $y$ coordinate is on the $x$-axis.

An alternative way is to include the coords in the data with something like

Flatten[Table[{x, y, N[Sin[x]*Cos[y]]}, {x, 0, 5}, {y, 2, 8}], 1]

or maintaining your axes:

Flatten[Table[{x, y, N[Sin[x]*Cos[y]]}, {x, 0, 5}, {y, 2, 8}], 1]

You have to flatten the array in this case because of what ListDensityPlot is able to interpret.

Edit: It seems I was ninja'd by the comments.

$\endgroup$
  • $\begingroup$ "It seems I was ninja'd by the comments." - it's fine, answers trump comments. :) $\endgroup$ – J. M. will be back soon Nov 5 '17 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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