2
$\begingroup$
DensityPlot[.015 Sin[x + y], {x, 0, Pi}, {y, 0, Pi}, ColorFunction -> "Rainbow", 
ImageSize -> 500, Frame -> False, Axes -> True, 
AxesLabel -> {"\!\(\*SubscriptBox[\(k\), \(z\)]\)", "\!\(\*SubscriptBox[\(k\), \(x\)]\)"}, 
PlotLegends -> BarLegend[Automatic, LegendMarkerSize -> 380]]

creates the plot Plot with BarLegend

and Dynamic[MousePosition["Graphics"]] displays the position of the mouse on the plot, with coordinates between 0 and Pi. However, if I replace BarLegend in the code above by Placed[BarLegend[Automatic, LegendMarkerSize -> 380], {{1.0, 0.955}, {0.4, 1.0}}], the range of coordinates displayed by MousePosition changes to between 0 and a number or order 0.94. How do I modify my code or otherwise work around this issue? (Observing the maximum values from MousePosition and using them to rescale the results seems neither robust nor elegant.) By the way, using Placed also changes the size of the plot.

$\endgroup$
  • 1
    $\begingroup$ maybe something like Placed[BarLegend[Automatic,LegendMarkerSize -> 380], {Scaled@{1.02, 0.97}, Scaled@{0.3, .95}}]? or {Scaled@{1.03, 0.97}, {0.4, 1.0}} $\endgroup$ – kglr Nov 16 '14 at 21:41
  • 1
    $\begingroup$ @kguler, could you explain why does that work, perhaps in an answer? (It sure does, I've tested! But I have no idea why...) $\endgroup$ – Aisamu Nov 16 '14 at 22:04
  • 1
    $\begingroup$ @Aisamu, I wish I knew why:) I just confirmed the issue (that, for some reason, Placed does change MousePosition["Graphics"] to MousePosition["GraphicsScaled"]), played with a few possible work-arounds, and found one that seemed to work:) $\endgroup$ – kglr Nov 16 '14 at 22:19
  • $\begingroup$ @kguler, Oh no, you were my hope! Hm.. I've tried Placed[... , After] and it did work as expected. It's only when specific (non-scaled) coordinates are used that things break. Even the ImageSize behavior changes, accounting for the whole image instead of only the plot part. $\endgroup$ – Aisamu Nov 16 '14 at 22:38
2
$\begingroup$

How do I modify my code or otherwise work around this issue?

Using Scaled in the last argument of Placed seems to fix the issue:

DensityPlot[.015 Sin[x + y], {x, 0, Pi}, {y, 0, Pi}, 
 ColorFunction -> "Rainbow", ImageSize -> 500, Frame -> False, Axes -> True,
 AxesLabel -> {"\!\(\*SubscriptBox[\(k\), \(z\)]\)", "\!\(\*SubscriptBox[\(k\), \(x\)]\)"},
 PlotRangeClipping -> False, 
 ImagePadding -> {{Automatic, 50}, {Automatic, Automatic}},
 PlotLabel -> Dynamic@Style[Grid[{{"MousePosition[\"Graphics\"]  ", MousePosition["Graphics"]},
       {"MousePosition[\"GraphicsScaled\"]  ", MousePosition["GraphicsScaled"]}}], 16, "Panel"],
 PlotLegends -> Placed[BarLegend[Automatic, LegendMarkerSize -> 380],
                      {Scaled[{1.04, 0.99}], {0.4, 1.0}}]]

enter image description here

$\endgroup$
  • $\begingroup$ What is the meaning of the coordinates used in Placed? {{x,y}, {sizeX, sizeY}}? The documentation says "explicit lists of scaled coordinates", but I'd expect only {x,y}! $\endgroup$ – Aisamu Nov 16 '14 at 22:43
  • 2
    $\begingroup$ @Aisamu, if it helps, in the Details section there is this: Placed[expr,{pos,epos}] specifies that position epos in expr should be placed at relative position pos. $\endgroup$ – kglr Nov 16 '14 at 22:47
  • $\begingroup$ Thanks, @kguler. I now see that BarLegend creates two graphics objects side by side, while Placed with BarLegend merges the two into one, evidently disabling the "Graphics" option. Why applying Scaled to one or both coordinate pairs reenables the option remains unclear to me. Perhaps, it has something to do with the Scaled Details statement, "You can use Scaled to represent objects that occupy a fixed region in a plot, independent of the specific range of coordinates in the plot." In any case, it certainly works. $\endgroup$ – bbgodfrey Nov 17 '14 at 16:59

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.