3
$\begingroup$

I have a matrix of data that I am plotting via ArrayPlot.

FData = {{0., 0., 0.066812, 0., 0., 0., 0.}, {0., 0.479787, 0., 
   0.0683112, 0., 0., 0.}, {0.0669646, 0., 0.0697005, 0., 0., 0., 
   0.}, {0., 0.0683715, 0., 0., 0., 0., 0.}, {0., 0., 0., 0., 0., 0., 
   0.}, {0., 0., 0., 0., 0., 0., 0.}, {0., 0., 0., 0., 0., 0., 0.}}

I would like to reverse the color bar colors in the Plot legend. I used:

ArrayPlot[
 FData,
 PlotLegends -> 
  BarLegend[{GrayLevel, {0, Max[FData]}}, 
   LabelStyle -> {FontSize -> 20}], DataReversed -> False]

And this gives me:

The colors are flipped in this figure and I'd like the darkest to represent the highest number in my matrix.

When I try to "Reverse" my data (which doesn't seem sound to me) using the below snippet:

FData[[1 ;; 7, 1 ;; 7]]
ArrayPlot[
 FData[[1 ;; 7, 1 ;; 7]],
 PlotLegends -> 
  BarLegend[{GrayLevel, {0, Reverse[FData[[1 ;; 7, 1 ;; 7]]]}}, 
   LabelStyle -> {FontSize -> 20}], DataReversed -> False]

And that gives me nothing!

Edit/Errata

I have been using PlotLegends -> BarLegend[{GrayLevel, {0, 1}}, LabelStyle -> {FontSize -> 20}] to set the font size. Based on the answers below, how can I go about changing the font size?

I know this is very basic but I can't quite figure out whats going on!

$\endgroup$
2
  • $\begingroup$ What Mma version are you using? $\endgroup$ Commented May 13, 2013 at 18:31
  • $\begingroup$ @belisarius Mathematica 9.0... Will make the edit $\endgroup$
    – dearN
    Commented May 13, 2013 at 18:33

2 Answers 2

5
$\begingroup$
With[{g = GrayLevel[1 - #] &},
       ArrayPlot[FData, ColorFunction -> g, PlotLegends -> BarLegend[{g, {Min@FData, Max@FData}}, 
                 LabelStyle -> Directive[Blue, Large]]]]

enter image description here

$\endgroup$
5
  • $\begingroup$ How do I change the font size of the text in the colorbar? I was using BarLegend for that purpose... ArrayPlot[mat, PlotLegends -> BarLegend[{GrayLevel, {0, 1}}, LabelStyle -> {FontSize -> 20}]] $\endgroup$
    – dearN
    Commented May 13, 2013 at 18:49
  • $\begingroup$ @drN See edit, please $\endgroup$ Commented May 13, 2013 at 19:06
  • $\begingroup$ Thank you. I would have never thought of this. $\endgroup$
    – dearN
    Commented May 13, 2013 at 19:08
  • $\begingroup$ You might want to change rData to FData.. Thank you though, the essense of the answer still exists. $\endgroup$
    – dearN
    Commented May 13, 2013 at 19:09
  • $\begingroup$ @drN yeah, I changed your FData for rData in my notebook. Always try to start your symbols with lowercase! $\endgroup$ Commented May 13, 2013 at 19:12
6
$\begingroup$

With ColorFunctionScaling -> True (default), the data values are rescaled to between 0 and 1, which is then input to the ColorFunction. So the general solution to "reverse" the color scheme is to simply input 1 - x, where x is the rescaled data point, as shown in belisarius' answer.

If you're using a built-in color scheme, you can use the "Reverse" property to automatically reverse the colors as in the following example:

With[{data = RandomReal[1, {10, 10}]},
    {ArrayPlot[data, ColorFunction -> "Temperature", PlotLabel -> "Original"], 
     ArrayPlot[data, ColorFunction -> ColorData[{"Temperature", "Reverse"}], 
        PlotLabel -> "Reversed"]
    } // GraphicsRow
]

enter image description here


The documentation for the *Legend functions fully specify how to use the options to style them. Here's an example:

ArrayPlot[RandomReal[1,{10, 10}], ColorFunction -> ColorData[{"Temperature", "Reverse"}], 
    PlotLegends -> BarLegend[{ColorData[{"Temperature", "Reverse"}], {0, 1}}, 
        LabelStyle -> Directive[FontFamily -> "Palatino", FontSize -> 14, Red], 
        LegendLabel -> "Colorbar", LegendFunction -> "Panel"
    ]
]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ that's a great answer. How one does both "Reverse" and specify a range to normalize the scale? ColorFunction -> ColorData[{"Temperature", "Reverse",{0,1}} does not work $\endgroup$
    – Rho Phi
    Commented Jun 2, 2017 at 16:13

Your Answer

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

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