Generate Color Plot - matlab

There is an object on an area with dimension M*M unit cells. The coverage rate C=1/M * Sum(i=1 to M J(i)) where J(i)=1 when the cell i is covered and 0 otherwise. This is a color scale map representing the visit of the cells vs the times of visiting by the object. So, the legend shows that there are cells which have been visited from 0 to 8 times in N number of iterations. Can anyone tell me how this color representation can be coded? What and how this can be generated?

Use image (or imagesc). You need a matrix of X values and Y values, and the corresponding matrix of Z values.
For example:
% generate some x,y
[x,y]=meshgrid(1:10,1:10);
% generate some z values: random numbers from 0 to 8
z = randi([0 8],size(x));
% plot
imagesc(x,y,z)
How you determine your x, y, z ... well, you'd have to provide more information.

You could use a two dimensional array and read the information in from a file.

Related

matlab scatter plot using colorbar for 2 vectors

I have a two columns of data. X = Model values of NOx concentrations and Y = Observations of NOx concentrations. Now, I want to scatter plot X, Y (markers varying with colors) as well as the colourbar which would show me the counts (i.e. number of data points in that range). X and Y are daily data for a year, i.e. 365 rows.
Please help me. Any help is greatly appreciated.
I have attached a sample image.
If I understand you correctly, the real problem is creating the color information, which is, creating a bivariate histogram. Luckily, MATLAB has a function, hist3, for that in the Statistics & Machine Learning Toolbox. The syntax is
[N,C] = hist3(X,nbins)
where X is a m-by-2 matrix containing the data, and nbins is a 1-by-2 vector containing the number of bins in each dimension. The return value N is a matrix of size nbins(1)-by-nbins(2), and contains the histogram data. C is a 1-by-2 cell array, containing the bin centers in both dimensions.
% Generate sample data
X = randn(10000, 1);
Y = X + rand(10000, 1);
% Generate histogram
[N,C] = hist3([X,Y], [100,100]);
% Plot
imagesc(C{1},C{2},N);
set(gca,'YDir','normal');
colormap(flipud(pink));
colorbar;
Result:

Color scatter plot in Matlab according to 0 or 1 value

I populate a grid data=zeros(n,n); with 0's and 1's (could also be thought of as an adjacency grid, if you'd like). I just want to plot the grid with colors according to whether the value at that point is 0 or 1. For example,
scatter(1:n,1:n,data);
It gives me the error:
Error using scatter (line 77)
C must be a single color, a vector the same length as X, or an M-by-3 matrix.
Any suggestions?
you are telling matlab to plot only n points ((1,1), (2,2), ..., (n,n)) where you want actually the cartesian product (1:nX1:n).
Try
[X,Y] = meshgrid(1:n,1:n);
scatter(X(:), Y(:), 10, data(:));
scatter allows you to plot points with different options (color, size, etc) for each point depending on a 'Z' value, but it creates a lot of graphic objects (one for each point).
In your case, you only have 2 subsets of data (among all your points). The points with value 1 and with value 0. So another option is to extract these 2 subsets then plot each subset with each a set of common properties.
%% // prepare test data
n = 10 ;
data=randi([0 1],n); %// create a 10x10 matrix filled with `0` and `1`
%% // extract the 2 subsets
[x0 , y0] = find( data == 0 ) ;
[x1 , y1] = find( data == 1 ) ;
%% // display
figure ; axes('Nextplot','add')
plotOptions = {'LineStyle','none','MarkerEdgeColor','k','MarkerSize',10} ; %// common options for both plots
plot(x0,y0,'o','MarkerFaceColor','r', plotOptions{:} ) %// circle marker, red fill
plot(x1,y1,'d','MarkerFaceColor','g', plotOptions{:} ) %// diamond marker, green fill
This way you have full control on each subset property (you can control the size, color, shape etc...). And you only have 2 graphic objects to handle (instead of n^2).

Matlab bar3 plot

I have a problem with MATLAB bar3 plots: Here is what I have:
m x n Array Values containing values of a measurement.
Another m x n Array Angles Represents the angle at which a value was measured (e.g. the 3rd value was measured at an angle of 90°). The angular values for each measurement value are stored in another variable.
I need a range for my x-axis from -180° to +180°. This alone is no problem. But how do I hand over my measurement values? I have to somehow link them to the angular values. So that each value in Values is somehow linked to it's angular value in Angles. For my y-axis, I can simply count from 0 to the amount of rows of my Values Array.
EXAMPLE:
Valueslooks like:
3 5 6
2 1 7
5 8 2
Angles looks like:
37° 38° 39°
36° 37° 38°
34° 35° 36°
Values(1,1) = 3 was measured at Angles(1,1) = 37° for example.
At each angle, the number of bars varies depending on how many measurements exist for that angle. bar3 needs a matrix input. In order to build a matrix, missing values are filled with NaN.
Warning: NaNs are usually ignored by plotting commands, but bar3 apparently breaks this convention. It seems to replace NaNs by zeros! So at missing values you'll get a zero-height bar (instead of no bar at all).
[uAngles, ~, uAngleLabels] = unique(Angles); %// get unique values and
%// corresponding labels
valuesPerAngle = accumarray(uAngleLabels(:), Values(:), [], #(v) {v});
%// cell array where each cell contains all values corresponding to an angle
N = max(cellfun(#numel, valuesPerAngle));
valuesPerAngle = cellfun(#(c) {[c; NaN(N-numel(c),1)]}, valuesPerAngle);
%// fill with NaNs to make all cells of equal lenght, so that they can be
%// concatenated into a matrix
valuesPerAngle = cat(2, valuesPerAngle{:}); %// matrix of values for each angle,
%// filled with NaNs where needed
bar3(uAngles, valuesPerAngle.'); %'// finally, the matrix can be plotted
ylabel('Angles')
xlabel('Measurement')
With your example Values and Angles this gives:

Matlab - multiple variables normalized histogram?

I'm working on MATLAB, where I have a vector which I need to split into two classes and then get a histogram of both resulting vectors (which have different sizes). The values represent height records so the interval is about 140-185.
How can I get a normalized histogram of both resulting vectors in different colors. I was able to get both normalized vectors in the same colour (which is indistiguible) and and also a histogram with different colours but not not normalized...
I hope you understand my question and will be able to help me.
Thanks in advance :)
Maybe this is what you need:
matrix = [155+10*randn(2000,1) 165+10*randn(2000,1)];
matrix(1:1100,1) = NaN;
matrix(1101:2000,2) = NaN; %// example data
[y x] = hist(matrix, 15); %// 15 is desired number of bins
y = bsxfun(#rdivide, y, sum(y)) / (x(2)-x(1)); %// normalize to area 1
bar(x,y) %// plots each column of y vs x. Automatically uses different colors

Matlab surface plot not giving desired results

I am charting the following data:
a=[...
0.1, 0.7, 0.00284643369242828;...
0.1, 0.71, 0.00284643369242828;...]
such that column 1 never surpasses approximately 10
also such that column 2 goes from .7 to 1.
Column 3 seems ok
When i chart my surface using surf(a) it looks like this:
it appears not to be properly considering what should be x and y.
anything seem weird there?
I think you need to try one of two things: either break out your height column into its own rectangular matrix Z and use surf(Z) to plot each point relative to its location in the matrix (so your x- and y-axes will not be scaled the way you want), or you can put your desired x- and y-coordinates in their own vectors, and plot the matrix Z (defined at every point (xi, yj) for all i in N and j in M where x is N elements long and y is M elements long) with surf(x,y,Z).
x = 0.1:0.1:10; % or whatever increment you need
y = 0.7:0.01:1; % or whatever increment you need
Z = zeros(length(x),length(y); % initialized to the correct size, fill with data
I think you are going to have to regenerate your Z-data so that it is in a rectangular matrix that is (elements in x) by (elements in y) in dimension.
EDIT: You do not need to recreate your data. If you know that you have n unique elements in x and m unique elements in y, then you can use:
X = reshape(data(:,1),m,n);
Y = reshape(data(:,2),m,n);
Z = reshape(data(:,3),m,n);
surf(X,Y,Z);
And that should give you what you are looking for.