I have a vector
A=[1 2 3 3 3 4 5]
I am able to show its box plot with
boxplot(A, 'orientation', 'horizontal')
I can also show its distribution with a stem plot like this
[nelements, centers] = hist(A);
stem(centers, nelements/numel(A), 'blue');
My question is how to combine these two plots into one figure? The figure should have its y-axis as probability and x-axis as the A values.
As for the height of the box plot in the figure, it does not matter.
How may I do this?
Use hold on as follows, and be aware that you need to vertically move (using 'position' again) your boxplot to fit the axes of stem
boxplot(A, 'orientation', 'horizontal','position',0.1); hold on
[nelements, centers] = hist(A);
stem(centers, nelements/numel(A), 'blue'); hold off
Related
I would like to plot each slice of my 3d matrix to show differences across the third dimension. However I can only manage to plot them besides each other, and I would like a 3d plot where it is clear that the slices of the matrix are in fact stacked. My code for two layers so far is
visualmatrix=zeros(10);
visualmatrix(1:5,1:5)=1;
visualmatrix2=zeros(10);
visualmatrix2(1:8,1:8)=1;
subplot(1,2,1)
[r,c] = size(visualmatrix); %# Get the matrix size
imagesc((1:c)+0.5,(1:r)+0.5,-visualmatrix); %# Plot the image
colormap(gray); %# Use a gray colormap
axis equal %# Make axes grid sizes equal
set(gca,'XTick',1:(c+1),'YTick',1:(r+1),... %# Change some axes properties
'XLim',[1 c+1],'YLim',[1 r+1],...
'GridLineStyle','-','XGrid','on','YGrid','on');
subplot(1,2,2)
[r,c] = size(visualmatrix2); %# Get the matrix size
imagesc((1:c)+0.5,(1:r)+0.5,-visualmatrix2); %# Plot the image
colormap(gray); %# Use a gray colormap
axis equal %# Make axes grid sizes equal
set(gca,'XTick',1:(c+1),'YTick',1:(r+1),... %# Change some axes properties
'XLim',[1 c+1],'YLim',[1 r+1],...
'GridLineStyle','-','XGrid','on','YGrid','on');
colorbar
colorbar('Ticks',[-1,0],...
'TickLabels',{'Equal','Different'})
suptitle('Illustration of the concept')
Which leads to the following image
Is there a simple way to make it visualize this in a 3d plot with i.e. 5 layers?
Thank you in advance.
There is a nice function for that in Matlab.
It is called slice.
It plots things like:
I am struggling to draw my two plots, one is a simple x=y and the other is a boxplot using plotyy. Here are the two :
h1=boxplot(box_panda_8(:, [8 16 24 32 128]) ,'symbol','','notch','on','whisker',0.3)
and
h2=plot([0 5],[0 5], 'k--')
given that i am defining the x axis as
x= 0:5
why the plotyy goes wrong (returns not enough input)
plotyy(x,h1,x,h2)
Updated question
Tackling the issue with two separate plots using axes:
%%% two y axes
y2 = 1:6;
x2 = 1:6;
% Plot the first data set
hl1 = boxplot(box_panda_8(:, [8 16 24 32 48 128]) ,'symbol','','notch','on','whisker',0.3)
% Get the axes and configure it
ax1 = gca;
set(ax1,'XColor','r','YColor','r')
%Create the new axes
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
% Plot the second data set with the new axes
hl2 =plot(x2,y2,'Color','k','parent',ax2);
but still I dont get my final plot in a right way.
plotyy() is not for merging plots together. Take a look at the documentation for plotyy().
[AX,H1,H2] = plotyy(X1,Y1,X2,Y2,'function1','function2')
Uses function1(X1,Y1) to plot the data for the left axis and function2(X2,Y2) to plot the data for the right axis. So you should be able to do what you're looking for by doing something along these lines:
[AX,H1,H2] = plotyy(boxplot_x,lineplot_x,lineplot_y,#boxplot,#plot);
You can use set() with AX(1) and AX(2) to change axes properties (like titles, labels, tickmarks etc.) for the left and right axis, respectively. You can use set() with H1 and H2 to set line properties for your boxplot and line plot, respectively.
Unfortunately I do not have the stats toolbox so I'm unable to test whether or not this syntax will work for boxplot().
It is also worth noting that plotyy() can be quite annoying to work with, and is limited to two plots. By stacking axes in the same figure with the backgrounds off, you remove this limitation and gain user friendly control over all aspects of each plot. See this question for a basic example.
I have a problem in matlab. I want to plot a graph having 5 plots. Let me go through them.
x axis for each data is from 1:500.
For plot 1 to 3, I want to place marker after every 10 values, whereas for plot 4 to 5 I want to place markers after every 5 values. Is it possible to do it ?
I followed a code something like this:
figure,
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
set(gcf,'Color','white');
plot(ObjVal1(1:10:end),'*r','LineWidth',3);
hold on;
plot(ObjVal2(1:10:end),'-.b','LineWidth',3);
plot(ObjVal3(1:10:end),'+-k','LineWidth',3);
plot(ObjVal4(1:5:end),'sm','LineWidth',3);
plot(ObjVal5(1:5:end),'.b','LineWidth',3);
hold off;
title({'Fitness Value'},'FontWeight','bold','FontSize', 12,'Color','black');
xlabel('Fitness Value --->','FontWeight','bold','FontSize', 12,'Color','black');
ylabel('Iterations --->','FontWeight','bold','FontSize', 12,'Color','black');
legend('CV GDS','CV Momentum','CV Exct LS','CV Back Track','CV Conjugate GDS');
Then I get an output like this :
The problem is quite evident from the picture. The plots of 1-3 is given for 50 values as the subplot is taken for each 10 iterations whereas the 4th and 5th plot is given for 100 values as the subplot is taken for each 5 iterations. I do not want to do this. Basically I want the plot of all the values but with the markers placed at each 10 iterations for plot 1-3 and at each 5 iterations for plots 4-5.
Thanks everybody in advance for your help !
Use first argument to plot to specify x-axis positions of the markers:
plot(1:10:numel(ObjVal3), ObjVal3(1:10:end),'+-k', 'LineWidth', 3);
plot(1:5:numel(ObjVal4), ObjVal4(1:5:end), 'sm', 'LineWidth', 3)
Is it possible to create a semilog plot (semilogx, semilogy, loglog) within a semilog plot? I need to have a zoom-in plot. Most solutions I found only solve for linear scale but not log scale.
Try using axes, for example:
x = linspace(0,1);
figure(1)
% plot on large axes
semilogy(x,1./x)
% create smaller axes in top right, and plot on it
axes('Position',[.55 .55 .33 .33])
box on
loglog(x,exp(x))
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