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 CenterFile Exchange에서 Specifying 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