Two y axis with the same x-axis [duplicate] - matlab

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Plotting 4 curves in a single plot, with 3 y-axes
assuming I have the following dataset as an example here in Matlab:
x = linspace(0, 9, 10);
y1=arrayfun(#(x) x^2,x);
y2=arrayfun(#(x) 2*x^2,x);
y3=arrayfun(#(x) x^4,x);
thus you can see they have the SAME x-axis. Now I want the following plot:
one x-axis with the limits 0 to 9 (those limits should also be ticks) with N ticks (I want to be able to define N myself), thus having N-2 ticks inbetween because 0 and 9 itself are already ticks. I want y1 and y2 to refer to the same y-axis, which is being displayed on the left with ticks for 0 and max([y1, y2]) and M more ticks inbetween.
than I want to have another axis on the right, where y3 refers to...
y1, y2 and y3 should have entries in the same legend box...
thanks so far!
edit: argh just found this: Plotting 4 curves in a single plot, with 3 y-axes perhaps I can bould it up myself... I will try just right now!
EDIT: What when using logarithmic x-axis?!

See this documentation on Using Multiple X- and Y-Axes. Something like this should do the trick:
figure
ax1 = gca;
hold on
plot(x,y1)
plot(x,y2)
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
linkaxes([ax1 ax2],'x');
hold on
plot(x,y3,'Parent',ax2);
Edit: whoops, missed a hold command. Should work now. Also, to remove the second x-axis on top, simply add 'XTickLabel',[] to the axes command.
As an aside, you really shouldn't use arrayfun for y1=arrayfun(#(x) x^2,x);. Instead, use the .^ operator: y1=x.^2;. It's much better style and is much quicker.

Related

What is missing for a better MATLAB Plot3 visualization? [duplicate]

This question already has answers here:
Produce a 3D stem plot with a custom colormap in MATLAB
(2 answers)
Closed 7 years ago.
I have produced the following figure using MATLAB plot3 function.
This figure is not good.
Because, I think, it's too hard for readers to estimate the coordinates from this figure.
Points height (Z value) is too hard to be estimated from the figure.
What is missing in my figure that makes it hard to be interpreted ?
To Play with the data:
The visualized data is here. The function to produce my current figure is here. Either comment the mArrow3 call, or download it from here.
To better see height you can use stem3 to draw a vertical line from the floor to each point. You can enhance the representation with a semi-transparent patch at zero height to highlight the floor.
% // Random data
x = -20+50*rand(1,50);
y = 150*rand(1,50);
z = -5+10*rand(1,50);
%// With plot
figure
plot3(x,y,z,'.','markersize',8)
grid on
axis equal
view(-33, 14)
%// With stem3 and patch
figure
stem3(x,y,z,'.','markersize',8)
grid on
hold on
patch([-20 30 30 -20], [0 0 150 150], [0 0 0 0], 'k', ...
'edgecolor', [.5 .5 .5], 'FaceAlpha' , .1)
axis equal
view(-33, 14)
I think the problem might be intrinsic to these kind of plots: the 0d dots of your data are hard to interpret perspectivically, your brain can't decypher at which depth the data points are located. For instance, it would seem to me that you have no data poinst above z=0 and above x=15, which is obviously wrong, but my brain attributes most of your points to the z=-5 plane.
Unless your data points have finite volume which proportionally changes with distance (which can not be done with matlab, and probably wouldn't help much anyway), you might want to reconsider your way of visualization. How about having 3 plots, one each with camera along the x, y, and z axes?
EDIT: the suggestion of Luis Mendo makes me think I should probably have a more open mind when trying to answer a question:)
You could also use different colors/markers/point size to discriminate between various regions in your data. For instance values having z below 0 are red and those above are green. Here is a simple example using scatter3 with 4 distinct regions. Thanks to Luis Mendo for the dummy data.
clc;clear;close all
% // Random data...thanks Luis Mendo
x = -20+50*rand(1,50);
y = 150*rand(1,50);
z = -5+10*rand(1,50);
%// Get indices for various regions in your data
region1 = find(z>=-4 & z<-2);
region2 = find(z>=-2 & z<0);
region3 = find(z>=0 & z<2);
region4 = find(z>=2 & z<4);
%// Draw each region with its own color/size
scatter3(x(region1),y(region1),z(region1),20,'r','filled')
hold on
scatter3(x(region2),y(region2),z(region2),40,'y','*')
scatter3(x(region3),y(region3),z(region3),60,'g','filled')
scatter3(x(region4),y(region4),z(region4),80,'b')
grid on
view(-33, 14)
kkuilla's answer about heat map produced a much better result:

Create a colormap in matlab [duplicate]

This question already has answers here:
How to create a custom colormap programmatically?
(2 answers)
Closed 7 years ago.
I have a contour plot with data that goes from -90 to 90 degrees. for now i am using jet so I have a map that looks like this
I have been asked to change the colormap so that instead of having a gradient, I have a fixed color for each 5 degress (so I believe 36 colors). Also i was thinking of maybe having same colors for the interval [5 10] and [-10 -5], and so on if that makes sense.
My code is quite long because i have a lot of data to process, but that's part of it just so you can see what function i am using to plot this
%%
x1=data(:,5); %x location
y1=data(:,16); %y location
z1=phi*90; %angle phi
z2=gamma*90; %angle gamma
n=300; precision of grid
%Create regular grid across data space
[X,Y] = meshgrid(linspace(min(x1),max(x1),n), linspace(min(y1),max(y1),n));
figure(3);
contourf(X,Y,griddata(x1,y1,z1,X,Y),100,'EdgeColor', 'None')
%title('Variation of In-plane angle \phi')
axis equal
axis ([0 8000 0 12000])
axis off
h=colorbar;
caxis([-90 90])
set(h, 'YTick', [-90:15:90])
Does anyone know how to create this colorbar?
Cheers
Every colormap-generating function in Matlab, including jet, takes an argument that specifies how many colormap entries there should be. In your case, you want 180 / 5 = 36 discrete colors:
colormap(jet(36))
To make sure the 36 colors cover exactly the 5 degree steps, set the color axis explicitly:
caxis([-90 90])
The result looks e.g. like this:

plotting two figures using matlab plotyy

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.

Matlab line plot overlaps axis when values are zero

I have plotted a figure with multiple lines on it, and I have noticed that the lines for the plot overlap the x-axis when they are zero. Is there a way that I can essentially get the x-axis to plot on the top, rather than the lines?
Here is a MWE that does the same thing (I haven't put my exact code up as my dataset is quite big).
xdata=1:1:10;
ydata=[1;0.8;0.6;0.4;0.2;0;0;0;0;0];
line(xdata,ydata)
After I plot the lines (multiple per plot in my case), I do various other things with the axes so I get what I need (including adding a secondary set of axes). None of this seems to make any difference as to whether the x-axis is plotted on top of the lines or not.
I did have a search online but couldn't find anything to do with this.
The answer given by Luis is a nice workaround, but the official way to solve this problem is to use the layer property of the axis object, see the manual. To plot the axis on top of the data you do
set(gca,'Layer','top')
To automatically do this for all your plots, you can put the following line in your startup.m:
set(0,'DefaultAxesLayer','top')
This kind of answers you do not make up yourself, I only discovered this trick after asking more or less the same question on comp.soft-sys.matlab many years ago. See also this SO question.
After having plotted all your lines, plot a line on the x axis with the same color as the axis:
hold on
a = axis; %// gives xmin xmax ymin ymax
cx = get(gca,'Xcolor'); %// color of x axis
plot([a(1) a(2)], [a(3) a(3)], 'color', cx)
If the lines also overlap with the y axis and you also want that axis to appear on top, add the following:
cy = get(gca,'Ycolor'); %// color of y axis
plot([a(1) a(1)], [a(3) a(4)], 'color', cy)

Plotting points while plotting vectors : Matlab

I need to make a plot with only points and tried something like
plot(x,y)
where x and y are vectors: collection of points.
I do not want matlab to connect these points itself. I want to plot as if plotted with
for loop
plot;hold on;
end
I tried
plot(x,y,'.');
But this gave me too thick points.
I do not want to use forloop because it is time expensive. It takes a lot of time.
You're almost there, just change the MarkerSize property:
plot(x,y,'.','MarkerSize',1)
Try:
plot(x,y,'*');
or
plot(x,y,'+');
You can take a look to the documentation: http://www.mathworks.nl/help/matlab/creating_plots/using-high-level-plotting-functions.html
help scatter
IIRC: where S is the size of the scatter points:
scatter(x,y,S)
You may try this piece of code that avoid using loops. The plot created does not have lines but markers of different colors corresponding to each column of matrices x and y.
%some data (matrix)
x = repmat((2:10)',1,6);
y = bsxfun(#times, x, 1:6);
set(0,'DefaultAxesColorOrder', jet(6)); %set the default matlab color
figure('Color','w');
plot(x,y,'p'); %single call to plot
axis([1 11 0 70]);
box off;
legend(('a':'f')');
This gives