I have two signals, one for each axis and taken singularly and plotted in a standard way separately they would look like this:
However they represent different measurements and I need to superimpose them by representing one on the standard X-Y axis (bottom-left) and one on a different set of coordinate axis (X = left, Y = top).
I thought of the following code:
figure(1);
line(1:128,imagesXMatrix(i,:));
ax1 = gca;
set(ax1,'XColor','r','YColor','r')
ax2 = axes('Position',get(ax1,'Position'), ...
'XAxisLocation','left','YAxisLocation','top', ...
'Color', 'none','XColor','k','YColor','k');
line(1:128,imagesYMatrix(i,:),'Parent',ax2);
but am getting the following error:
Error using axes
Bad property value found.
Object Name: axes Property
Name: 'XAxisLocation'.
which I guess means that value left is not ok for the XAxisLocation variable.
What I would really want is simply the superimposition of the two following plots, any idea on how to make it happen?
Thanks a lot!
plotyy will do the trick:
figure(1)
AX = plotyy(x1,y1,x2,y2,'plot');
set(get(AX(1),'Ylabel'),'String','y1')
set(get(AX(2),'Ylabel'),'String','y2')
and additional properties like:
set(AX(1),'ylim',[...],...)
set(AX(2),'ylim',[...],...)
and more information in the documentation.
Related
I've never seen a plot like the following (the plot in (a) ). Is it even possible?
According to the profile page of #Ander Biguri
Matlab can even make your dinner, if you know how to use it.
Which answers the question, if this is even possible ;-)
All we need is basic knowledge of the axes command - the rest is just tweaking to make it look nice. Let's have a look at it:
We'll start off by creating some sample data:
t = 100:220;
x1 = -(10*(t-130)).^2;
x2 = -(10*(t-150)).^2;
x3 = -(10*(t-170)).^2;
Then we'll create an initial figure with a white background
fig = figure(1);
set(fig,'Color','w');
Now we can create a new axes object and plot x1 on it:
ax(1) = axes('Position',[0.1,0.1,0.6,0.6]);
plot(ax(1),t,x1+10^4*rand(size(x1)),'-k',t,x1,'-r');
We'll remove the box around the axes, so only the x- and y-axes remain. Further we resize the plot, so we'll have enough space for the other two plots. We also set the color to none, i.e. transparent.
set(ax(1),'Color','none');
set(ax(1),'Box','off');
set(ax(1),'Position',[0.1,0.1,0.6,0.6]);
Now we need to create the second graph. We'll just create another axes object at a position which we like:
ax(2) = axes('Position',[0.2,0.2,0.6,0.6]);
plot(ax(2),t,x2+10^4*rand(size(x2)),'-k',t,x2,'-r');
set(ax(2),'Color','none');
set(ax(2),'Box','off');
and so on:
ax(3) = axes('Position',[0.3,0.3,0.6,0.6]);
plot(ax(3),t,x3+10^4*rand(size(x3)),'-k',t,x3,'-r');
set(ax(3),'Color','none');
set(ax(3),'Box','off');
And simple as that, we get something that doesn't even look that bad:
Using multiple waterfall plots, as Horchler suggested:
%// create some sample data
t=10:20:110;
x=0:1:200;
Y=bsxfun(#(x,t) normpdf(x,t,20),x,t.'); %//' fix the code formatting on SO!!
%// Make a colormap to to set the colour of the lines
colormap([1 0 0;0 0 0]);caxis=[0 1];
%// Plot the first set of lines (red ones)
h1=waterfall(x,t,Y,zeros(size(Y)));
set(h1,'FaceColor','none','LineWidth',2) %// tweak the properties
hold on
%// Plot the second set of lines (black lines), just the red lines with some noise
h2=waterfall(x,t,Y+0.002*(rand(size(Y))-0.5),ones(size(Y)));
set(h2,'LineWidth',2)
hold off
view([16 28])
we can get this:
Suppose that I have 2 figures in MATLAB both of which plot data of size (512x512), however one figure is being plotted by an external program which is sets the axis parameters. The other is being plotted by me (using imagesc). Currently the figures, or rather, the axes are different sizes and my question is, how do I make them equal?.
The reason for my question, is that I would like to export them to pdf format for inclusion in a latex document, and I would like to have them be the same size without further processing.
Thanks in Advance, N
Edit: link to figures
figure 1: (big)
link to smaller figure (i.e. the one whose properties I would like to copy and apply to figure 1)
For this purpose use linkaxes():
% Load some data included with MATLAB
load clown
% Plot a histogram in the first subplot
figure
ax(1) = subplot(211);
hist(X(:),100)
% Create second subplot
ax(2) = subplot(212);
Now link the axes of the two subplots:
linkaxes(ax)
By plotting on the second subplot, the first one will adapt
imagesc(X)
First, you have the following:
Then:
Extending the example to images only:
load clown
figure
imagesc(X)
h(1) = gca;
I = imread('eight.tif');
figure
imagesc(I)
h(2) = gca;
Note that the configurations of the the first handle prevail:
linkaxes(h)
1.Get the handle of your figure and the axes, like this:
%perhaps the easiest way, if you have just this one figure:
myFigHandle=gcf;
myAxHandle=gca;
%if not possible, you have to search for the handles:
myFigHandle=findobj('PropertyName',PropertyValue,...)
%you have to know some property to identify it of course...
%same for the axes!
2.Set the properties, like this:
%set units to pixels (or whatever you prefer to make it easier to compare to the other plot)
set(myFigHandle, 'Units','pixels')
set(myAxHandle, 'Units','pixels')
%set the size:
set(myFigHandle,'Position',[x_0 y_0 width height]) %coordinates on screen!
%set the size of the axes:
set(myAxHandle,'Position',[x_0 y_0 width height]) %coordinates within the figure!
Ok, based on the answer of #Lucius Domitius Ahenoba here is what I came up with:
hgload('fig1.fig'); % figure whose axis properties I would like to copy
hgload('fig2.fig');
figHandles = get(0,'Children');
figHandles = sort(figHandles,1);
ax(1) = findobj(figHandles(1),'type','axes','-not','Tag','legend','-not','Tag','Colorbar');
ax(2) = findobj(figHandles(2),'type','axes','-not','Tag','legend','-not','Tag','Colorbar');
screen_pos1 = get(figHandles(1),'Position');
axis_pos1 = get(ax(1),'Position');
set(figHandles(2),'Position',screen_pos1);
set(ax(2),'Position',axis_pos1);
This is the 'before' result:
and this is the 'after' result:
Almost correct, except that the aspect ratios are still off. Does anybody know how to equalize everything related to the axes? (I realize that I'm not supposed to ask questions when posting answers, however adding the above as a comment was proving a little unwieldy!)
I have a fairly complex plotting problem that i thought it would be interesting to get a solution to. Say i have two plots, number 1:
This plot was created using plotyy.
And number 2:
This plot was created using plot3(x, y, z, '.')
Now, the complex part is i want to take plot number 2, watermark it and put it behind plot number 1. Which would result in something like this:
Effectively what i want to show is that plot 1 is made from data that looks like plot 2. Now i haven't been able to find how to do this so it may not even be possible, but if it can be done then it would be a great tutorial to have on stack overflow!
You can do this but it will take some work to get the axes formatted so they look nice.
What you need to do is put one axes object on top of another axes; however, to prevent the top axes from occluding the bottom one you need to set the 'Color' property of the top axes object to 'none'.
Here is an example script that generates something similar to what you are looking for
f = figure;
axes();
x = rand(100,3)*3 + 3;
plot3(x(:,1), x(:,2), x(:,3),'.');
axes('Color', 'none');
x = -5:5;
y = x.^2;
line(x,y, 'Color', 'r', 'LineWidth', 2);
Here is the resulting figure:
If you don't like how this works out you can try to project your 3D data into 2D and then draw that projection as an image behind your lines. Here is a link to a discussion about how you might go about creating the 2D projection.
I am using plotyy to plot two vectors on different y-axes. I wish to add a third vector to one of the two axes. Can someone please tell me why the following code is not working?
[ax h1 h2] = plotyy(1:10,10*rand(1,10),1:10,rand(1,10));
hold on; plot(ax(2),1:10,rand(1,10));
??? Error using ==> plot
Parent destroyed during line creation
I simply wish to add an additional vector to one of the axes (ax(1),ax(2)) created by plotyy.
Apply hold to the axis of interest.
[ax h1 h2] = plotyy(1:10,10*rand(1,10),1:10,rand(1,10));
hold(ax(2), 'on');
plot(ax(2),1:10,rand(1,10));
plotyy works by creating two axes, one on top of the other. You are carefully adding the new vector to the second axis. The hold property is also a per-axis property, so you just need to make sure that the hold is set on the same axis.
Is there a way to turn the y axis upside down in matlab plots, so that the positive direction of the y axis, instead of up, points down ?
(I beg of you; please do not say, print it out, then turn the paper around ;-)
The 'YDir' axes property can be either 'normal' or 'reverse'. By default it is 'normal' for most plots, but some plots will automatically change it to 'reverse', such as the image or imagesc functions.
You can set the y-axis direction of an axes with either the set function or dot indexing (in newer MATLAB versions):
h = gca; % Handle to currently active axes
set(h, 'YDir', 'reverse');
% or...
h.YDir = 'reverse';
I'm baffled by some of the other answers saying that the 'YDir' property has somehow disappeared or is giving an error. I haven't seen any such behavior in versions of MATLAB from 2013, 2014, or 2016. There are only two potential pitfalls I came across:
The property can't be set with a cell array, only a character string:
>> set(gca, 'YDir', {'reverse'});
Error using matlab.graphics.axis.Axes/set
While setting property 'YDir' of class 'Axes':
Invalid enum value. Use one of these values: 'normal' | 'reverse'.
although this works:
set(gca, {'YDir'}, {'reverse'}); % Property name is also a cell array
The gca function can't be used interchangeably as a handle when performing dot indexing (which is why I first saved it to a variable h in the above example):
>> gca.YDir
Undefined variable "gca" or class "gca.YDir".
>> gca.YDir = 'reverse' % Creates a variable that shadows the gca function
gca =
struct with fields:
YDir: 'reverse'
Finally, if you want some code that will toggle the 'YDir' property no matter what its current state is, you can do this:
set(gca, 'YDir', char(setdiff({'normal', 'reverse'}, get(gca, 'YDir'))));
% or...
h = gca;
h.YDir = char(setdiff({'normal', 'reverse'}, h.YDir));
The command
axis ij
Will also reverse the Y-axis (negative above x-axis; positive below).
The solutions on the top of the stack did not work for me,
imagesc(x,y,data) % results in a flipped plot, the y axis is upside down
set(gca,'YDir','reverse'); % gives an error
axis ij; % still gives the flipped plot
what did work was the following:
imagesc(x,y,data); axis xy; % results in the correct plot
The YDir property has vanished in the matlab version (2013 and up) that I'm using.
To update this answer, since it is still a popular Google result:
As of R2014a, the correct way to flip the Y axis is the following:
>> axis ij
This change can be reversed through the following command
>> axis ji
To flip the X or Z axes, do the following
set(gca,'XDir','reverse');
set(gca,'ZDir','reverse');
Personally, I think it would have been easier to keep the YDir option, but what do I know.
As an alternative to YDir (for some reason I cannot currently see) you can rotate the axes with view. To turn the y-axis upside down, use
view(0,-90);