5
$\begingroup$

Is there any way of generating a density plot (or something similar) with one of its axes on a log scale? Essentially I'm trying to combine a LogLinearPlot and a normal Plot on two different variables to create the associated density plot.

If this can't be done directly, what would be the best work-around?

$\endgroup$
6
  • $\begingroup$ If you add a small self-contained example you will get more/better answers. $\endgroup$
    – Ajasja
    Feb 1, 2013 at 13:00
  • 4
    $\begingroup$ I can't think of an easier workaround than: DensityPlot[f[x,y], {x, Xmin, Xmax}, {y, Ymin, Ymax}] to DensityPlot[f[x, E^y],{x, Xmin, Xmax}, {y, Log@Ymin, Log@Ymax}]. Adding ticks along y might take you some time but it shouldn't be hard (I've assumed that's the direction you want to log plot) $\endgroup$
    – gpap
    Feb 1, 2013 at 13:13
  • $\begingroup$ I think ScalingFunctions works with DensityPlot... That might help. $\endgroup$
    – cormullion
    Feb 1, 2013 at 17:32
  • $\begingroup$ @cormullion Could you give an example? I can only get it working for the function value but not the axis. (Something like DensityPlot[Sin[x] Sin[y], {x, 1, 10}, {y, 1, 10}, ScalingFunctions -> "Log"]) $\endgroup$
    – Silvia
    Feb 1, 2013 at 18:50
  • $\begingroup$ @silvia no I can't (:-() but I thought there might be those who were clever enough to make it happen... $\endgroup$
    – cormullion
    Feb 1, 2013 at 19:16

1 Answer 1

3
$\begingroup$

Thanks for the input in the comments; here's what I eventually came up with (including tick placement):

fSpace[min_, max_, steps_, f_: Identity] := 
 InverseFunction[f] /@ 
  Range[f@min, f@max, (f@max - f@min)/(steps - 1)]
GetMajorTicks[min_, max_, count_, f_: Identity] := 
  fSpace[min, max, count, f];
GetMinorTicks[min_, max_, majorCount_, minorCount_, f_: Identity] := 
  Flatten[Table[
    Drop[Drop[
      fSpace[x, 
       x ((f@max - f@min)/(majorCount - 1) // InverseFunction[f]), 
       minorCount + 2], 1], -1], {x, 
     Drop[GetMajorTicks[min, max, majorCount, f], -1]}]];

xMin = 1/4;
xMax = 4;
xMajorTicks = 5;
xMinorTicks = 3;
f = Log;
ContourPlot[
 Sin[2 \[Pi] y] InverseFunction[f]@x, {x, f@xMin, f@xMax}, {y, -1, 1},
  FrameTicks -> {{Automatic, 
    None}, {({f@#, # // N} & /@ 
       GetMajorTicks[xMin, xMax, xMajorTicks, f])~
     Join~({f@#, Null, {0.005, 0}} & /@ 
       GetMinorTicks[xMin, xMax, xMajorTicks, xMinorTicks, f]), 
    None}}]

The plot

If this can be cleaned up or improved in any way, I'd love to hear!

$\endgroup$

Your Answer

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

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