4
$\begingroup$

I am trying to put specific labels on the x-axis of my bar graph. There are 56 bars. I want to have labels 1960, 1970, ... and 2010 on bars 1, 11, ... and 51 respectively with ticks at only those points. I can't seem to be able to figure out how to do this. Code is below. Would anyone be able to help me? Thank you!

Show[BarChart[
  data,
  FrameLabel -> {{"A", None}, {"" (*because there are three stacked graphs and only the bottom one has an axis label*), None}},
  ChartStyle -> {{Red}, {EdgeForm[]}},
  BaseStyle -> {FontFamily -> "Arial", FontSize -> 10},
  Frame -> {{True, None}, {True, None}},
  ImageSize -> {3*150, 2*150/3} (*the three graphs are supposed to add up in height to that of another graph*),
  AspectRatio -> 2/9,
  PlotRange -> {0, 
    Max[dataFromTheThreeGraphs] (*so the y-axes are the same*)}]]

EDIT: Here's some sample data:

data={0.,0.,0.,0.,0.,0.00437956,0.,0.,0.00236407,0.,0.00510725,0.00300903,0.0030426,0.000983284,0.00102987,0.00304569,0.00285714,0.00100806,0.000982318,0.,0.,0.00188324,0.00172563,0.000807754,0.000795545,0.00233645,0.0015528,0.,0.000776398,0.,0.001321,0.000788644,0.,0.000747943,0.,0.,0.000599161,0.,0.000578704,0.,0.,0.,0.000926355,0.,0.,0.000728597,0.00103448,0.000319898,0.000886001,0.,0.,0.000469373,0.000425532,0.000400882,0.00132475,0.000366569};
dataFromTheThreeGraphs={0.,0.,0.,0.00118064,0.,0.00437956,0.,0.,0.0035461,0.00107875,0.00715015,0.00501505,0.0040568,0.00294985,0.00514933,0.00609137,0.0047619,0.00100806,0.00196464,0.000882613,0.00187793,0.00282486,0.00172563,0.000807754,0.00318218,0.00233645,0.0015528,0.00149365,0.000776398,0.000688705,0.00198151,0.00236593,0.00225904,0.00149589,0.,0.00332889,0.00179748,0.00126743,0.00289352,0.00218103,0.00265675,0.00104275,0.00509495,0.00292887,0.00161616,0.00327869,0.00413793,0.00223928,0.00502067,0.00434075,0.00392465,0.00469373,0.00595745,0.00521147,0.00567752,0.00293255};
$\endgroup$
  • $\begingroup$ Can you provide the data? Or if it's too long, perhaps a short snippet that makes fake data we could use? First step is usually copying the code and trying to execute it. $\endgroup$ – Jason B. Aug 24 '16 at 18:04
  • $\begingroup$ The data are being pulled from a massive excel file. I edited in some sample data. @JasonB $\endgroup$ – user3769181 Aug 24 '16 at 18:43
5
$\begingroup$
Show[BarChart[data, ChartStyle -> {{Red}, {EdgeForm[]}}, 
  Axes -> False],
 FrameLabel -> {{"A", None}, {"" , None}},
 BaseStyle -> {FontFamily -> "Arial", FontSize -> 10},
 Frame -> {{True, None}, {True, None}},
 FrameTicks -> {{Automatic, 
    Automatic}, {{{1, "1960"}, {11, "1970"}, {51, "2010"}}, None}},
 ImageSize -> {3*150, 2*150/3} ,
 AspectRatio -> 2/9,
 PlotRange -> {0, Max[dataFromTheThreeGraphs] }]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Fantastic, thank you! Is there a way to reduce/limit the number of labels on the y-axis so there aren't so many crowded in? @JasonB $\endgroup$ – user3769181 Aug 24 '16 at 19:07
  • $\begingroup$ Sure, you can manually adjust the ticks, or use FindDivisions to help out. Use the line FrameTicks -> {{N@FindDivisions[MinMax[data], 4], None}, {{{1, "1960"}, {11, "1970"}, {51, "2010"}}, None}} $\endgroup$ – Jason B. Aug 24 '16 at 19:15
  • $\begingroup$ Apparently Mathematica doesn't recognize MinMax. Where's that from? $\endgroup$ – user3769181 Aug 24 '16 at 19:31
  • $\begingroup$ It was introduced in version 10.1. Replace MinMax[data] with {Min@data,Max@data} $\endgroup$ – Jason B. Aug 24 '16 at 19:34
  • $\begingroup$ Okay, that makes sense. I'm sorry, one last question; it's bothering me that the numbers go to different decimal places (i.e., 0.01 and 0.015). Is there a way to normalize that (i.e., make it 0.010 and 0.015)? Would that also be possible for 0? $\endgroup$ – user3769181 Aug 24 '16 at 19:54

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.