# Plot and bar graphs with independent axis

9 visualizzazioni (ultimi 30 giorni)
Danilo M il 8 Dic 2016
Commentato: Iddo Weiner il 8 Dic 2016
I need to make a graph with one plot axis and one bar axis upside down, like this image:
There's an easy way to do that on Matlab?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (3)

dpb il 8 Dic 2016
Not too difficult, no...
hAx(1)=axes('XAxisLocation','top',...
'YDir','reverse');
bar(rand(10,1))
set(hAx(1),'ytick',[])
ylim(hAx(1),[0 10])
hAx(2)=axes('position',get(hAx(1),'position'),'color','none');
line([1:10].',rand(10,1),'color','r')
xlim(hAx(2),xlim(hAx(1)))
results in something of the sort...
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Iddo Weiner il 8 Dic 2016
Here's a trick for doing precisely what you are looking for, run the example:
data1 = randi([0,100],100,1); %for plot()
data2 = randi([0,10],100,1); %for bar()
% this is the easy part
plot(1:100, data1,'-r*')
hold on
% here comes the tricky part
data2_complement = 100 - data2;
data2 = [data2_complement data2];
b = bar(data2,'stacked');
b(1).FaceAlpha = 0;
b(1).EdgeAlpha = 0;
In words - what happens here is the following: 1. You plot the data set you want on the top, as a stacked bar, ranging the entire span of the yaxis limits. Do so by adding to your real vector a complementary vector that holds up space (in my example this was data2_complement). 2. Now all you need to do is set the Alpha (transparency) to zero so you won't actually see this complementary vector
Hope this helps
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Iddo Weiner il 8 Dic 2016

Accedi per commentare.

Danilo M il 8 Dic 2016
Tks guys! But there's a way to put the label axis for the bar on the right side? Cause the plot data are on the scale of hundred, and the bar data are decimals
##### 2 CommentiMostra NessunoNascondi Nessuno
dpb il 8 Dic 2016
Modificato: dpb il 8 Dic 2016
Sure...for my above example
hAx(1)=axes('XAxisLocation','top',...
'YAxisLocation','right',...
'YDir','reverse');
Or, given that you want that, use plotyy or the new-fangled HG2 version of same (I forget its name/syntax not having HG2)
hAx=plotyy(xL,yL,xR,yR,@plot,@bar);
set(hAx(2),'Ydir','reverse','XAxisLoc','top') % reverse,move RH axes
where L/R are LH and RH datasets, respectively.
Iddo Weiner il 8 Dic 2016
Precisely, excellent answer. That is the best way, I agree

Accedi per commentare.

### Categorie

Scopri di più su Line Plots in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by