Plot legend in a bar graph
조회 수: 86(최근 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.
댓글 수: 0
채택된 답변
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);
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
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.
.
추가 답변(1개)
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);
참고 항목
범주
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!