Plot legend in a bar graph
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ashfaq Ahmed
am 30 Jun. 2022
Kommentiert: Voss
am 30 Jun. 2022
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 Kommentare
Akzeptierte Antwort
Star Strider
am 30 Jun. 2022
Bearbeitet: Star Strider
am 30 Jun. 2022
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 Kommentare
Star Strider
am 30 Jun. 2022
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.
.
Weitere Antworten (1)
Voss
am 30 Jun. 2022
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 Kommentare
Siehe auch
Kategorien
Mehr zu Specifying Target for Graphics Output finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!