Plot legend in a bar graph
8 次查看(过去 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);
另请参阅
类别
在 Help Center 和 File 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!