3 x-axis in matlab plot? - matlab

I need to plot a figure with 3 x-axes. Each axis is linked to the other by a mathematical formula. I want to do this because the x value can be seen as wavelength [nm], velocity [m/s] or energy [eV] and I want the reader to not have to convert it themselves on each graph.
I searched online and only found something for 2 x-axes, but no more.
Edit: I am using version R2011a.
So it should look like this, which I (obviously) didn't create in MATLAB:
Thanks in advance!

As shown in this answer, you can create a new axes object with near-zero height, so that it is essentially just the x-axis. Be aware that all actual plots must be done on the first axes as this is the area you can see!
Demo code:
% Create some plotting data and plot
x = 0:0.1:2*pi; y = sin(x);
% Plot, can specify line attributes (like LineWidth) either
% - inline: plot(x,y,'linewidth',2)
% - after: p1 = plot(x,y); p1.LineWidth = 2;
plot(x,y);
% Get current axes object (just plotted on) and its position
ax1 = gca;
axPos = ax1.Position;
% Change the position of ax1 to make room for extra axes
% format is [left bottom width height], so moving up and making shorter here...
ax1.Position = axPos + [0 0.3 0 -0.3];
% Exactly the same as for plots (above), axes LineWidth can be changed inline or after
ax1.LineWidth = 2;
% Add two more axes objects, with small multiplier for height, and offset for bottom
ax2 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.15 0 0], 'color', 'none', 'linewidth', 2);
ax3 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.00 0 0], 'color', 'none', 'linewidth', 2);
% You can change the limits of the new axes using XLim
ax2.XLim = [0 10];
ax3.XLim = [100 157];
% You can label the axes using XLabel.String
ax1.XLabel.String = 'Lambda [nm]';
ax2.XLabel.String = 'Velocity [m/s]';
ax3.XLabel.String = 'Energy [eV]';
Output:
Edit:
Before the 2014b graphics changes you will need to make a couple of tweaks for getting and setting axes properties. The equivalent code would more heavily use the set command, and look something like this:
x = 0:0.1:2*pi; y = sin(x);
plot(x,y);
ax1 = findobj(gca, 'type', 'axes')
axPos = get(ax1, 'Position');
set(ax1, 'Position', axPos + [0 0.3 0 -0.3]);
set(ax1, 'LineWidth', 2);
ax2 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.15 0 0], 'color', 'none', 'linewidth', 2);
ax3 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.00 0 0], 'color', 'none', 'linewidth', 2);
set(ax2, 'xlim', [0 10]);
set(ax3, 'xlim', [100 157]);
axes(ax1); xlabel('Lambda [nm]');
axes(ax2); xlabel('Velocity [m/s]');
axes(ax3); xlabel('Energy [eV]');

Here's an example of how you can do this:
msx = [1 50 60 90];
msy = [0 1 3 8];
lx = 90/4*[1 2 3 4]; % Scale the data with respect to the data that will use the "primary" X-axis
ly = [0 2 8 10];
evx = 90/19*[1 7 10 19]; % Scale the data with respect to the data that will use the "primary" X-axis
evy = [0 8 16 20];
figure
a=axes('units','normalized','position',[.1 .35 .7 .6],'xlim',[0 100],'xtick',0:10:100);
plot(lx, ly)
hold on
plot(msx, msy)
hold on
plot(evx, evy)
xlabel(a,'velocity m/s')
b=axes('units','normalized','position',[.1 .21 .7 0.000001],'xlim',[0 4],'color','none', 'xtick',0:1:10);
xlabel(b,'lambda nm');
c=axes('units','normalized','position',[.1 .10 .7 0.000001],'xlim',[0 19],'color','none', 'xtick',0:1:19);
xlabel(c,'energy eV');
For the position: specified as a four-element vector of the form [left bottom width height]. The default value of [0 0 1 1] includes the whole interior of the container. (see https://de.mathworks.com/help/matlab/ref/axes-properties.html)
Output figure:

Related

Plotting bar and reverse bar into the Y axis

I am trying to plot two sequences of bars, using the same y-axis. One of the sequences goes on the bottom, the other goes on top (from top to bottom), and the y axis is split into two (half for the bottom bars and half for the top bars). The bottom bars use one color, like green, and the top bars use another color, red, and the y axis uses the respective colors on each half on the axis.
Example:
Question: How can I split the y-axis in the way represented in the figure above?
Thanks for helping! =D
You could create this effect by layering three axes and adjusting their properties accordingly. For example:
% The larger left axes:
hLeft = axes('Position', [0.1 0.1 0.8 0.8], ... % Normalized position
'XLim', [0 11], 'YLim', [0 3], ... % Set desired limits
'Box', 'off'); % Turn off outline
hLeft.XLabel.String = 'Time'; % Add an x label
hLeft.YLabel.String = 'Line'; % Add a y label
hLeft.Title.String = 'Bar-bar-plot'; % Add a title
hLine = line(hLeft, 1:10, rand(1,10)+1, 'Color', 'k'); % Plot a sample line
% The lower right axes:
hLower = axes('Position', [0.1 0.1 0.8 0.4], ... % Position over bottom half
'XLim', [0 11], 'YLim', [0 1.5], ... % Set desired limits
'YColor', [0 0.5 0], 'Color', 'none', ... % Change colors
'YAxisLocation', 'right'); % Position y axis
hLower.XAxis.Visible = 'off'; % Turn of x axis
hLower.YLabel.String = 'Bar 1'; % Add a y label
hold on;
hBarLower = bar(hLower, 1:10, rand(1,10), ... % A sample bar plot
'FaceColor', 'none', 'EdgeColor', [0 0.5 0]);
% The upper right axes:
hUpper = axes('Position', [0.1 0.5 0.8 0.4], ... % Position over top half
'XLim', [0 11], 'YLim', [0 1.5], ... % Set desired limits
'YColor', [1 0 0], 'Color', 'none', ... % Change colors
'YAxisLocation', 'right', ... % Position y axis
'YDir', 'reverse'); % Reverse y axis
hUpper.XAxis.Visible = 'off'; % Turn off x axis
hUpper.YLabel.String = 'Bar 2'; % Add a y label
hold on;
hBarUpper = bar(hUpper, 1:10, rand(1,10), ... % A sample bar plot
'FaceColor', 'none', 'EdgeColor', [1 0 0]);
% Add a legend:
hLegend = legend(hUpper, [hLine hBarLower hBarUpper], ...
{'line', 'bar 1', 'bar 2'}, 'Color', 'w');
And here's the plot:
You can reposition the legend as needed by left clicking on it and dragging it to the best spot.

Additional axis on 3D surface in MATLAB

Suppose I plot a 3d graph using
[X,Y,Z] = peaks(25);
figure
surf(X,Y,Z);
How can I add addtional x and y axis with labels to the z=0 plane like the red ones shown in this picture? I would like to keep the original axis as is.
I would just draw my own fake axis. I'm not sure there is a better solution, but this one works.
[X,Y,Z] = peaks(25);
figure
surf(X,Y,Z);
hold on;
%define the plot limits
xlim_arr = [-4 4];
ylim_arr = [-5 5];
zlim_arr = [-10 10];
%fix the limits of the real axes
xlim(xlim_arr);
ylim(ylim_arr);
zlim(zlim_arr);
%add grid and labels through all x-points
for i=xlim_arr(1):xlim_arr(2)
plot3([i i],[ylim_arr(1) ylim_arr(2)], [0 0], 'Color', [0.7 0.7 0.7], 'LineWidth',0.4);
text(i, ylim_arr(1)-0.4, 0, num2str(i));
text(i, ylim_arr(2)+0.4, 0, num2str(i));
end
%add grid and labels through all y-points
for i=ylim_arr(1):ylim_arr(2)
plot3([xlim_arr(1) xlim_arr(2)], [i i], [0 0], 'Color', [0.7 0.7 0.7], 'LineWidth',0.4);
text(xlim_arr(1)-0.4, i, 0, num2str(i));
text(xlim_arr(2)+0.4, i, 0, num2str(i));
end
%add the bold frame to highlight the fake axes
px = [xlim_arr(1) xlim_arr(1) xlim_arr(2) xlim_arr(2) xlim_arr(1)];
py = [ylim_arr(1) ylim_arr(2) ylim_arr(2) ylim_arr(1) ylim_arr(1)];
pz = [0 0 0 0 0];
plot3(px,py,pz, 'k', 'LineWidth', 0.5);

Matlab axis error using imagesc

I am plotting multiple heatmaps in matlab 3014b using imagesc with one common colorbar. Here is my code:
a(1)= subplot('Position',[0.1, 0.65, 0.3, 0.3]);
data1 = rand(5);
imagesc(data1)
ax = gca;
ax.XTick = [1 2 3 4 5 6];
ax.XTickLabel = {'0','0.1', '0.2', '0.3','0.4','0.5'};
ax.YTick = [1 2 3 4 5 6];
ax.YTickLabel = {'1','10', '100', '1000', '10000', '100000'};
a(2)= subplot('Position',[0.45, 0.65, 0.3, 0.3]);
data2 = rand(5);
imagesc(data2)
ax = gca;
ax.XTick = [1 2 3 4 5 6];
ax.XTickLabel = {'0','0.1', '0.2', '0.3','0.4','0.5'};
ax.YTick = [1 2 3 4 5 6];
ax.YTickLabel = {'1','10', '100', '1000', '10000', '100000'};
h=colorbar;
set(h, 'Position', [.8 .135 .0581 .8150])
for i=1:2
pos=get(a(i), 'Position');
set(a(i), 'Position', [pos(1) pos(2)]);
end
But I get the following error:
Error using matlab.graphics.axis.Axes/set
While setting the 'Position' property of Axes:
Value must be a 4 element vector
Not exactly sure how to resolve this? Thanks!
Error is right here:
for i=1:2
pos=get(a(i), 'Position');
set(a(i), 'Position', [pos(1) pos(2)]); %// <--- here
end
Is there a particular reason why you're truncating the last two elements? Position should be a 4 element vector where the first two elements define the distance from the lower-left corner of the container to the lower-left corner of the axes, and the third and fourth element are the width and height of the axes within the window. If it's your intention to perhaps keep all of the axes the same width/height, do something like this:
for i=1:2
pos=get(a(i), 'Position');
set(a(i), 'Position', [pos(1) pos(2) 1 1]); %// Change
end
Check out the documentation on MathWorks on axes properties... specifically Position here: http://www.mathworks.com/help/matlab/ref/axes-properties.html#zmw57dd0e52524

connect four matlab

Ok, right now i'm trying to create a connect four game through Matlab coding; now the game is still infant but my problem is that I either can't get the figure to plot in each grid square or I can't get the 'circle' figure to plot at all. Please help in any way possible. Also if anyone knows about any connect four matlab tutorials, it would be greatly appreciated.
function [] = Kinect4(nrRows, nrCols)
board = zeros(nrRows, nrCols);
nrMoves = 0;
set(gca, 'xlim', [0 nrCols]);
set(gca, 'ylim', [0 nrRows]);
for r = 1 : 1 : nrRows - 1
line([0, nrCols], [r, r], ...
'LineWidth', 4, 'Color', [0 0 1]);
end
for c = 1 : 1 : nrCols - 1
line([c, c], [0, nrRows], ...
'LineWidth', 4, 'Color', [0 0 1]);
end
DrawBoard(nrRows, nrCols)
hold on;
while nrMoves < nrRows * nrCols %Computes ability to move polygon
[x, y] = ginput(1);
r = ceil(y); % convert to row index
c = ceil(x); % convert to column index
angles = 0 : 1 : 360;
x = cx + r .* cosd(angles);
y = cy + r .* sind(angles);
plot(x, y, 'Color', [1 1 1], 'LineWidth', 3);
axis square;
end
end
Here are some fixes to the code.
removed the line DrawBoard(nrRows, nrCols). Not sure if you put it there as a comment as you have already drawn the board or if DrawBoard is a separate function.
Changed the calculation for r and c to give the center of the cell you wan the put the peg in. This is done by subtracting 0.5 from each.
Changed the line x = cx + r .* cosd(angles); to x = c + 0.5*cosd(angles);. In the previous one, variable cx is undefined and instead of r being the radius of the peg, I used 0.5 you can replace it by appropriate variable. But the idea is to draw a circle of radius 0.5 (so that it fits in a cell) with the center offset by c along x-axis. Similar change for y to offset the peg along y-axis.
Changed the color in plot command to [0 0 0], which is black. [1 1 1] is white and is impossible to see on white background :). I would suggest using 'k' for black, 'b' for blue and so on. See matlab documentation for basic color specifications.
I am guessing you are yet to implement gravity so that the peg moves down. Also you need to check is a cell is already filled. All these and other improvements (like removing unnecessary for-loops, better way to draw pegs, etc.) are left once you get to a working code.
Here's a "working" code:
function [] = Kinect4(nrRows, nrCols)
board = zeros(nrRows, nrCols);
nrMoves = 0;
set(gca, 'xlim', [0 nrCols]);
set(gca, 'ylim', [0 nrRows]);
for r = 1 : 1 : nrRows - 1
line([0, nrCols], [r, r], ...
'LineWidth', 4, 'Color', [0 0 1]);
end
for c = 1 : 1 : nrCols - 1
line([c, c], [0, nrRows], ...
'LineWidth', 4, 'Color', [0 0 1]);
end
axis square;
hold on;
while nrMoves < nrRows * nrCols %Computes ability to move polygon
[x, y] = ginput(1);
r = ceil(y) - 0.5;
c = ceil(x) - 0.5;
angles = 0 : 1 : 360;
x = c + 0.5*cosd(angles);
y = r + 0.5*sind(angles);
plot(x, y, 'Color', [0 0 0], 'LineWidth', 3);
end
end

How to plot 3D grid (cube) in Matlab

Hi I would like to plot transparent cube-shaped grid with lines in it. Something like this:
However, I managed only to draw a 2D grid:
[X,Y] = meshgrid(-8:.5:8);
Z = X+1;
surf(X,Y,Z)
I use Matlab R2009b.
If it is impossible to plot this in matlab could you recommend me a software I could use.
Consider this vectorized solution. It has the advantage that it creates a single graphic object:
%# these don't all have to be the same
x = -8:2:8; y = -8:2:8; z = -8:2:8;
[X1 Y1 Z1] = meshgrid(x([1 end]),y,z);
X1 = permute(X1,[2 1 3]); Y1 = permute(Y1,[2 1 3]); Z1 = permute(Z1,[2 1 3]);
X1(end+1,:,:) = NaN; Y1(end+1,:,:) = NaN; Z1(end+1,:,:) = NaN;
[X2 Y2 Z2] = meshgrid(x,y([1 end]),z);
X2(end+1,:,:) = NaN; Y2(end+1,:,:) = NaN; Z2(end+1,:,:) = NaN;
[X3 Y3 Z3] = meshgrid(x,y,z([1 end]));
X3 = permute(X3,[3 1 2]); Y3 = permute(Y3,[3 1 2]); Z3 = permute(Z3,[3 1 2]);
X3(end+1,:,:) = NaN; Y3(end+1,:,:) = NaN; Z3(end+1,:,:) = NaN;
%#figure('Renderer','opengl')
h = line([X1(:);X2(:);X3(:)], [Y1(:);Y2(:);Y3(:)], [Z1(:);Z2(:);Z3(:)]);
set(h, 'Color',[0.5 0.5 1], 'LineWidth',1, 'LineStyle','-')
%#set(gca, 'Box','on', 'LineWidth',2, 'XTick',x, 'YTick',y, 'ZTick',z, ...
%# 'XLim',[x(1) x(end)], 'YLim',[y(1) y(end)], 'ZLim',[z(1) z(end)])
%#xlabel x, ylabel y, zlabel z
axis off
view(3), axis vis3d
camproj perspective, rotate3d on
If you don't mind a few for loops, something like this will work:
clf
figure(1)
for g = 0:.2:2
for i = 0:.2:2
plot3([g g], [0 2], [i, i])
hold on
end
end
for g = 0:.2:2
for i = 0:.2:2
plot3([0 2], [g g], [i, i])
hold on
end
end
for g = 0:.2:2
for i = 0:.2:2
plot3([i i], [g g], [0 2])
hold on
end
end
You will just need to make the grid transparent by probably changing line properties, I don't think you can change alpha values to accomplish this. Hope that is helpful.
A more vectorized version of Stephen's answer might be the following:
i = 0:0.2:2;
[X Y] = meshgrid(i,i);
x = [X(:) X(:)]';
y = [Y(:) Y(:)]';
z = [repmat(i(1),1,length(x)); repmat(i(end),1,length(x))];
col = 'b';
hold on;
plot3(x,y,z,col);
plot3(y,z,x,col);
plot3(z,x,y,col);
Unfortunately, MATLAB does not currently support transparent lines (to my knowledge). If you really need them to be transparent I'd suggest using 'patch'.
I understand this is a late reply but it is still valid in case anyone else is looking at doing the same thing.
Assuming you are plotting cubes (/their edges), an alternative to the answers already provided is to use the 'plotcube' code from Oliver:
plotcube
The advantage of this solution is that you can:
Change the transparency of the faces (FaceAlpha), and/or,
Change the transparency of the edges (EdgeAlpha), and/or,
Change the colour of the lines (EdgeColor).
All of these can be constants, or variables.
(e.g. fixed edge colour, or a colour that changes with Z-value etc.)
To add in functionality of 2. and 3. (above) change the 'cellfun(#patch...' section in Olivers code, adding in the four extra lines of code as follows: (replace the whole cellfun section with this; including the new 'EdgeAlpha' and 'EdgeColor' lines):
cellfun(#patch,XYZ{1},XYZ{2},XYZ{3},...
repmat({clr},6,1),...
repmat({'FaceAlpha'},6,1),...
repmat({alpha},6,1),...
repmat({'EdgeAlpha'},6,1),...
repmat({0.2},6,1),... % Set this value to whatever you want; even a variable / matrix
repmat({'EdgeColor'},6,1),...
repmat({'black'},6,1)...
);
For more info on 'patch' please see patch documentation.
An important note:
- for large models (many cubes) this is very slow to run.
e.g. running this 'plotcube' function in a 'for' loop in MATLAB over thousands of blocks. I believe this is from calling the 'patch' function multiple times.
A better solution would be to vectorise; to put all your points (vertices/faces/whatever) together in a single matrix first and then call the #patch function only once (no 'for' loop). This would require changing the code somehow to update all the XYZ data.
I hope that helps someone.
Here is the 'plotcube' code in case the link to the original code by Oliver breaks someday:
function plotcube(varargin)
% PLOTCUBE - Display a 3D-cube in the current axes
%
% PLOTCUBE(EDGES,ORIGIN,ALPHA,COLOR) displays a 3D-cube in the current axes
% with the following properties:
% * EDGES : 3-elements vector that defines the length of cube edges
% * ORIGIN: 3-elements vector that defines the start point of the cube
% * ALPHA : scalar that defines the transparency of the cube faces (from 0
% to 1)
% * COLOR : 3-elements vector that defines the faces color of the cube
%
% Example:
% >> plotcube([5 5 5],[ 2 2 2],.8,[1 0 0]);
% >> plotcube([5 5 5],[10 10 10],.8,[0 1 0]);
% >> plotcube([5 5 5],[20 20 20],.8,[0 0 1]);
% Default input arguments
inArgs = { ...
[10 56 100] , ... % Default edge sizes (x,y and z)
[10 10 10] , ... % Default coordinates of the origin point of the cube
.7 , ... % Default alpha value for the cube's faces
[1 0 0] ... % Default Color for the cube
};
% Replace default input arguments by input values
inArgs(1:nargin) = varargin;
% Create all variables
[edges,origin,alpha,clr] = deal(inArgs{:});
XYZ = { ...
[0 0 0 0] [0 0 1 1] [0 1 1 0] ; ...
[1 1 1 1] [0 0 1 1] [0 1 1 0] ; ...
[0 1 1 0] [0 0 0 0] [0 0 1 1] ; ...
[0 1 1 0] [1 1 1 1] [0 0 1 1] ; ...
[0 1 1 0] [0 0 1 1] [0 0 0 0] ; ...
[0 1 1 0] [0 0 1 1] [1 1 1 1] ...
};
XYZ = mat2cell(...
cellfun( #(x,y,z) x*y+z , ...
XYZ , ...
repmat(mat2cell(edges,1,[1 1 1]),6,1) , ...
repmat(mat2cell(origin,1,[1 1 1]),6,1) , ...
'UniformOutput',false), ...
6,[1 1 1]);
cellfun(#patch,XYZ{1},XYZ{2},XYZ{3},...
repmat({clr},6,1),...
repmat({'FaceAlpha'},6,1),...
repmat({alpha},6,1)...
);
view(3);
you can make the inside line kind of transparent by setting color = [0.65, 0.65, 0.65]. And you can use dash line style for interior lines and solid lines for boundary to make it more like a 3-D object.
In my software package, I code a mesh3 function to plot the 3-D tensor product meshes.
clear all
close all
clc
Nx=11;
Ny=11;
Nz=11;
clf
hold on
[i,j]=meshgrid(1:Nx,1:Ny);
k=zeros(Ny,Nx)+Nz;
surf(i,j,k)
[i,k]=meshgrid(1:Nx,1:Nz);
j=zeros(Nz,Nx)+Ny;
surf(i,j,k)
[j,k]=meshgrid(1:Ny,1:Nz);
i=zeros(Nz,Ny)+Nx;
surf(i,j,k)
[i,j]=meshgrid(1:Nx,1:Ny);
k=zeros(Ny,Nx)+1;
surf(i,j,k)
[i,k]=meshgrid(1:Nx,1:Nz);
j=zeros(Nz,Nx)+1;
surf(i,j,k)
[j,k]=meshgrid(1:Ny,1:Nz);
i=zeros(Nz,Ny)+1;
surf(i,j,k)
view(30,30)