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

27 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Star Strider
Star Strider el 1 de Ag. de 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 comentarios
Confused Student
Confused Student el 2 de Ag. de 2021
Editada: Confused Student el 2 de Ag. de 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 el 2 de Ag. de 2021
Thank you!
As always, my pleasure!
.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by