The following commands produce some very strange results -
plotyy(1:3,2:4,3:5,4:6)
hold on
plotyy(1:3,2.1:4.1,3:5,4.1:6.1)
I basically want to plot two different series on the left y axes and two more series on the right y axes. The above commands work fine for the left series, but produce weird results for the right one. The second green line doesn't look like it should.
The problem that you are having is related to the way that the plotyy creates they plot. plotyy creates two different axes that it plots on, and then mounts them into a single figure. When you issue the hold on command, you are only freezing one of the axes. To fix this, you need to hold each one individually, and then plot back onto them using the plot command.
[ax,hl,hr] = plotyy(1:3,2:4,3:5,4:6);
hold(ax(1), 'on')
hold(ax(2), 'on')
plot(ax(1), 1:3,2.1:4.1)
plot(ax(2), 3:5,4.1:6.1)
Indeed pretty weird behavior. For fun, select the 'hand' tool in the plot window and then drag the graph around, you see that only one of the two green curves moves and that on the right side there are two sets of labels drawn on top of each other. I would qualify this as a bug in matlab (far from the only ugly behavior in Matlab's plots). This seems to be a workaround for what you want to achieve:
[AX, H1, H2] = plotyy(1:3, [2:4;2.1:4.1], 3:5,[4:6;4.1:6.1]);
>> set(H1, 'color','b')
>> set(H2, 'color','g')
Note that this only works if the two left plots have the same set of x-values, and similar for the right plots, like in your case. A=[4000;0;1]. But this is a workaround, the real solution is given by slbass.
Related
Consider the following code:
x = 0:0.1:pi;
y = sin(x);
plot(x,y)
I want to switch the display of this plot, such that x displays on the vertical axis and y displays on the horizontal axis.
Obviously for this example, the easiest way is to plot(y,x). However in my actual code I'd have a ton of plot calls to edit among several functions, and I want to switch back and forth easily. It's a bird's eye East-North plot, and some experiment geometries don't come out very well with East on the x axis.
Thanks in advance!
what you want is to toggle between views after selecting the axes of you plot.
For example, say you run your code and have a bunch of plots on the screen. You choose the plot you want to flip by clicking on the empty space within the plot box. Then you type in the command line some function name that is designed to flip the plot.
This function can be view ( view(90,90) or view(0,90) ), or a generic function such as:
function flipplot
h=get(gca);
xd=h.Children.XData;
yd=h.Children.YData;
h.Children.XData=yd;
h.Children.YData=xd;
end
you can save that flipplot function as flipplot.m, then each time you do the above (select a plot with your mouse etc) and enterflipplot on the command line you get what you want.
I would really appreciate some help.
I have to independent datasets. Each dataset contains two variables: dates (as datenumber) and corresponding data. I need to plot both datasets on one scatter plot with dates on x-axis and two y-axes. I have been trying with the following code:
figure(1);
scatter(x1,y1,'g');
set(gca);
ax1=gca;
set(ax1,'YColor','g');
ax2 = axes('Position',get(gca,'Position'),'YAxisLocation','right', XTick'[],'Color','none','YColor','r');
hold on; scatter(x2,y2,'r');
Now, this gives correct y-axis on the right side, however on the right side I end up with two overlapping y-axes.
Also, I need to change x-axis so that it displays dates as opposed to datenumbers. I've tried to incorporate datetick into the code but it again gives me two overlapping x-axes.
Does anyone know how to go about it?
thank you
I tried your script with your sample input and found no problems. Anyway, here's a solution which uses the matlab function plotyy, which is suited to simple plots like this:
%generate input
x1=[732490 732509 732512 732513 732521 732528];
y1=[7.828 7.609 22.422 14.758 26.258 1.477];
x2=[732402 732403 732404 732404 732433];
y2=[0.693 0.645 0.668 0.669 0.668];
figure(1);
[ax, h1,h2]=plotyy(x1,y1,x2,y2,'scatter');
%set colors manually
green=[0 1 0];
red=[1 0 0];
set(h1,'cdata',green);
set(h2,'cdata',red);
set(ax(1),'ycolor',green);
set(ax(2),'ycolor',red);
%note the 'keepticks' and 'keeplimits' options
datetick(ax(1),'x','yyyy-mm-dd','keepticks','keeplimits');
datetick(ax(2),'x','yyyy-mm-dd','keepticks','keeplimits');
Without the datetick call the plotyy function synchronizes the xticks in the plot. When you call datetick, it recalculates the ticks, unless you explicitly tell it not to, see the option keepticks, and this is seen as two sets of x axes (even though the x coordinates are the same, the ticks are located at different positions). The keeplimits option is needed to preserve the original xlim. It obviously needs some more manual work to get plots like this sufficiently pretty.
Also note that I set the axes and data colors manually: there might be a way to do this more elegantly.
Update: keeplimits originally missing
Update2: changed sample data to correspond to updated question comment
I want to draw two graphs in Matlab with different colors. Then I want a box in the upper right corner which names each of the two graphs. The code that I am writing is:
x=1:1:max
%err_t_coupled,err_t_uncoupled are arrays
figure
plot(x, err_t_coupled,'red',x, err_t_uncoupled,'blue')
legend('uncoupled','coupled','Location','Northeast')
title('Maximum error')
xlabel('Iterations')
ylabel('Maximum error wrt D-Norm')
It produces the desired graph. However in the top right corner, it draws a red line for both coupled and uncoupled. I instead want red for coupled and blue for uncoupled. Any solutions?
The problem has to do with the fact that err_t_coupled and err_t_uncoupled are arrays, not vectors.
This will work:
x=1:1:max
%err_t_coupled,err_t_uncoupled are arrays
figure
h1 = plot(x, err_t_coupled,'red');
hold on
h2 = plot(x, err_t_uncoupled,'blue');
legend([h1(1) h2(1)], 'coupled','uncoupled','Location','Northeast')
title('Maximum error')
xlabel('Iterations')
ylabel('Maximum error wrt D-Norm')
The MATLAB surf plot below is essentially two plots plotted adjacent to each other. For clarity, I have included some code below used to prepare the plot:
band1 = horzcat(band1, eSurface2(:,:,1));
band2 = horzcat(band2, eSurface2(:,:,2));
surf(band2,'DisplayName','band2');
surf(band3,'DisplayName','band2');
I would like for the y axis numbering to restart at the start of the second graph. How do I go about doing that?
You can use the 'YTick' and 'YTickLabel' properties of the axis to control the ticks, this way you can make it start from zero for the second graph. It will require some trail and error to get it right. See the relevant doc here (you'll have to scroll all the way to the bottom of the page).
Take advantage of the following feature of 'YTickLabel': "If you do not specify enough text labels for all the tick marks, MATLAB uses all of the labels specified, then reuses the specified labels".
Usually when I plot in MATLAB, it always draws on the same figure. How do I make it draw in a new figure?
I know it is pretty elementary, but I'm not finding it using Google Search.
figure;
plot(something);
or
figure(2);
plot(something);
...
figure(3);
plot(something else);
...
etc.
While doing "figure(1), figure(2),..." will solve the problem in most cases, it will not solve them in all cases. Suppose you have a bunch of MATLAB figures on your desktop and how many you have open varies from time to time before you run your code. Using the answers provided, you will overwrite these figures, which you may not want. The easy workaround is to just use the command "figure" before you plot.
Example: you have five figures on your desktop from a previous script you ran and you use
figure(1);
plot(...)
figure(2);
plot(...)
You just plotted over the figures on your desktop. However the code
figure;
plot(...)
figure;
plot(...)
just created figures 6 and 7 with your desired plots and left your previous plots 1-5 alone.
The other thing to be careful about, is to use the clf (clear figure) command when you are starting a fresh plot. Otherwise you may be plotting on a pre-existing figure (not possible with the figure command by itself, but if you do figure(2) there may already be a figure #2), with more than one axis, or an axis that is placed kinda funny. Use clf to ensure that you're starting from scratch:
figure(N);
clf;
plot(something);
...
As has already been said: figure will create a new figure for your next plots. While calling figure you can also configure it. Example:
figHandle = figure('Name', 'Name of Figure', 'OuterPosition',[1, 1, scrsz(3), scrsz(4)]);
The example sets the name for the window and the outer size of it in relation to the used screen.
Here figHandle is the handle to the resulting figure and can be used later to change appearance and content. Examples:
Dot notation:
figHandle.PaperOrientation = 'portrait';
figHandle.PaperUnits = 'centimeters';
Old Style:
set(figHandle, 'PaperOrientation', 'portrait', 'PaperUnits', 'centimeters');
Using the handle with dot notation or set, options for printing are configured here.
By keeping the handles for the figures with distinc names you can interact with multiple active figures. To set a existing figure as your active, call figure(figHandle). New plots will go there now.
Another common option is when you do want multiple plots in a single window
f = figure;
hold on
plot(x1,y1)
plot(x2,y2)
...
plots multiple data sets on the same (new) figure.
As simple as this-
figure, plot(yourfigure);