MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: inverting y axis in DensityPlot

  • To: mathgroup at
  • Subject: [mg27278] RE: [mg27271] inverting y axis in DensityPlot
  • From: "Wolf, Hartmut" <Hartmut.Wolf at>
  • Date: Sun, 18 Feb 2001 02:52:12 -0500 (EST)
  • Sender: owner-wri-mathgroup at

-----Original Message-----
From: Matthias Hertel [mailto:wir95cgu at]
To: mathgroup at
Subject: [mg27278] [mg27271] inverting y axis in DensityPlot


I would like to display the design matrix of a medium-sized general
least-squares problem as a bitmap. (It's too large for StandardForm or
MatrixForm to be useful, yet its structure is simple enough that one
can tell if something has gone really wrong just by looking at a

    ListDensityPlot[desmtx, Mesh->False]

almost does what I want, but the matrix is displayed upside down
(desmtx[[1,1]] is in the *bottom* left corner).

    ListDensityPlot[Reverse@desmtx, Mesh->False]

is closer, but now the frame tick marks are wrong (don't correspond to
the row index). It would be perfect if there was something like

    ListDensityPlot[desmtx, InvertY->True, Mesh->False]

that's just like a normal ListDensityPlot but with the y axis flipped.
But there isn't.

So, how can I get a DensityPlot with (0, 0) in the top left corner and
y increasing downwards?




let's make up an example:

t = Table[Sin[x^2 - x y], {y, 0., 6, 6/50}, {x, 0., 6, 6/50}];
g = ListDensityPlot[t, MeshRange -> {{-0.5, 50.5}, {-0.5, 50.5}}, 
    Mesh -> False]

We have 51 points in each direction, and we adjusted MeshRange a bit 
as to have the Tick marks point to the center of our raster "pixels" 
and not to their left lower corners. Now if we plot with the y-axes 

gy = ListDensityPlot[Reverse@t, 
     MeshRange -> {{-0.5, 50.5}, {-0.5, 50.5}}, Mesh -> False]

we get (of course) the same tick marks, corresponding to the indices 
of the reversed matrix. But you want to have them correspond to the 
indices of the original matrix.

You can do that by either specifying the FrameTicks explicitly or with 
a hand-crafted tick function, yet the better idea seems to be: just take 
the automatically generated tick marks of the graphics and manipulate 
their labels a bit such that they'll come out "right".

Now we can't get at the FrameTicks of a DensityGraphics by FullOptions 
(which does next to nothing here) nor by FullGraphics (which here does 
exactly nothing). The trick is to convert the DensityGraphics to a 
Graphics first:

rg = Graphics[gy]

and then get at the FullOptions

opts = FullOptions[rg];

Now we can do what we want, I did it this way:

Block[{minlab = Infinity, maxlab = -Infinity}, 
  Show[rg, opts /. (FrameTicks -> {bottom_, left_, up_, right_}) :> 
        FrameTicks -> {bottom, 
            left /. {y_, ylabel_?NumberQ, len_, style_} 
                    /; (If[ylabel > maxlab, maxlab = ylabel]; 
                        If[ylabel < minlab, minlab = ylabel];
                    :> {y, maxlab - ylabel + minlab, len, style}, 
            up, right}]]

This code deserves some explanation. As you see we manipulate the 
tick marks to the left. As generated by the automatic tick function the
labels are numeric (not explicitly converted to strings) -- this is
our luck. The intend of the conversion formula is clear, but how come 
the right values of maxlab and minlab (at the right time)? 
RuleDelayed does not evaluate it's rhs (it has the HoldRest attribute), 
but the lhs is evaluated and such is the condition on the pattern, which 
we just set to True, but misuse it as a hook to calculate the 
minimum and maximum for the labels on the fly.

-- Hartmut

  • Prev by Date: Re: Problem: a matrix and a box...
  • Next by Date: Re: RE: Solving for Sum element
  • Previous by thread: inverting y axis in DensityPlot
  • Next by thread: Re: RE: inverting y axis in DensityPlot