How to Overlay Curve Fit Over Bar Plot Without Overriding Data?

26 次查看(过去 30 天)
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.

采纳的回答

Star Strider
Star Strider 2021-8-1
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 个评论
Confused Student
Confused Student 2021-8-2
编辑:Confused Student 2021-8-2
Ah yes it does, I appreciate your work!
After looking around for a bit, I noticed that the newer version of Matlab I installed yesterday (to replace the R2015b version I had on my laptop) had nice looking data cursors, so I decided to use them for my project presentation instead for some added visual clarity.
I will definitely use this on the Matlab file I send in for my final submission though.
Thanks!

请先登录,再进行评论。

更多回答(0 个)

标签

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by