Skip to main content

plotting - Making a presentable BarChart


I've been asked to reproduce a bar chart generated in Excel in Mathematica. The original Excel chart looks like this;


Excel chart



My Mathematica version looks like this;


Mathematica chart


There are a couple of things wrong that I'd like to fix;



  1. The BarChart and ListPlot overlay doesn't seem to match up.

  2. The ChartLabels seem to have disappeared on the BarChart.

  3. Is there a nice way to make the ticks on the left and right sides match up (like when the Excel chart matches 9 % on the left to 90 % on the right)?

  4. I can't get the box and line to center align to the text in the legend.


Questions 1 and 2 are what I really need to fix, but 3 and 4 would be nice to have. Any help would be appreciated.



Here's the code I used to generate my chart;


purple = RGBColor[97/255, 16/255, 106/255];
orange = RGBColor[245/255, 132/255, 31/255];
labels =
{
"FY15 Q1/2", "FY15 Q3/4", "FY16 Q1/2", "FY16 Q3/4",
"FY17 Q1/Q2", "FY17 Q3/Q4", "FY18 Q1/2", "FY18 Q3/4"
};
starvedTime = {7.55, 11.23, 8.58333, 6.88833, 4.65167, 1.89, 6.49833, 1.95};
satTime = {70.1483, 81.1467, 81.115, 86.5483, 84.6833, 90.685, 79.6017, 91.0133};


plot1 = BarChart[starvedTime,
PlotRange -> {0, 12},
ChartStyle -> purple,
BaseStyle -> "Text",
Frame -> {True, True, True, False},
FrameTicks -> {False, True},
FrameLabel -> {None, Style["Smelter Starved Time (%)", "Text"], None, None},
PlotLabel -> "Smelter Starved Time by 6 Month Period: Base Case",
ImageSize -> Large,

ChartLabels -> {Placed[Style[#, "Text"] & /@ labels, Below], None},
AxesOrigin -> {0, 0},
PlotRangePadding -> {0, 0},
BarSpacing -> 1];

plot2 = ListPlot[satTime,
Joined -> True,
PlotRange -> {60, 100},
PlotStyle -> orange,
Frame -> {False, False, False, True},

FrameTicks -> {None, None, None, All},
FrameLabel ->
{None, None, None, Style["Smelter Operating at Constraint Rate (%)", "Text"]},
BaseStyle -> "Text",
GridLines -> {None, Automatic},
GridLinesStyle -> Directive[Gray, Dashed],
ImageSize -> Large];

box = Graphics[{purple, Rectangle[]}, ImageSize -> 12];
text = Style[" Smelter Starved (%) ", "Text"];

line = Graphics[{orange, Line[{{0, 0.5}, {1, 0.5}}]}, ImageSize -> {30, Automatic}];
text2 = Text[Style[" % Time Smelter at Constraint Rate (%) ", "Text"]];
legend = Row[{box, text, line, text2}];

Column[{Overlay[{plot1, plot2}], legend}, Alignment -> {Center, Center}]

Answer



Answers to your 4 questions step by step to see how each of these changes the composite plot:


1. The image padding around the two images differs so you need to set a fixed value for each. With ImagePadding -> {{50, 50}, {50, 10}} as an option for both plots I get this:


enter image description here


2. ChartLabels -> Placed[Style[#, "Text"] & /@ labels, Below],ImagePadding -> {{50, 50}, {20, 10}},



enter image description here


3. in plot #2 add PlotLabel -> ""


enter image description here


4. I almost always prefer Grid to Row:


box = Graphics[{purple, Rectangle[]}, ImageSize -> 12];
text = "Smelter Starved (%)";
line = Graphics[{orange, Line[{{0, 0.5}, {1, 0.5}}]}, ImageSize -> {30, Automatic}];
text2 = "% Time Smelter at Constraint Rate (%)";
legend = Grid[{{box, text, line, text2}}, Alignment -> {{Right, Left}, Center},
BaseStyle -> Directive[FontFamily -> "Arial"], Spacings -> {{0, 0.5, 2}, 0}];


enter image description here


Finishing touches


Bar chart plot ranges go from 0.5 to length of data + 0.5. So set the plot range of your bar chart to PlotRange -> {{0.5, 8.5}, {0, 12}} and for the list plot to PlotRange -> {{0.5, 8.5}, {60, 100}}. Now set your data range for ListPlot to be DataRange -> {1, 8}. This will ensure that the point coincide with the middle of your bars.


plot1 = BarChart[starvedTime, AspectRatio -> 1/GoldenRatio, 
AxesOrigin -> {0, 0}, BarSpacing -> 1,
BaseStyle -> Directive[FontFamily -> "Arial"],
ChartLabels -> Placed[labels, Below], ChartStyle -> purple,
Frame -> {True, True, True, False},
FrameLabel -> {None, "Smelter Starved Time (%)", None, None},

FrameTicks -> {{#, "", {0, 0.01}} & /@ Range[0.5, 8.5, 1], {0, 3,
6, 9, 12}, None, None}, FrameTicksStyle -> Directive[Plain, 12],
GridLines -> {None, {3, 6, 9}},
GridLinesStyle -> Directive[Gray, Dashed],
ImagePadding -> {{50, 50}, {20, 10}}, ImageSize -> 600,
LabelStyle -> Directive[Bold, 12],
PlotLabel ->
Style["Smelter Starved Time by 6 Month Period: Base Case", 13],
PlotRange -> {{0.5, 8.5}, {0, 12}}, PlotRangePadding -> 0,
Ticks -> None];


plot2 = ListPlot[satTime, AspectRatio -> 1/GoldenRatio, Axes -> False,
BaseStyle -> Directive[FontFamily -> "Arial"],
DataRange -> {1, 8}, Frame -> {False, False, False, True},
FrameTicks -> {None, None, None, {60, 70, 80, 90, 100}},
FrameTicksStyle -> Directive[Plain, 12],
FrameLabel -> {None, None, None,
"Smelter Operating at Constraint Rate (%)"}, ImageSize -> 600,
ImagePadding -> {{50, 50}, {20, 10}}, Joined -> True,
LabelStyle -> Directive[Bold, 12], PlotRangePadding -> 0,

PlotRange -> {{0.5, 8.5}, {60, 100}}, PlotStyle -> orange,
PlotLabel -> Style["", 13]];

enter image description here


Note #1. there is scope for you to match the fonts of the Excel chart.


Note #2. Labeled could be used instead of Column.


Note #3. To completely match the Excel chart you actually need the grid lines to be used in the bar chart rather than the list plot.


Note #4. Added some ticks between the bars.


Note #5. Corrected labels: "FY17 Q1/Q2", "FY17 Q3/Q4",


Comments

Popular posts from this blog

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]