1
$\begingroup$

I want to place a bar legend right up against a density plot.

Here are what I have tried

fcn = N[Array[Sin[#1*#2] &, {10, 10}, {1, 1}]];
frameLabels = {DataRange -> {{-π/2., 3 π/2.}, {-π/2., 
      3 π/2.}}, Frame -> True, 
   FrameTicks -> {#, #} &@
    Transpose[
     Array[{{-π + π/2 #, -1. + .5 #}, {-π + π/
            2 #,}} &, 5]], 
   Epilog -> {MapThread[
      Text[Style[#1, FontFamily -> "Times New Roman", 
         Italic, #2], #3] &, {{"θ/π", 
        Rotate["ϕ/π", 90 Degree]}, {22, 
        22}, {{π/2., -π/2. - .7}, {-π/2. - .9, π/
          2.}}}]}};
leg = PlotLegends -> 
   Placed[BarLegend[{ColorData["DarkRainbow"], {Min[fcn], Max[fcn]}}, 
     LegendLayout -> "Row", LegendMarkerSize -> 220, 
     "Ticks" -> {{-3.14, -π}, {0., 0}, {3.14, π}}, 
     "TickSide" -> Left, "TickLengths" -> -1.5], {.534, 0.99999}];
imagePad = {PlotRangeClipping -> False, 
   ImagePadding -> {{44, 10}, {34, 10}}, PlotRangePadding -> 0, 
   ImageSize -> 300};
appearStyle = {BaseStyle -> {FontSize -> 16}, 
   ColorFunction -> ColorData["DarkRainbow"], 
   PlotTheme -> "Classic"};
ListDensityPlot[fcn, 
 Evaluate[{frameLabels, imagePad, appearStyle, leg}]]

try1

When I changed the position in Placed to be {.534, 1.00001}, it looked like this:

enter image description here

But what I want to obtain is something like this:

aim

How could this be achieved?

I set PlotRangePadding->False because I don't want to have space between frame and data.

(I apologize that i did not give a code generating fcn in last version.)

$\endgroup$
  • 5
    $\begingroup$ Since you don't give us code we can run (fcn, Zak are undefined), we can't experiment with your plot. It is unlikely you will get an answer. $\endgroup$ – m_goldberg Jan 13 '17 at 4:57
5
$\begingroup$

I tried looking at the inside of the auto-generated legends and found the option Charting`TickSide which helps a bit. I just used Column to put them next to each other, I'm sure there is a better solution though. Here's the best I got:

With[
 {data = {{1, 1, 1, 1}, {1, 2, 1, 2}, {1, 1, 3, 1}, {1, 2, 1, 4}}},
 Column[{
   BarLegend[
    {"DarkRainbow", MinMax[data[[All, -1]]]},
    LegendMargins -> 0,
    LegendLayout -> "Row",
    LegendMarkerSize -> 300,
    Charting`TickSide -> Left
    ],
   ListDensityPlot[data,
    PlotRangePadding -> 0,
    ColorFunction -> "DarkRainbow",
    ImageMargins -> 0,
    ImageSize -> 300
    ]
   },
  Spacings -> 0
]] 

legends

$\endgroup$

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.