Effacer les filtres
Effacer les filtres

Create a bar graph where the next bar starts where the previous ends

6 vues (au cours des 30 derniers jours)
Maria Ali
Maria Ali le 31 Déc 2021
Commenté : Maria Ali le 1 Jan 2022
I have a data set where I have alternatingly positive and negative values for each year. I need to plot them on a bar chart but the 2nd bar must start where the first bar ends. I cannot make it work with changing basevalue and can't find anyother option.

Réponse acceptée

Matt J
Matt J le 31 Déc 2021
Modifié(e) : Matt J le 31 Déc 2021
For example,
y = [4 2 3;
1 6 5]';
n=size(y,1);
dy=([1,-1].*y).';
yc=[0, repelem(cumsum(dy(:).'),2)];
ymin=min(yc(:));
lims=sort(reshape(yc(1:end-1),2,[]))-ymin;
ys=[lims(1,:); diff(lims)]';
h=bar(ys,'stacked','FaceColor','flat','ShowBase',0);
h(1).Visible='off';
h(2).CData(1:2:end,:)=repmat( [0 0 1] ,n,1);
yticklabels( str2double(yticklabels) +ymin + "")
  7 commentaires
Maria Ali
Maria Ali le 1 Jan 2022
well when i simply run this code it, the two mentioned lines open up a new figure. I changed the following code
h=bar(ys,'stacked','FaceColor','flat','ShowBase',0);
to
h = bar(app.Bar_tab_16, ys,'stacked','FaceColor','flat','ShowBase',0);
where app.Bar_tab_16 is the name of the axes where i am plotting my chart. But since i don't know how to change the remaining two lines so that I can add the names of the axes, it opens up a new figure and the plot is also different in app designer than simply running it in script file.
Maria Ali
Maria Ali le 1 Jan 2022
It's resolved now. Thanks all.

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 31 Déc 2021
I think you'll have to use the patch() function to create rectangular patches "floating" off the axis.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Tags

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by