15
$\begingroup$

I would like to place AxesLabels:

  1. For x axis at center of the Histogram and LineListPLots.
  2. For y axis rotated 90 degree anti clockwise.

I don't see any options for it?

Histogram[Table[zcb[i],{i,mcRunHJM}],AxesLabel->{Price,Relative Frequency}]

ListLinePlot[Table[zcb[i],{i,mcRunHJM}],PlotRange->{{0,mcRunHJM},{0.9,1.1}}, PlotLabel->Style["Convergence",FontSize->12],AxesLabel->{Simulation Step,Price}]  

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ Take a look at FrameLabel. $\endgroup$
    – VLC
    Jan 16, 2013 at 15:57
  • 2
    $\begingroup$ Duplicate of this? $\endgroup$
    – m_goldberg
    Jan 16, 2013 at 17:04

3 Answers 3

21
$\begingroup$

For the histogram:

Labeled[Histogram[
  RandomReal[NormalDistribution[0, 1], 200]], {Rotate[
   "Relative Frequency", 90 Degree], "Price"}, {Left, Bottom}]

enter image description here

Addendum

Further to Sebastian's comment to add stats:-

data = RandomReal[NormalDistribution[0, 1], 200];
Labeled[Histogram[data,
  Epilog -> Inset[Framed[Grid[{
       {"Mean =", Mean[data]},
       {"Max =", Max[data]},
       {"Min =", Min[data]}},
      Alignment -> {{Left}}], Background -> White],
    {Right, Top}, {Right, Top}],
  BaseStyle -> {FontFamily -> "Courier"}], {Rotate[
   "Relative Frequency", 90 Degree], "Price"},
 {Left, Bottom}]

enter image description here

To show two data sets, two methods:-

dataA = RandomReal[NormalDistribution[0, 1], 200];
dataB = RandomReal[NormalDistribution[0, 1], 100];

Labeled[Histogram[{dataA, dataB},
  ChartStyle -> {ColorData[3, 4], ColorData[3, 6]},
  ChartLegends -> {
    Style[StringJoin["Set A mean = ",
      ToString[Round[Mean[dataA], 10.^-6]]],
     FontFamily -> "Courier"],
    Style[StringJoin["Set B mean = ",
      ToString[Round[Mean[dataB], 10.^-6]]],
     FontFamily -> "Courier"]}],
 {Rotate["Relative Frequency", 90 Degree],
  "Price                         "}, {Left, Bottom}]

enter image description here

For more control of appearance the second method uses ShowLegend:-

(* Specify common bin division size *)
histA = Histogram[dataA, {0.5}];
histB = Histogram[dataB, {0.5}];
(* Find common plot range and axes origin *)
minmaxA = First[PlotRange /. Options[histA, PlotRange]];
minmaxB = First[PlotRange /. Options[histB, PlotRange]];
tAB = Transpose[{minmaxA, minmaxB}];
minmaxAB = {Min[First[tAB]], Max[Last[tAB]]};
aoA = First[AxesOrigin /. Options[histA, AxesOrigin]];
aoB = First[AxesOrigin /. Options[histB, AxesOrigin]];
aoAB = Min[aoA, aoB];

Quiet[Needs["PlotLegends`"]];
Labeled[
 ShowLegend[Show[
   Histogram[dataA, {0.5},
    ChartBaseStyle -> {ColorData[3, 4], Opacity[0.5]}],
   Histogram[dataB, {0.5},
    ChartBaseStyle -> {ColorData[3, 6], Opacity[0.5]}],
   PlotRange -> {minmaxAB, {All, All}},
   AxesOrigin -> {aoAB, 0}],

  {{{Graphics[{ColorData[3, 4], Opacity[0.5], Rectangle[]}], 
     Style[StringJoin["Set A mean = ",
       ToString[Round[Mean[dataA], 10.^-6]]],
      FontFamily -> "Courier"]}, {Graphics[{ColorData[3, 6], 
       Opacity[0.5], Rectangle[]}], Style[StringJoin["Set B mean = ",
       ToString[Round[Mean[dataB], 10.^-6]]],
      FontFamily -> "Courier"]}},
   LegendPosition -> {0.2, 0.6},
   LegendSize -> {1.1, 0.25},
   LegendTextSpace -> 8,
   LegendShadow -> None}],
 {Rotate["Relative Frequency", 90 Degree], "Price      "},
 {Left, Bottom}]

enter image description here

And a matching ListLinePlot:-

linedataA = RandomReal[NormalDistribution[0, 1], 20];
linedataB = Accumulate[linedataA];

ListLinePlot[{linedataA, linedataB},
 Frame -> {{True, False}, {True, False}},
 FrameLabel ->
   Map[Style[#, {FontFamily -> "Courier", 13}] &,
   {"Simulation Step", "Price"}],
 PlotLegends -> {"Set A", "Set B"}]

enter image description here

You might find this book useful: Mathematica Navigator. It shows a lot of charting techniques.

$\endgroup$
5
  • $\begingroup$ Hi Chris, what about placing the Mean[List], Max[List] and Min[List] on the histogram (or ListLinePlot) that I already calculated? At the moment they are simply outputs somewhere else. $\endgroup$ Jan 19, 2013 at 14:34
  • $\begingroup$ Hi Chris, could you show how to overlay histogram simulations a and b together? I tried plot1=Labeled[Histogram[data...]], plot2=Labeled[Histogram[data...]], and then Show[plot1, plot2] doesnt work? Error:Labeled is not a Graphics primitive or directive. $\endgroup$ Jan 20, 2013 at 1:32
  • $\begingroup$ Hi Chris, Thanks very cool, but not sure the difference between the two from the histograms? Dont see where you define the histogram colors and thickness? By ColorData? $\endgroup$ Jan 20, 2013 at 13:19
  • $\begingroup$ ChartLegends is not an option for ListLinePlot? Tried applying the Histogram example for ListLinePlot but returns-> Unknown option ChartLegends? $\endgroup$ Jan 20, 2013 at 16:43
  • $\begingroup$ @sebastian c. - For ListPlot you can use PlotLegends (as shown), or ShowLegend, as shown earlier. $\endgroup$ Jan 20, 2013 at 19:20
12
$\begingroup$

This can be accomplished by a combination of two options: Frame and FrameLabel.

data = RandomReal[10, {10, 2}];
ListPlot[data, FrameLabel -> {"x", "y"}, Frame -> {{True, False}, {True, False}}]

enter image description here

As shown above, FrameLabels by itself does not do it as you need to specify which sides of the frame are displayed. Note, this applies to all two dimensional plots.

$\endgroup$
4
  • $\begingroup$ Thanks @rcollyer, though the FrameLabel looks small, any way set FontSize? But applying it on the Histogram I noticed the whole set of data gets shifted up so data seems to be floating instead of where it was before? Histogram[Table[zcb[i],{i,mcRunHJM}],FrameLabel-> {"Price","Relative Frequency"},Frame-> {True, True, False, False}] $\endgroup$ Jan 16, 2013 at 16:19
  • $\begingroup$ @sebastianc. LabelStyle. Also, if this is useful to you, you should vote for it. If it solves your problem, you should accept it. I'd suggest accepting only after a period of time (like 24 hours) to allow for other answers to come in. $\endgroup$
    – rcollyer
    Jan 16, 2013 at 16:23
  • $\begingroup$ @BrettChampion both forms work, at least in 8. $\endgroup$
    – rcollyer
    Jan 16, 2013 at 16:40
  • $\begingroup$ Yes, both forms work, but I'd rather people learn to use the "new" (introduced in V6) syntax rather than the older syntax. $\endgroup$ Jan 16, 2013 at 17:41
5
$\begingroup$

I like to use the frame method for histograms as well as for plots. To stop it shifting up the you can set PlotRangePadding to None for the relevant side. In addition, if manually setting the chart range, you can turn off the y-axis to avoid a nasty line in your histogram marking the first bin. Finally, I change the font settings using BaseStyle.

Histogram[RandomReal[NormalDistribution[0, 1], 200],
  Frame -> {True, True, False, False},
  FrameLabel -> {"Price", "Frequency"},
  PlotRangePadding->{Automatic, {None, Automatic}},
  BaseStyle -> {FontSize -> 14},
  PlotRange -> {{-5, 5}, Automatic}, Axes -> {True, False}]

Produces:

Histogram with sensible axis label positions

$\endgroup$

Your Answer

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

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