Plot multiple columns with different colors in MATLAB - matlab

I have a 372x15 matrix. I'm trying to graph this in such a way that columns 1-14 will be on the x-axis with different colors for each column, whereas the 15th column will be treated as the y-axis. For example, the plot with follow (x1, y), (x2, y) so on so forth, where x1 is all the data points in column 1. This is a simple scatterplot. How can I do this on MATLAB?

A simple way to do that is just use plot(A(:,1:end-1), A(:,end), '.'). Here's an example:
A = [(1:14)-.6*rand(372,14) ((1:372).'+rand(372,1))]; % example A. Uses implicit expansion
plot(A(:,1:end-1), A(:,end), '.') % do the plot
axis tight % optionally make axis limits tight
The above cycles through the 7 predefined colors. If you prefer to customize the colors, set the 'ColorOrder' property of the axes before calling plot, and use hold on to prevent Matlab from resetting it:
clf % clear figure
cmap = autumn(size(A,2)); % example colormap
set(gca, 'ColorOrder', cmap); % set that colormap
hold on % needed so that the colormap is not automatically reset
plot(A(:,1:end-1), A(:,end), '.')
axis tight
You can specify different markers or marker sizes; see plot's documentation.

Related

Vertical lines for Bode plots in Matlab

I have graphed a Bode plot for my transfer function, and I was wondering if there is some way to insert either horizontal or vertical lines to show a specific value for the gain/phase angle or frequency?
I have found with the following code I can draw a horizontal line on the phase angle graph:
x = linspace(10^-1,10^2,100);
for bleh = 1:length(x)
y(bleh) = -30.9638;
end
bode(num, den)
hold on
plot(x,y)
But this does not seem to apply in the gain graph, nor does my limited knowledge (and only way that makes sense to me) of vertical lines. I tried:
y1 = get(gca,'ylim');
w1 = 1.2;
bode(num, den)
hold on
plot(x,y,[w1 w1],y1)
But I only get the one horizontal line as was done from the above code.
Is this a possibility?
(Using R2017a, if that matters.)
I'm not sure I've understood you question, nevertheless, I propose the following.
When there are more one axes in a figure, as it is the case of the bode diagram, if you want to add something in a specific axes (or in all) you have to specify, in the call to plot the handle of the axes.
So, to add lines in the bode diagram, you have first to identify the handles of the two axes: you can do it in, at least two way:
using the findobj function: ax=findobj(gcf,'type','axes')
extract them as the Children of the figure: ax=get(gcf,'children')
Once you have the handles of the axes, you can get their XLim and YLim that you can use to limit the extent of the line you want to add.
In the following example, I've used the above proposed approach to add two lines in each graph.
The horizontal and vertical lines are added in the middle point of the X and Y axes (problably this point does not have a relevant meaning, but it is ... just an example).
% Define a transfer function
H = tf([1 0.1 7.5],[1 0.12 9 0 0]);
% PLot the bode diagram
bode(H)
% Get the handles of the axes
ax=findobj(gcf,'type','axes')
phase_ax=ax(1)
mag_ax=ax(2)
% Get the X axis limits (it is the same for both the plot
ax_xlim=phase_ax.XLim
% Get the Y axis limits
phase_ylim=phase_ax.YLim
mag_ylim=mag_ax.YLim
%
% Define some points to be used in the plot
% middle point of the X and Y axes of the two plots
%
mid_x=(ax_xlim(1)+ax_xlim(2))/2
mid_phase_y=(phase_ylim(1)+phase_ylim(2))/2
mid_mag_y=(mag_ylim(1)+mag_ylim(2))/2
% Set hold to on to add the line
hold(phase_ax,'on')
% Add a vertical line in the Phase plot
plot(phase_ax,[mid_x mid_x],[phase_ylim(1) phase_ylim(2)])
% Add an horizontal line in the Phase plot
plot(phase_ax,[ax_xlim(1), ax_xlim(2)],[mid_phase_y mid_phase_y])
% Set hold to on to add the line
hold(mag_ax,'on')
% Add a vertical line in the Magnitide plot
plot(mag_ax,[mid_x mid_x],[mag_ylim(1) mag_ylim(2)])
% Add an Horizontal line in the Magnitide plot
plot(mag_ax,[ax_xlim(1), ax_xlim(2)],[mid_mag_y mid_mag_y])
Hope this helps,
Qapla'

Matlab logarithmic range colorbar imagesc

Using the function imagesc in Matlab, I plot my (X,Y, Z) data-X array distance, Y array time, and my data Z = Z(X,Y) a matrix.
I notice that the 80% of the image has one color, because the change of Z data occurred only in the end of X for almost all Y.
Right now I use colormap('hsv') which give I think the largest range of different colors.
I need to change the colorbar range to a logarithmic one to improve visual the range of the change of my output data through time along the distance X.
I have used also contourf but still I am not sure if it will better to use this function and not imagesc which the output is more smoothed.
Please, any idea, any method or any small script that I could use to show visual the difference in data in logarithmic scale in 2D using imagesc or another build in function is more than welcome!
thank you
There is a discussion at the Mathworks website where someone provided a function that does logarithmic color bars.
https://www.mathworks.com/matlabcentral/newsreader/view_thread/152310
EDIT: copying and pasting code from link
function cbar = colorbar_log(my_clim)
%COLORBAR_LOG Apply log10 scaling to pseudocolor axis
% and display colorbar COLORBAR_LOG(V), where V is the
% two element vector [cmin cmax], sets manual, logarithmic
% scaling of pseudocolor for the SURFACE and PATCH
% objects. cmin and cmax should be specified on a LINEAR
% scale, and are assigned to the first and last colors in
% the current colormap. A logarithmic scale is computed,
% then applied, and a colorbar is appended to the current
% axis.
%
% Written by Matthew Crema - 7/2007
% Trick MATLAB by first applying pseudocolor axis
% on a linear scale
caxis(my_clim)
% Create a colorbar with log scale
cbar = colorbar('Yscale', 'log');
% Now change the pseudocolor axis to a log scale.
caxis(log10(my_clim));
% Do not issue the COLORBAR command again! If you want to
% change things, issue COLORBAR_LOG again.

MATLAB: a better way to switch between primary and secondary y-axis?

I'm aware of plotyy but in my opinion it's not as intuitive as for example typing subplot(2,3,1) and from that point one working in that particular subplot's environment...
Suppose I have the following data:
a=rand(20,1);
a_cumul=cumsum(a);
I would like to do a plot of a_cumul on the primary (left hand) y-axis and a bar chart of a on the secondary (right hand) y-axis.
I'm well aware that I can do:
plotyy(1:length(a_cumul),a_cumul,1:length(a),a,'plot','bar')
But this is cumbersome and what if I want to for example plot to the secondary y-axis only and not plot to the primary y-axis? In short, I'm looking for whether a solution like this exists:
figure;
switchToPrimaryYAxis; % What to do here??
plot(a_cumul);
% Do some formatting here if needed...
switchToSecondaryYAxis; % What to do here??
bar(a);
Thanks a lot for your help!
Basically plotyy:
creates two superimposed axes
plots the data specified as the first two params on the first axes
plots the data specified as the last two params on the second axes
set the second second axes color to none making it "transparent" so allowing seeing the graph on the first axes
moves the yaxislocation from the standard position (left) to right
You can create a figure, then two axes make make any plot on the two axes by selecting then with axes(h) where h is the handler of the axes.
Then you can write a your own function performing the axes adjustment.
Script to create figure, axes and call the function to adjust the axes
% Generate example data
t1=0:.1:2*pi;
t2=0:.1:4*pi;
y1=sin(t1);
y2=cos(t2);
% Create a "figure"
figure
% Create two axes
a1=axes
a2=axes
% Set the first axes as current axes
axes(a1)
% Plot something
plot(t1,y1,'k','linewidth',2)
% Set the second axes as current axes
axes(a2)
% Plot something
plot(t2,y2,'b','linewidth',2)
grid
% Adjust the axes:
my_plotyy(a1,a2)
Function to adjust the axes - emulating plotyy behaviour
The function requires, as input, the handles of the two axes
function my_plotyy(a1,a2)
set(a1,'ycolor',[0 0 0])
set(a1,'box','on')
% Adjust the second axes:
% change x and y axis color
% move x and y axis location
% set axes color to none (this make it transparend allowing seeing the
% graph on the first axes
set(a2,'ycolor','b')
set(a2,'xcolor','b')
set(a2,'YAxisLocation','right')
set(a2,'XAxisLocation','top')
set(a2,'color','none')
set(a2,'box','off')
Hope this helps.

matlab colorbar for lineplot colored by value

I have 2d line plot in matlab where each line is colored according to a value. I would like to add a colorbar showing the color that corresponds to these values.
I got a solution to plot the lines according to the value I want, however I can not figure out to get the colorbar correctly. I have been searching on this but I am stuck.
Define an RGB color matrix COL.
(N x 3 low red to dark matrix corresponding to equally spaced values 0:1).
Sort the data according to their z value.
Interpolate the COL matrix to get values for all z values, giving the TRUECOL matrix for the lines.
Set the axiscolor-ordering to the TRUECOL matrix and plot the data.
minimalistic example:
% Generate 10 lines of 10 points
x = normrnd(0,1,10,10);
% The corresponding values are
% Note that these do not have to linearly spaced in real code
z = [0,0.05,0.1,0.11,0.12,0.2,0.4,0.45,0.8,0.9];
% Define colormatrix
COL = [0.996078431372549 0.878431372549020 0.823529411764706;...
0.937254901960784 0.231372549019608 0.172549019607843;...
0.403921568627451 0 0.0509803921568627];
% Interpolate the COL matrix to get colors for the data
TRUECOL = interp1(linspace(0,1,3),COL,z,'pchip');
% Set the axis coloring qnd plot the data
set(gcf,'DefaultAxesColorOrder',TRUECOL);
plot(x);
colormap(TRUECOL);
colorbar
I then change the colormap and plot the colobar, however the colors in the colorbar to not correspond to the values z. Is there a way of telling matlab which color corresponds to which value? Looking at the colorbar editor I see that CData must have something to do with it, but I cant find a way to specify that CData should be z.
My understanding is that you want the labels on the colorbar to go from 0 to 1, not 0 to 11. To fix this, use this caxis command. To get finer gradations of colors in the colorbar, you need to more finely interpolate the colormap. Try this:
colormap(interp1(linspace(0,1,size(COL,1)), COL, linspace(0,1,100)));
caxis([0,1]);
colorbar

How can I make a "color map" plot in matlab?

I have some data (a function of two parameters) stored in a matlab format, and I'd like to use matlab to plot it. Once I read the data in, I use mesh() to make a plot. My mesh() plot gives me the the value of the function as a color and a surface height, like this:
What matlab plotting function should I use to make a 2D mesh plot where the dependent variable is represented as only a color? I'm looking for something like pm3d map in gnuplot.
By default mesh will color surface values based on the (default) jet colormap (i.e. hot is higher). You can additionally use surf for filled surface patches and set the 'EdgeColor' property to 'None' (so the patch edges are non-visible).
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
% surface in 3D
figure;
surf(Z,'EdgeColor','None');
2D map: You can get a 2D map by switching the view property of the figure
% 2D map using view
figure;
surf(Z,'EdgeColor','None');
view(2);
... or treating the values in Z as a matrix, viewing it as a scaled image using imagesc and selecting an appropriate colormap.
% using imagesc to view just Z
figure;
imagesc(Z);
colormap jet;
The color pallet of the map is controlled by colormap(map), where map can be custom or any of the built-in colormaps provided by MATLAB:
Update/Refining the map: Several design options on the map (resolution, smoothing, axis etc.) can be controlled by the regular MATLAB options. As #Floris points out, here is a smoothed, equal-axis, no-axis labels maps, adapted to this example:
figure;
surf(X, Y, Z,'EdgeColor', 'None', 'facecolor', 'interp');
view(2);
axis equal;
axis off;
gevang's answer is great. There's another way as well to do this directly by using pcolor. Code:
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
figure;
subplot(1,3,1);
pcolor(X,Y,Z);
subplot(1,3,2);
pcolor(X,Y,Z); shading flat;
subplot(1,3,3);
pcolor(X,Y,Z); shading interp;
Output:
Also, pcolor is flat too, as show here (pcolor is the 2d base; the 3d figure above it is generated using mesh):
Note that both pcolor and "surf + view(2)" do not show the last row and the last column of your 2D data.
On the other hand, using imagesc, you have to be careful with the axes. The surf and the imagesc examples in gevang's answer only (almost -- apart from the last row and column) correspond to each other because the 2D sinc function is symmetric.
To illustrate these 2 points, I produced the figure below with the following code:
[x, y] = meshgrid(1:10,1:5);
z = x.^3 + y.^3;
subplot(3,1,1)
imagesc(flipud(z)), axis equal tight, colorbar
set(gca, 'YTick', 1:5, 'YTickLabel', 5:-1:1);
title('imagesc')
subplot(3,1,2)
surf(x,y,z,'EdgeColor','None'), view(2), axis equal tight, colorbar
title('surf with view(2)')
subplot(3,1,3)
imagesc(flipud(z)), axis equal tight, colorbar
axis([0.5 9.5 1.5 5.5])
set(gca, 'YTick', 1:5, 'YTickLabel', 5:-1:1);
title('imagesc cropped')
colormap jet
As you can see the 10th row and 5th column are missing in the surf plot. (You can also see this in images in the other answers.)
Note how you can use the "set(gca, 'YTick'..." (and Xtick) command to set the x and y tick labels properly if x and y are not 1:1:N.
Also note that imagesc only makes sense if your z data correspond to xs and ys are (each) equally spaced. If not you can use surf (and possibly duplicate the last column and row and one more "(end,end)" value -- although that's a kind of a dirty approach).
I also suggest using contourf(Z). For my problem, I wanted to visualize a 3D histogram in 2D, but the contours were too smooth to represent a top view of histogram bars.
So in my case, I prefer to use jucestain's answer. The default shading faceted of pcolor() is more suitable.
However, pcolor() does not use the last row and column of the plotted matrix. For this, I used the padarray() function:
pcolor(padarray(Z,[1 1],0,'post'))
Sorry if that is not really related to the original post