7

I want to use ListDensityPlot on a dataset containing a few (~600) triplets and I would like to reverse one of the axes. Sadly, the workaround involving the ScalingFunctions option won't work with ListDensityPlot and neither does the DataRange option.

For example

Module[{data = RandomReal[{0, 10}, {100, 3}]},
 ListDensityPlot[data, InterpolationOrder -> 0]
]

without datarange option

I know how to solve my problem by changing the sign of the dimension I want to invert, I am just baffled with not being able to do it within ListDensityPlot. I am on OSX and v10.2.0.0

gpap
  • 9,707
  • 3
  • 24
  • 66
  • So, to be clear, you don't want a solution of the form of ListDensityPlot[{#1, -#2, #3} & @@@ data, InterpolationOrder -> 0]? – march Sep 23 '15 at 16:14
  • Ehm, I was hoping for an option within the plotting function but I don't think there is one. I am currently doing more or less what you are suggesting. When I initially asked, I had a mental block and thought datarange could be changed but that's fixed here. – gpap Sep 23 '15 at 16:23
  • Yeah, just checking. There are some things that it seems like should be included in Mathematica's plotting functionality that aren't. So it goes. – march Sep 23 '15 at 16:25

1 Answers1

8

If you are only looking to reverse the axis when your values go from 0 to 10, it is pretty simple - as you said you just multiply the first column by -1. But then you still haven't reversed the tick labels. For this, I think the easiest thing is to use the CustomTicks package:

Module[{data = RandomReal[{0, 10}, {100, 3}]},
   Grid[{{ListDensityPlot[data, InterpolationOrder -> 0], 
     ListDensityPlot[{-#1, #2, #3} & @@@ data, InterpolationOrder -> 0,
      FrameTicks -> {LinTicks[-10, 0, 2, 4,TickLabelFunction -> (Round[-#] &)], LinTicks, Automatic,Automatic}]}}
   ]
]

enter image description here

But what if you have a general range in the x coordinate that doesn't start at zero? This should do the trick I think:

reverseXplot[data_] := Module[{xmin, xmax, xmid},
  xmin = Min[data[[All, 1]]];
  xmax = Max[data[[All, 1]]];
  xmid = (xmin + xmax)/2 &@data[[All, 1]];
  ListDensityPlot[{2 xmid - #1, #2, #3} & @@@ data, 
  InterpolationOrder -> 0, 
  FrameTicks -> {LinTicks[FindDivisions[{xmin, xmax}, 5], FindDivisions[{xmin, xmax}, 20], 
  TickLabelFunction -> (Round[2 xmid - #] &)], LinTicks, 
Automatic, Automatic}]
  ]

Module[{data = RandomReal[{10, 20}, {100, 3}]},
       Grid[{{ListDensityPlot[data, InterpolationOrder -> 0], 
       reverseXplot[data]}}]
]

enter image description here

Jason B.
  • 68,381
  • 3
  • 139
  • 286