Matlab bar chart plotting issue
Show older comments
I have two sets of data and I am using the Matlab "bar" function to create a bar chart. However, I would like to enhance the plot by adding a trend line. When I tried using the "plot" function, I found that the points did not align with the bars in the bar chart. I'd like to know if, when encountering this issue, creating another corresponding array for the x-values and adjusting it is the only way to align the trend line with the bars, or if the "bar" function has built-in capabilities to directly plot a trend line.
clear all ; clc ; clf
set(gcf,'color','w')
load m_time.mat
%%
y1 = 1 : 1 : 10
y2 = [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0]
y = [y1 ;y2]
bar(m_time_112 , y )
hold on
plot(m_time_112 , y1,'o-')
plot(m_time_112 , y2,'*-')
I want the effect to be something like this: aligning the x-points of the "plot" function with the bars of the "bar" chart.

Answers (2)
Recent MATLAB releases can return the (x,y) coordinates of the bar end points.
Using those, the result improves considerably —
% type('bar_q.m')
LD = load('m_time.mat');
m_time_112 = LD.m_time_112;
y1 = 1 : 1 : 10;
y2 = [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0];
y = [y1 ;y2];
figure
hb = bar(m_time_112 , y );
xep1 = hb(1).XEndPoints;
yep1 = hb(1).YEndPoints;
xep2 = hb(2).XEndPoints;
yep2 = hb(2).YEndPoints;
hold on
plot(xep1 , yep1,'o-')
plot(xep2 , yep2,'*-')
If you have an earlier MATLAB version, this is still possible, however more complicated.
.
5 Comments
Dyuman Joshi
on 24 Jul 2023
This is the way to go.
However, @Star Strider I believe this feature was implemented quite a long while ago. I have seen release version mentioned on any feature introduced recently to a function, specifically in the documentation of bar - section Specify Axes for Bar Graph. (Though this is speculation on my part, I do not have any concrete evidence of this)
Star Strider
on 24 Jul 2023
@Dyuman Joshi — Thank you! The documentation does not say when that was added, however I believe it was a few releases ago, perhaps R2020b. Prior to that, it was necessary to use the undocumented ‘XOffset’ property for each bar group.
peter huang
on 25 Jul 2023
Dyuman Joshi
on 25 Jul 2023
@Star Strider Ah, I see. Thanks for the info!
@peter huang if this answer solved your problem, please consider accepting the answer.
Accepting the answer indicates that your problem has been solved (which can be helpful to other people in future) and it awards the volunteer with reputation points for helping you.
You can accept only 1 answer for a question, but you can vote for as many answers as you want. Voting an answer also provides reputation points.
Star Strider
on 25 Jul 2023
@peter huang — My pleasure!
clc; clear; close all;
set(gcf,'color','w')
% load m_time.mat % Load your data here
y1 = 1 : 10;
y2 = 0.1:0.1:1;
y = [y1 ;y2];
numBars = numel(y1);
numSeries = size(y, 1);
groupWidth = min(0.8, numSeries/(numSeries+1.5));
bar(y', 'BarWidth', groupWidth);
hold on
x = 1:numBars;
for i = 1:size(y, 1)
% Calculate center of each bar
xAdjusted = x - groupWidth/2 + (2*i-1) * groupWidth / (2*numSeries);
p = polyfit(x, y(i,:), 1);
y_fit = polyval(p, x);
plot(xAdjusted, y_fit, 'o-', 'LineWidth', 2);
end
hold off
1 Comment
peter huang
on 25 Jul 2023
Categories
Find more on 2-D and 3-D 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!
