Borrar filtros
Borrar filtros

Matlab bar chart plotting issue

3 visualizaciones (últimos 30 días)
peter huang
peter huang el 24 de Jul. de 2023
Comentada: Star Strider el 25 de Jul. de 2023
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.

Respuestas (2)

Star Strider
Star Strider el 24 de Jul. de 2023
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 comentarios
Dyuman Joshi
Dyuman Joshi el 25 de Jul. de 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
Star Strider el 25 de Jul. de 2023
@peter huang — My pleasure!

Iniciar sesión para comentar.


Raheel Naveed
Raheel Naveed el 24 de Jul. de 2023
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

Categorías

Más información sobre Graphics Objects en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by