16
$\begingroup$

I want to plot a big 2D array of experiment data. I need plot axes and tick labels as well as a bar legend using BarLegend:

ArrayPlot[
 Array[RandomInteger[100] &, {100, 100}],
 PlotLegends -> Placed[Automatic, Below],
 ImageSize -> 200,
 PlotLabel -> "Test",
 FrameLabel -> {"Axes", "Dimensions"},
 FrameTicks -> Automatic,
 BaseStyle -> {FontSize -> 14}
]

The output I get is

output of the test script

I am not happy with the size of the BarLegend. I would like to have the BarLegend with the width and horizontal offset of the actual plot. It properly scales with the image size, if I don't use the PlotLabel, FrameTicks and FrameLabel but I need all of them.

I would be happy also with the solutions, which does work in general case, but needs adjusting some number by hand. So far I have figured out, that I can change the horizontal offset with PlotLegends -> Placed[BarLegend[Automatic, LegendMargins -> {{20, 20}, {0, 0}}], Below], but this does not make the legend smaller.

As the BarLegend does not return Graphics, I can not just use Scale[BarLegend["BlueGreenYellow", LegendMargins -> {{20, 20}, {0, 0}}], 2] etc.

Side note: For plotting experiment data I use ArrayPlot instead of ListContourPlot and similar because they get very slow for big arrays. And at least ListContourPlot suffers from the same problem.

$\endgroup$
20
$\begingroup$

As it often happens, when I was researching for writing a fine question I also digged through the documentation and found the answer. There is an option LegendMarkerkSize which in my opinion has an unintuitive name in the case of the BarLegend. Anyway it is exactly meant to do what I needed:

testPlot = ArrayPlot[
  Array[RandomInteger[100] &, {100, 100}],
  PlotLegends -> 
   Placed[BarLegend[Automatic, LegendMarkerSize -> 125, 
     LegendMargins -> {{10, 0}, {0, 0}}], Below],
  ImageSize -> 200,
  PlotLabel -> "Test",
  FrameLabel -> {"Axes", "Dimensions"},
  FrameTicks -> Automatic,
  BaseStyle -> {FontSize -> 14}
  ]

Outcome

enter image description here

As I already wrote the detailed question, I hope it was worth sharing the solution.

$\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.