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

1 回表示 (過去 30 日間)
Confused Student
Confused Student 2021 年 8 月 1 日
コメント済み: Star Strider 2021 年 8 月 2 日
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!
Star Strider
Star Strider 2021 年 8 月 2 日
Thank you!
As always, my pleasure!
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSmoothing についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by