5
$\begingroup$

Hi I have a question on how to plot a calculated gradient fit (fd4,fdata4) into a bar chart. Data 1 is the raw data, using FindFit function I got the gradient fit but I would like to plot the function into the bar chart? Any recommended steps that I can take note of?

Data1 = {10.95, 11.35, 10.0533, 10.22, 9.66, 10.2283, 10.3889, 10.8889,
    10.1667, 10.7111, 11.7917, 12.2083, 10.7667, 11.5417, 12.0583, 12.55,
    12.175, 12.9167, 14.4333, 14.9917, 14.7417, 14.9333, 15.8455, 15.95, 
    17.1773, 17.6, 13.9909, 14.0909, 14.7545, 14.6227, 15.8212, 15.5848, 
    15.2152, 14.8697, 13.8, 13.8333, 15.1472, 15.0861, 14.2111, 14.4639, 
    13.6583, 14.1194};

fd4 = FindFit[Data1, {a + b*x + c*x^2 + d*x^3 + e*x^4}, {a, b, c, d, e}, x]

{a -> 12.1913, b -> -0.872446, c -> 0.10227, d -> -0.0033337, e -> 0.0000338078}

fdata4 = a + b*x + c*x^2 + d*x^3 + e*x^4 /. fd4

12.1913 - 0.872446 x + 0.10227 x^2 - 0.0033337 x^3 + 0.0000338078 x^4

label = {"5 S1A", "5 S1B", "5 S2A", "5 S2B", "5 S3A", "5 S3B", 
   "7 S1A", "7 S1B", "7 S2A", "7 S2B", "10YS1A", "10YS1B", "10YS2A", 
   "10YS2B", "10YS3A", "10YS3B", "10XS1A", "10XS1B", "10XS2A", 
   "10XS2B", "10XS3A", "10XS3B", "20YS1A", "20YS1B", "20YS2A", 
   "20YS2B", "20XS1A", "20XS1B", "20XS2A", "20XS2B", "27 S1A", 
   "27 S1B", "27 S2A", "27 S2B", "27 S3A", "27 S3B", "30 S1A", 
   "30 S1B", "30 S2A", "30 S2B", "30 S3A", "30 S3B"};

CombineBarchart = BarChart[{Data1}, ChartStyle -> "Pastel", 
  PlotLabel -> "Individual Resistance per Length Value at 0.2 Strain Condition ",
  AxesLabel -> {"Sample Length",  "(ΔkΩ/(n*cm)"}, 
  ChartLabels -> Placed[Rotate[#, 90 Degree] & /@ label, Axis], 
  Frame -> All, PlotRange -> All]
$\endgroup$

2 Answers 2

7
$\begingroup$

Just add the Epilog -> {Black,Line@Table[{x, fdata4}, {x, 1, Length@Data1}]} after your PlotRange definition enter image description here

You can change the line style definition like usually in Graphics.

$\endgroup$
1
  • $\begingroup$ Thank you i will give it a try. $\endgroup$
    – ljsg
    Jul 12, 2018 at 7:39
2
$\begingroup$

Update: You can also use the option Joined with a function setting as follows:

BarChart[Data1, ChartStyle -> "Pastel", 
   PlotLabel -> " Individual Resistance per Length Value at 0.2 Strain Condition ",
   ChartLabels -> Placed[Rotate[#, 90 Degree] & /@ label, Axis], 
   AxesLabel -> {"Sample Length", "(ΔkΩ/(n*cm)"},
   Joined -> {{Mean[#1[[1]]], fdata4 /. x -> Mean[#1[[1]]]} &}] /. 
 Line[x_] /; Length[x] > 2 :> {Red, Thick, Line[x]}

enter image description here

Original answer:

CombinedBarchart2 = BarChart[{Data1}, ChartStyle -> "Pastel", 
   PlotLabel -> " Individual Resistance per Length Value at 0.2 Strain Condition ", 
   ChartLabels -> Placed[Rotate[#, 90 Degree] & /@ label, Axis],
   Frame -> True, 
   FrameStyle -> {{Automatic, Automatic}, {Opacity[0], Automatic}}, 
   PlotRange -> All];

Labeled[Show[CombinedBarchart2, 
    ListLinePlot[Table[fdata4, {x, 1, Length @ Data1}], PlotStyle -> Thick]], 
  {Style["Sample Length", 12, "Panel", ShowStringCharacters -> False], 
   Rotate[Style["(ΔkΩ/(n*cm)", 12, "Panel", ShowStringCharacters -> False], 90 Degree]},

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you i will give it a try. $\endgroup$
    – ljsg
    Jul 12, 2018 at 7:39

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.