Create a colormap in matlab [duplicate] - matlab

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:

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:

How to get variable scales on y axis for same graph in matlab plotting?

I am working on matlab programming, my problem is that in the same graph on y axis i need to have variable scaling, for example from 0.1 to 1 i need to have a gap between scales 0.1, but after 1 I need to have scale gap of 2, is there some command available for the same?
There is an example by The Mathworks on Matlab answers which does pretty much what you want to achieve. The idea is to create 2 axes on the same figure and use one axes to plot some data (eg. for the 0.1:0.1:1 tick marks) and the rest on the other axes. Then you overlay both axes with a transparent background:
%Create two overlapping axes
axes_handle_1 = axes;
axes_position = get(axes_handle_1, 'Position');
axes_handle_2 = axes('Position', axes_position);
%Create some data with a large gap in the x domain
my_x_data = [1:10 25:35];
my_y_data = rand(1, length(my_x_data));
%Plot the two sections of data on different axes objects
plot(axes_handle_1, my_x_data(1:10), my_y_data(1:10))
plot(axes_handle_2, my_x_data(11:end), my_y_data(11:end))
%Link the y axis limits and fontsize property of the axes objects
linkaxes([axes_handle_1 axes_handle_2], 'y');
linkprop([axes_handle_1 axes_handle_2], 'FontSize');
%Set the x range limits and tick mark positions of the first axes object
set(axes_handle_1, 'XLim', [1 21], ...
'XTick', [1 5 10])
%Set the x range limits and tick mark positions for the second axes object.
%Also set the background color to 'none', which makes the background
%transparent.
set(axes_handle_2, 'Color', 'none', ...
'YTickLabel', [], ...
'XLim', [14 35], ...
'XTick', [25 30 35])
It's quite straightforward and to my knowledge there is no built-in way to do it otherwise, except maybe with submissions from the File Exchange. Anyhow if you have questions about the above code please ask!
Please use gca property of matlab. In gca you can set a variable as your scales. Make that variable by merging two different scales
x=[1:80];
y=[.1:.1:8];
figure
plot(x,y);
%First Scale
scale1=[.1:.1:1];
%New scale is being started from 3. If we start from 1, 1 will be repeated
scale2=[3:2:9];
%Merging two variables scale1 and scale2
set(gca,'YTick',[scale1 scale2]);
Please refer http://www.mathworks.in/help/matlab/creating_plots/change-tick-marks-and-tick-labels-of-graph.html
You can also try the idea of scaling one dataset so that it has a similar magnitude as the other data set. Here you can multiply one dataset by 100 (or any suitable scaling parameter), and then it will be similar in size to the other data set. In order to clearly mention which data has been scaled in the graph use the legend.
plot(x,data1,x,100*data2)
legend('data1','100*data2','location','southeast')
Hope this helps.

Apply variable marker position to matlab plots

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)

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

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

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.