How to Overlay Curve Fit Over Bar Plot Without Overriding Data?
24 views (last 30 days)
Show older comments
Confused Student
on 1 Aug 2021
Commented: Star Strider
on 2 Aug 2021
I'm trying to plot an exponential curve fit on top of this bar chart that I've got, but even when I attempt to use hold on or organize the code a different way it seems to either compress all the data into two separate bins or overrides the data entirely. In essence, I'm trying to predict data three years "into the future", but can't visualize it on the same image.
Here's a simplified version of my code...
Year = 2013:2019;
Video_Cost = [11.09, 17.8, 22.35, 30.06, 37.29, 50.22, 64.83];
Audio_Cost = [3.19, 3.61, 4.75, 6.13, 8.4, 11.54, 14.22];
Total_Cost = Video_Cost + Audio_Cost;
Matrix_Costs = [Video_Cost; Audio_Cost];
figure('Units', 'Normalized', 'Outerposition', [0 0 1 1])
bar(Matrix_Costs.', 'stacked');
hold on
[Curve_Fit, Goodness] = fit(Year', Total_Cost', 'exp1');
xlim([2013, 2022])
hold on
plot(Curve_Fit, 'predobs')
Do I have to use a work around for the bar function here? Or do I need to do some exponential fit using polyfit and polyval with logs? I'm entirely lost so any help would be appreciated.
0 Comments
Accepted Answer
Star Strider
on 1 Aug 2021
Include the independent variable ‘Year’ in the bar call, and change the xlim values:
Year = 2013:2019;
Video_Cost = [11.09, 17.8, 22.35, 30.06, 37.29, 50.22, 64.83];
Audio_Cost = [3.19, 3.61, 4.75, 6.13, 8.4, 11.54, 14.22];
Total_Cost = Video_Cost + Audio_Cost;
Matrix_Costs = [Video_Cost; Audio_Cost];
figure('Units', 'Normalized', 'Outerposition', [0 0 1 1])
bar(Year, Matrix_Costs.', 'stacked');
hold on
[Curve_Fit, Goodness] = fit(Year', Total_Cost', 'exp1');
xlim([2012.5, 2022])
hold on
plot(Curve_Fit, 'predobs')
See if that does what you want.
.
6 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!