0
$\begingroup$

I am plotting DensityHistograms with a logscaled y-axis with this code:

DensityHistogram[wd, {{1}, {50}}, PlotRange -> {{-20, 50}, {Log10[40], Log10[6000]}}, 
 ScalingFunctions -> {"Linear", "Log10"}, 
 FrameLabel -> {{"Energie/q in eV/q"}, {"x in R"}}, 
 LabelStyle -> {"Medium"}, ColorFunction -> "DarkRainbow", 
 ColorFunction -> "DarkRainbow", 
 ChartLegends -> BarLegend[{"DarkRainbow", {0, 25}},
  LegendLabel -> Placed["#Ionen", Top]]]

My data is weighted so I hope it doesn't make any difference if an example for a row of wd is:

{47.4237, 1266.92}  

I recieve a logscaled $y$ axis and a linear $x$ axis. The problem is that my BarLegend isn't scaled by Log10. Could anyone change my code so I receive a logscaled BarLegend? (The label and placement of the label is very important, I hope it is possible without changing that.) Thank you.

$\endgroup$
  • $\begingroup$ i dont know if that is useful but for example the values of my barlegend range from 0 to 1000 $\endgroup$ – Sophia Aug 25 '17 at 16:16
1
$\begingroup$

First, a note about your BarLegend: It is generally safer if you just put Automatic as specification for the color function an range - that way, those parameters are taken from the Plot and are guaranteed to match: In your case, this means

ChartLegends -> BarLegend[Automatic, LegendLabel -> Placed["#Ionen", Top]]

Note: The z specification in PlotRange only removes data outside the specified range from the plot - it doesn't rescale the color function to the new maximum, and the legends are left untouched. If you need to customize this further, take a look at ColorFunctionScaling.

Actual answer

The colors of the individual cells are the z-axis of your plot. Therefore, if you want to scale the colors in the legend, simply specify "Log10" scaling in ScalingFunctions:

DensityHistogram[wd, {{1}, {50}}, 
 PlotRange -> {{-20, 50}, {Log10[40], Log10[6000]}}, 
 ScalingFunctions -> {"Linear", "Log10", "Log10"}, 
 FrameLabel -> {{"Energie/q in eV/q"}, {"x in R"}}, 
 LabelStyle -> {"Medium"}, ColorFunction -> "DarkRainbow", 
 ChartLegends -> 
  BarLegend[Automatic, LegendLabel -> Placed["#Ionen", Top]]]

Produces: (with random data: Table[{RandomReal[{-20, 50}], RandomReal[3000]}, 10000])

enter image description here

vs. before (same data):

enter image description here

$\endgroup$
  • $\begingroup$ Thank you so much for this perfect answer :) $\endgroup$ – Sophia Aug 28 '17 at 9:10

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.