Why does plotyy work when I use area and bar graphs but not when I use line and bar graphs?

4 次查看(过去 30 天)
So I am essentially making rainfall runoff plots, so an inverted hyetograph (rainfall) on one y axis and flow on the other. Now plotyy works great when I use an area function to describe the flow and a bar graph for the rainfall. However, when I change the word area to plot or line in the code, it makes the hydrograph look funny and screws up the bar graph. The bars are no longer closed over the appropriate time which causes the FaceColor fill to drag across the screen, its weird. Now I have worked around this, by using area plots with no fill, but I would like to understand what is happening? Here are small excerpts of the data and the code I use:
Flow93AD: This is hourly datenumbers for x values and flow rates for y values x = [728150.8333333; 728150.875000;728150.9166667;728150.9583333;728151.0000000;728151.0416667;728151.0833333;728151.1250000;728151.1666667]
y = [259.383703193410; 258.723409532005; 258.289898463849; 257.980320845167; 257.575066143656; 257.158211988047; 360.562036053458; 1992.39612523645; 4211.89982677947;]
Rain93: Uses the same date x-values as above and the y values are rainfall in inches:
y = [0; 0; 0.300000000000000; 1.40000000000000; 1.40000000000000; 0.100000000000000; 0.100000000000000; 0; 0;]
Here is the code:
x1 = datenum(1993,8,9); % Start date of the graph display
x2 = datenum(1993,8,19); % End date of the graph display
D1 = datenum(1993,8,1);
D2 = datenum(1993,8,30);
Selector = AllDrained(:,1)>=D1 & AllDrained(:,1) < D2;
Flow93AD = AllDrained(Selector,:);
Rain93 = Raindata(Selector,1:2);
Flow93AUD = AllUndrained(Selector,:);
Flow93Orig = OriginalDrainage(Selector,:);
[AX,H1,H2]=plotyy(Rain93(:,1),Rain93(:,2),Flow93AD(:,1),Flow93AD(:,2),'bar','area');
set(AX(1),'xlim',[x1,x2],'xtick',x1:x2,'FontWeight','Bold','TickDir','out'); % Sets the precipdata window, ticks and axis label properties
set(AX(1),'xtick',[]); % Removes the top plot ticks
set(AX(1),'ydir','rev','ylim',[0,3],'ytick',0:0.5:3); % Inverts the precip y axis and sets the labels
set(AX(1),'Box','Off'); % Turns of the right side tick marks from the precip axis.
set(AX(2),'xlim',[x1,x2],'xtick',x1:x2,'ylim',[0,10000],'ytick',0:1000:10000,'FontWeight','Bold','TickDir','out'); % Sets the soil hydrograph window, ticks and axis label properties
datetick(AX(1),'keeplimits'); % This changes the tick labels to dates
datetick(AX(2),'keeplimits');
set(get(AX(1),'Ylabel'),'String','Precipitation (in)','FontSize',12,'FontWeight','Bold'); % Sets the Y-axis label
set(get(AX(2),'Ylabel'),'String','Discharge (cfs)','FontSize',12,'FontWeight','Bold'); % Sets the Y-axis label
set(get(AX(1),'Xlabel'),'String','Date','FontSize',12,'FontWeight','Bold'); % Sets the Y-axis label
set(H1,'DisplayName','Precip','FaceColor','b');
set(H2,'DisplayName','Drained','FaceColor','none','EdgeColor','g');
legend('show','Location','West'); % Turns the legend on
title('August 1993 Hydrograph for the One Soil All Drained Scenario','FontSize',14); % Creates a title
Once I change the word from area to plot in the plotyy function, everything screws up. Any thoughts? Thanks
Brandon

采纳的回答

Brandon Sloan
Brandon Sloan 2013-7-10
As Duane pointed out to me after looking at the data, it was my renderer that was the problem. I changed the renderer from zbuffer to painter and everything worked fine. Then I changed it back to zbuffer and that worked fine, so something just needed to be reset with my computer. Here is the following line to change the renderer:
set(gcf,'renderer','painter')
Thanks again Duane!

更多回答(1 个)

dpb
dpb 2013-7-8
编辑:dpb 2013-7-8
I simplified your case somewhat for diagnostic purposes. The following snippet draws the essence of your original...
MATL
dn=datenum(1993,8,9,20+[0:8]',0,0);
y1 = [0; 0; 0.3; 1.4; 1.4; 0.1; 0.1; 0; 0;];
y2 = [259.4, 258.7, 258.3, 258.0, 257.6, 257.1, 360.6, 1992.4, 4211.9];
x1 = datenum(1993,8,9); % Start date of the graph display
x2 = datenum(1993,8,19); % End date of the graph display
[AX,H1,H2]=plotyy(dn,y1,dn,y2,@bar,@area);
set(AX(1),'xlim',[x1,x2],'xtick',x1:x2,'TickDir','out');
set(AX(1),'ydir','rev','ylim',[0,3],'ytick',0:0.5:3);
set(AX(1),'Box','Off');
set(AX(2),'xlim',[x1,x2],'xtick',[x1:x2], 'xticklab',[],...
'ylim',[0,10000],'ytick',0:1000:10000, ...
'TickDir','out');
set(AX,'fontsize',9)
datetick(AX(1),'keeplimits','keepticks');
datetick(AX(2),'keeplimits','keepticks');
ylabel(AX(1),'Precipitation (in)');
ylabel(AX(2),'Discharge (cfs)');
xlabel(AX(1),'Date');
set(H1,'DisplayName','Precip','FaceColor','b'); set(H2,'DisplayName','Drained','FaceColor','none','EdgeColor','g');
legend('show','Location','West');
I then added a logic flag and branch to conveniently be able to flip between the two choices for the RH axis style as...
MATL
lplot=false; % a logic variable to choose between the two...
dn=datenum(1993,8,9,20+[0:8]',0,0);
y1 = [0; 0; 0.3; 1.4; 1.4; 0.1; 0.1; 0; 0;];
y2 = [259.3; 258.7; 258.3; 258.0; 257.6; 257.2; 360.6; 1992.4; 4211.9;];
x1 = datenum(1993,8, 9); % Start date of the graph display
x2 = datenum(1993,8,19); % End date of the graph display
if lplot
[AX,H1,H2]=plotyy(dn,y1,dn,y2,@bar,@line);
else
[AX,H1,H2]=plotyy(dn,y1,dn,y2,@bar,@area);
end
and observed your problem w/ the colors...then's when I saw the culprit--you're setting a 'facecolor' property for the line intended only for the area plot--add the same logic around it and all is well(+)...
MATL
...
if ~lplot
set(H2,'DisplayName','Drained','FaceColor','none','EdgeColor','g');
end
legend('show','Location','West');
(+) Not sure the effect you're after w/ the line plot; at first I thought you were concerned it didn't draw a line back down to the baseline and erroneously presumed that the datenum resolution on the x-axis wasn't sufficient to show it up--looking at the y2 data that is increasing removed that.
Oh, one last little nit that's often useful w/ plotyy---occasionally there's enough roundoff in the location that the x-axis ticklabels don't quite overlap and so look "fuzzy". I generally as above set the 'xticklabel' property on the RH axis to the empty set so they don't get drawn. In fact, I have never found any reason for them to be on for both axes simultaneously so I have modified my installation to do that inside plotyy since TMW hasn't seen fit to incorporate it as an enhancement per my suggestion.
  2 个评论
Brandon Sloan
Brandon Sloan 2013-7-8
Hey, thanks for the quick response. In my code, when I changed to line I would remove the face color. I ran your code and got it to work for the small excerpt of data, but I was curious why the tick marks where not located in the midpoint of the bars. Then, I ran your code with my larger set of data on got the same errors. The only mod I made to your code was that I used the first column of my two time series vectors which are datenumbers so I don't think it should matter. Here is what the code looks like with my full dataset:
x1 = datenum(1993,8, 9); % Start date of the graph display
x2 = datenum(1993,8,19); % End date of the graph display
[AX,H1,H2]=plotyy(Rain93(:,1),Rain93(:,2),Rain93(:,1),Flows93AD(:,2),@bar,@line);
set(H1,'DisplayName','Precip','FaceColor','b');
set(H2,'DisplayName','Drained','color','red');
set(AX(1),'ydir','rev','ylim',[0,3],'ytick',0:0.5:3);
set(AX(1),'xlim',[x1,x2],'xtick',x1:x2,'FontWeight','Bold','TickDir','out');
set(AX(2),'xlim',[x1,x2],'xtick',x1:x2,'FontWeight','Bold','TickDir','out');
I wish I could figure out a way to send you an image on here. It looks like the bar graphs are not closing and when it fills, there is a large fill triangle going to a previous unclosed box. Then, for some reason, it slightly messes up the hydrograph shape.
Let me know if there is anything I can send to you to help you understand the issue and thanks again for your help
dpb
dpb 2013-7-8
编辑:dpb 2013-7-8
What does the 'Image' icon do then?
Have you thoroughly vetted the datasets to make sure it's not something real in them?
As for what to send, can you isolate the problem to a given bar or set of bars that's fairly short-enough dataset to post?
Oh, what happens if you just do bar() alone...I presume it's got the same problem and it's not really anything to do w/ plotyy()?
Try one of the freebie hosting sites for an image, I guess...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Two y-axis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by