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

25 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Star Strider
Star Strider am 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 Kommentare
Confused Student
Confused Student am 2 Aug. 2021
Bearbeitet: Confused Student am 2 Aug. 2021
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!
Star Strider
Star Strider am 2 Aug. 2021
Thank you!
As always, my pleasure!
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by