plotyy with bars side by side

19 visualizzazioni (ultimi 30 giorni)
Gunnar
Gunnar il 27 Set 2016
Modificato: dpb il 27 Set 2016
I have two sets of measured data "y1","y2" and one reference data set "yref". I would like to plot "y1" and "y2" as bars side-by-side against the common data set "x" on the x-axis. The left hand y-axis values should be in absolute form. The right hand y-axis values should be in percentage form. How to do this?
I on the other hand need to plot bars for two data sets. I would also like the starting y-value on the left hand y-axis to be "yref", which explains the use of "ylim" in my example code below. The percentages on the right hand y-axis should also be calculated with respect to "yref".
I have made an initial attempt but without getting the desired output. Help is therefore very much appreciated.
x = 1:1:3;
y1 = [10 20 30]; % Measured data set #1
y2 = [20 40 90]; % Measured data set #2
yref = 5; % Baseline or reference data set
figure(1)
plotyy(x,y1, x,y2, 'bar') % PlotYY
yl=get(gca,'ylim')
ylim([yref yl(2)])
fcyy = get(gcf, 'Children') % Gets handles for both Y-axes
Ryax = fcyy(1); % Right axis handle
Lyax = fcyy(2); % Left axis handle
Lyaxt = get(fcyy(2), 'YTick') % Get Left axis ticks
LyaxDegC = ((Lyaxt-yref)/yref)*100; % Convert to °C (or whatever you want)
set(Ryax, 'YTick',Lyaxt, 'YTickLabel', LyaxDegC) % New Y-tick values

Risposte (1)

dpb
dpb il 27 Set 2016
Modificato: dpb il 27 Set 2016
Not sure exactly what you're trying to show, but try the following for starters--
x=x.'; y1=y1.'; y2=y2.'; % needs must have column vectors
y2pct=[(y2-yref)/yref]*100;
[hAx,hBL,hBR]=plotyy(x,[y1 nan(size(y1))], x,[nan(size(y1)) y2pct], 'bar');
set(hBL(1),'basevalue',yref)
yL=ylim(hAx(1));
ylim(hAx(1),[yref yL(2)])
legend([hBL(1) hBR(end)],'Y1','Y2Pct','location','northwest')
The above results in
The biggest "trick" with bar for two series on same x-axis is the augmentation with NaN as a placeholder so the bars aren't drawn overlaying each other but appear as if grouped...I'm guessing your definition of the percentages for the RH axis may not be what you really intended, but the point is to convert to whatever units you want plotted and plot those values instead of the raw data. To fixup the LH axes limits with a bar plot, the 'baseline' value also needs adjusting if you don't want blank space under it...to see the difference, just comment out that set command.
As always, "salt to suit"...

Categorie

Scopri di più su Two y-axis in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by