Plot legend in a bar graph

6 ビュー (過去 30 日間)
Ashfaq Ahmed
Ashfaq Ahmed 2022 年 6 月 30 日
コメント済み: Voss 2022 年 6 月 30 日
Hi all,
Can anyone please tell me how can I plot the average of the bar (values of Y) in the bar graph? It can be a legend, or can be a line parallel to X axis. I just need to show the average, that's all! I am using this simple code.
clear all; close all; clc;
hFig = figure(1);
Month = {'April'; 'May'; 'June'; 'July'; 'August'; 'September'; 'October'; 'November'; 'December';'January'; 'February'; 'March'; 'April'; 'May'; 'June'};
Money_Sent = [0, 0, 568, 0, 0, 15, 1513, 583, 0, 254, 0, 1970, 0, 78, 228];
bar(Money_Sent);
grid on;
xticklabels(Month); ylim([0 2500])
hFig.WindowState = 'maximized';
text(1:length(Money_Sent),Money_Sent,num2str(Money_Sent'),'vert','bottom','horiz','center','FontSize',25);
all_money = sum(Money_Sent);
Average_HomeMoneySent = all_money/length(Month);
Thank you for your time.

採用された回答

Star Strider
Star Strider 2022 年 6 月 30 日
編集済み: Star Strider 2022 年 6 月 30 日
hFig = figure(1);
Month = {'April'; 'May'; 'June'; 'July'; 'August'; 'September'; 'October'; 'November'; 'December';'January'; 'February'; 'March'; 'April'; 'May'; 'June'};
Money_Sent = [0, 0, 568, 0, 0, 15, 1513, 583, 0, 254, 0, 1970, 0, 78, 228];
mean_Money_Sent = mean(Money_Sent);
bar(Money_Sent);
yline(mean_Money_Sent, '-r', 'LineWidth',2, 'DisplayName','Mean of Money_Sent')
grid on;
xticklabels(Month); ylim([0 2500])
hFig.WindowState = 'maximized';
text(1:length(Money_Sent),Money_Sent,num2str(Money_Sent'),'vert','bottom','horiz','center','FontSize',25);
all_money = sum(Money_Sent);
Average_HomeMoneySent = all_money/length(Month);
The yline functionwas introduced in R2018b.
An equivalent approach would be:
hold on
plot(xlim, [1 1]*mean_Money_Sent, '-r', 'LineWidth',2, 'DisplayName','Mean of Money Sent')
hold off
.
  2 件のコメント
Ashfaq Ahmed
Ashfaq Ahmed 2022 年 6 月 30 日
Hi!
Excellent! I was exactly looking for something like this. But can you please tell me how can I actually show the value of the mean in the figure?
Star Strider
Star Strider 2022 年 6 月 30 日
As always, my pleasure!
There are at least two options:
Month = {'April'; 'May'; 'June'; 'July'; 'August'; 'September'; 'October'; 'November'; 'December';'January'; 'February'; 'March'; 'April'; 'May'; 'June'};
Money_Sent = [0, 0, 568, 0, 0, 15, 1513, 583, 0, 254, 0, 1970, 0, 78, 228];
mean_Money_Sent = mean(Money_Sent);
hFig = figure(1);
bar(Money_Sent, 'DisplayName','Money Sent');
yline(mean_Money_Sent, '-r', 'LineWidth',2, 'DisplayName',sprintf('Mean of Money\\_Sent = %.2f',mean_Money_Sent))
grid on;
xticklabels(Month); ylim([0 2500])
hFig.WindowState = 'maximized';
text(1:length(Money_Sent),Money_Sent,num2str(Money_Sent'),'vert','bottom','horiz','center','FontSize',25);
legend('Location','NW')
figure
bar(Money_Sent, 'DisplayName','Money Sent');
yline(mean_Money_Sent, '-r', 'LineWidth',2, 'DisplayName',sprintf('Mean of Money\\_Sent = %.2f',mean_Money_Sent))
grid on;
xticklabels(Month); ylim([0 2500])
hFig.WindowState = 'maximized';
text(1:length(Money_Sent),Money_Sent,num2str(Money_Sent'),'vert','bottom','horiz','center','FontSize',25);
text(1, mean_Money_Sent, sprintf('\\leftarrow Mean of Money Sent = %.2f',mean_Money_Sent), 'Horiz','left', 'Vert','bottom', 'Rotation',75)
all_money = sum(Money_Sent);
Average_HomeMoneySent = all_money/length(Month);
The first option uses the 'DisplayName' property with the legend, that I augmented to show the value as well in this example.
The second option uses a text object to label the line, and omits the legend call.
.

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

その他の回答 (1 件)

Voss
Voss 2022 年 6 月 30 日
Month = {'April'; 'May'; 'June'; 'July'; 'August'; 'September'; 'October'; 'November'; 'December';'January'; 'February'; 'March'; 'April'; 'May'; 'June'};
Money_Sent = [0, 0, 568, 0, 0, 15, 1513, 583, 0, 254, 0, 1970, 0, 78, 228];
Average_HomeMoneySent = mean(Money_Sent);
A legend:
hFig = figure;
bar(Money_Sent);
grid on;
xticklabels(Month); ylim([0 2500])
hFig.WindowState = 'maximized';
text(1:length(Money_Sent),Money_Sent,num2str(Money_Sent'),'vert','bottom','horiz','center','FontSize',25);
legend(line('LineStyle','none'), ...
sprintf('Avg: %.2f',Average_HomeMoneySent), ...
'Location','NorthWest', ...
'FontSize',15);
An annotation:
hFig = figure;
bar(Money_Sent);
grid on;
xticklabels(Month); ylim([0 2500])
hFig.WindowState = 'maximized';
text(1:length(Money_Sent),Money_Sent,num2str(Money_Sent'),'vert','bottom','horiz','center','FontSize',25);
annotation('textbox',[0.15 0.81 0.2 0.1], ...
'String',sprintf('Avg: %.2f',Average_HomeMoneySent), ...
'FitBoxToText','on', ...
'FontSize',15, ...
'BackgroundColor','w');
A horizontal line with text:
hFig = figure;
bar(Money_Sent);
grid on;
xticklabels(Month); ylim([0 2500])
hFig.WindowState = 'maximized';
text(1:length(Money_Sent),Money_Sent,num2str(Money_Sent'),'vert','bottom','horiz','center','FontSize',25);
yline(Average_HomeMoneySent,'r','LineWidth',2);
text(0,Average_HomeMoneySent,sprintf('Avg: %.2f',Average_HomeMoneySent), ...
'vert','bottom', ...
'horiz','center', ...
'FontSize',15, ...
'BackgroundColor','w', ...
'Color','r', ...
'Rotation',55);
  2 件のコメント
Ashfaq Ahmed
Ashfaq Ahmed 2022 年 6 月 30 日
This is amazing! Thank you so much for such an important help @Voss
Voss
Voss 2022 年 6 月 30 日
You're welcome!

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

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by