How can I create a plot like this in MATLAB? - matlab

I am trying to plot the ranges of different cellular base stations in MATLAB, like this:
But I can't figure out how to do it.

Here's an example of how you can create a plot like this. Note that I created sample data for the plot by randomly generating the positions of cellular base stations using uniformly distributed pseudorandom numbers:
%# Initializations:
minRange = 0; %# Lower x and y range
maxRange = 3.5; %# Upper x and y range
resolution = 1000; %# The number of data points on the x and y axes
cellRange = linspace(minRange, maxRange, resolution);
[x, y] = meshgrid(cellRange); %# Create grids of x and y coordinates
cellCoverage = zeros(size(x)); %# Initialize the image matrix to zero
%# Create the sample image data:
numBases = 200;
cellRadius = 0.75;
for iBase = 1:numBases
point = rand(1,2).*(maxRange - minRange) + minRange;
index = ((x - point(1)).^2 + (y - point(2)).^2) <= cellRadius^2;
cellCoverage(index) = cellCoverage(index) + 1;
end
%# Create the plot:
imagesc(cellRange, cellRange, cellCoverage); %# Scaled plot of image data
axis equal; %# Make tick marks on each axis equal
set(gca, 'XLim', [minRange maxRange], ... %# Set the x axis limit
'YLim', [minRange maxRange], ... %# Set the y axis limit
'YDir', 'normal'); %# Flip the y axis direction
xlabel('X-distance (km)'); %# Add an x axis label
ylabel('Y-distance (km)'); %# Add a y axis label
colormap(jet); %# Set the colormap
colorbar; %# Display the color bar
And here's the resulting plot:
Note also that the data in the image matrix cellCoverage contains no noise and has no smoothing applied, which is why the edges appear sharper than the original image in the post (which I'm guessing is generated from real data, not "fake" sample data like I used here).

Use "image"
image(x),colormap(hsv) <- where x is a matrix of cellular intensities(x,y)

You need to get the coordinate of each station then create a circle polygon around it (with a given radius), then convert this polygon into a grid. Then you sum up these grids (matrices) on top of each other. For speed, instead of using polygons you can also define which cells will be covered by a station, like all cells within 5 rows or columns of a station get the value.
You can also apply a 2D Gaussian filter to your matrix, where only the cells containing a station have a value of 1. The bandwidth of your Gaussian kernel will be your coverage radius (range). http://www.mathworks.ch/help/toolbox/images/ref/fspecial.html

Related

How can I plot only a specific range of a curve, while having axis with higher limits?

I need to plot a section of curve, using MATLAB. But I need my axes to be larger than the part I am showing.
For example, I have the following data:
x = 0:50
y = 0.5*x
I would like to plot this data from x=0 to x=20, with xlim([0 50]).
Just to clarify, I do not want to change the range of values of x, I just want to change what is shown on the graph.
Say you have some data
x = 0:50;
y = 0.5*x;
and you would like to plot only a part of it, say everything where x<=20. You can do as follows:
index = x <= 20;
plot(x(index), y(index))
xlim(x([1,end])) % set the x-axis limit to the range of all your `x` values
ylim([min(y),max(y)]) % set the y-axis limit to the range of all your `y` values
Do this:
x = 0:20
y = 0.5*x
plot(x,y)
xlim([0 50]) % This will set x-axis to the desired range
ylim([min(y) max(y)])

Matlab - Trying to use vectors with grid coordinates and value at each point for a color plot

I'm trying to make a color plot in matlab using output data from another program. What I have are 3 vectors indicating the x-position, y-yposition (both in milliarcseconds, since this represents an image of the surroundings of a black hole), and value (which will be assigned a color) of every point in the desired image. I apparently can't use pcolor, because the values which indicate the color of each "pixel" are not in a matrix, and I don't know a way other than meshgrid to create a matrix out of the vectors, which didn't work due to the size of the vectors.
Thanks in advance for any help, I may not be able to reply immediately.
If we make no assumptions about the arrangement of the x,y coordinates (i.e. non-monotonic) and the sparsity of the data samples, the best way to get a nice image out of your vectors is to use TriScatteredInterp. Here is an example:
% samplesToGrid.m
function [vi,xi,yi] = samplesToGrid(x,y,v)
F = TriScatteredInterp(x,y,v);
[yi,xi] = ndgrid(min(y(:)):max(y(:)), min(x(:)):max(x(:)));
vi = F(xi,yi);
Here's an example of taking 500 "pixel" samples on a 100x100 grid and building a full image:
% exampleSparsePeakSamples.m
x = randi(100,[500 1]); y = randi(100,[500 1]);
v = exp(-(x-50).^2/50) .* exp(-(y-50).^2/50) + 1e-2*randn(size(x));
vi = samplesToGrid(x,y,v);
imagesc(vi); axis image
Gordon's answer will work if the coordinates are integer-valued, but the image will be spare.
You can assign your values to a matrix based on the x and y coordinates and then use imagesc (or a similar function).
% Assuming the X and Y coords start at 1
max_x = max(Xcoords);
max_y = max(Ycoords);
data = nan(max_y, max_x); % Note the order of y and x
indexes = sub2ind(size(data), max_y, max_x);
data(indexes) = Values;
imagesc(data); % note that NaN values will be colored with the minimum colormap value

MATLAB - Pixelize a plot and make it into a heatmap

I have a matrix with x and y coordinates as well as the temperature values for each of my data points. When I plot this in a scatter plot, some of the data points will obscure others and therefore, the plot will not give a true representation of how the temperature varies in my data set.
To fix this, I would like to decrease the resolution of my graph and create pixels which represent the average temperature for all data points within the area of the pixel. Another way to think about the problem that I need to put a grid over the current plot and average the values within each segment of the grid.
I have found this thread - Generate a heatmap in MatPlotLib using a scatter data set - which shows how to use python to achieve the end result that I want. However, my current code is in MATLAB and even though I have tried different suggestions such as heatmap, contourf and imagesc, I can't get the result I want.
You can "reduce the resolution" of your data using accumarray, where you specify which output "bin" each point should go in and specify that you wish to take a mean over all points in that bin.
Some example data:
% make points that overlap a lot
n = 10000
% NOTE: your points do not need to be sorted.
% I only sorted so we can visually see if the code worked,
% see the below plot
Xs = sort(rand(n, 1));
Ys = rand(n, 1);
temps = sort(rand(n, 1));
% plot
colormap("hot")
scatter(Xs, Ys, 8, temps)
(I only sorted by Xs and temps in order to get the stripy pattern above so that we can visually verify if the "reduced resolution" worked)
Now, suppose I want to decrease the resolution of my data by getting just one point per 0.05 units in the X and Y direction, being the average of all points in that square (so since my X and Y go from 0 to 1, I'll get 20*20 points total).
% group into bins of 0.05
binsize = 0.05;
% create the bins
xbins = 0:binsize:1;
ybins = 0:binsize:1;
I use histc to work out which bin each X and Y is in (note - in this case since the bins are regular I could also do idxx = floor((Xs - xbins(1))/binsize) + 1)
% work out which bin each X and Y is in (idxx, idxy)
[nx, idxx] = histc(Xs, xbins);
[ny, idxy] = histc(Ys, ybins);
Then I use accumarray to do a mean of temps within each bin:
% calculate mean in each direction
out = accumarray([idxy idxx], temps', [], #mean);
(Note - this means that the point in temps(i) belongs to the "pixel" (of our output matrix) at row idxy(1) column idxx(1). I did [idxy idxx] as opposed to [idxx idxy] so that the resulting matrix has Y == rows and X == columns))
You can plot like this:
% PLOT
imagesc(xbins, ybins, out)
set(gca, 'YDir', 'normal') % flip Y axis back to normal
Or as a scatter plot like this (I plot each point in the midpoint of the 'pixel', and drew the original data points on too for comparison):
xx = xbins(1:(end - 1)) + binsize/2;
yy = ybins(1:(end - 1)) + binsize/2;
[xx, yy] = meshgrid(xx, yy);
scatter(Xs, Ys, 2, temps);
hold on;
scatter(xx(:), yy(:), 20, out(:));

How to draw a straight across the centroid points of the barcode using best fit points Matlab

This is the processed image and I can't increase the bwareaopen() as it won't work for my other image.
Anyway I'm trying to find the shortest points in the centre points of the barcode, to get the straight line across the centre points in the barcode.
Example:
After doing a centroid command, the points in the barcode are near to each other. Therefore, I just wanted to get the shortest points(which is the barcode) and draw a straight line across.
All the points need not be join, best fit points will do.
Step 1
Step 2
Step 3
If you dont have the x,y elements Andrey uses, you can find them by segmenting the image and using a naive threshold value on the area to avoid including the number below the bar code.
I've hacked out a solution in MATLAB doing the following:
Loading the image and making it binary
Extracting all connected components using bwlabel().
Getting useful information about each of them via regionprops() [.centroid will be a good approximation to the middel point for the lines].
Thresholded out small regions (noise and numbers)
Extracted x,y coordinates
Used Andreys linear fit solution
Code:
set(0,'DefaultFigureWindowStyle','docked');
close all;clear all;clc;
Im = imread('29ekeap.jpg');
Im=rgb2gray(Im);
%%
%Make binary
temp = zeros(size(Im));
temp(Im > mean(Im(:)))=1;
Im = temp;
%Visualize
f1 = figure(1);
imagesc(Im);colormap(gray);
%Find connected components
LabelIm = bwlabel(Im);
RegionInfo = regionprops(LabelIm);
%Remove background region
RegionInfo(1) = [];
%Get average area of regions
AvgArea = mean([RegionInfo(1:end).Area]);
%Vector to keep track of likely "bar elements"
Bar = zeros(length(RegionInfo),1);
%Iterate over regions, plot centroids if area is big enough
for i=1:length(RegionInfo)
if RegionInfo(i).Area > AvgArea
hold on;
plot(RegionInfo(i).Centroid(1),RegionInfo(i).Centroid(2),'r*')
Bar(i) = 1;
end
end
%Extract x,y points for interpolation
X = [RegionInfo(Bar==1).Centroid];
X = reshape(X,2,length(X)/2);
x = X(1,:);
y = X(2,:);
%Plot line according to Andrey
p = polyfit(x,y,1);
xMin = min(x(:));
xMax = max(x(:));
xRange = xMin:0.01:xMax;
yRange = p(1).*xRange + p(2);
plot(xRange,yRange,'LineWidth',2,'Color',[0.9 0.2 0.2]);
The result is a pretty good fitted line. You should be able to extend it to the ends by using the 'p' polynomal and evaluate when you dont encounter any more '1's if needed.
Result:
If you already found the x,y of the centers, you should use polyfit function:
You will then find the polynomial coefficients of the best line. In order to draw a segment, you can take the minimal and maximal x
p = polyfit(x,y,1);
xMin = min(x(:));
xMax = max(x(:));
xRange = xMin:0.01:xMax;
yRange = p(1).*xRange + p(2);
plot(xRange,yRange);
If your ultimate goal is to generate a line perpendicular to the bars in the bar code and passing roughly through the centroids of the bars, then I have another option for you to consider...
A simple solution would be to perform a Hough transform to detect the primary orientation of lines in the bar code. Once you find the angle of the lines in the bar code, all you have to do is rotate that by 90 degrees to get the slope of a perpendicular line. The centroid of the entire bar code can then be used as an intercept for this line. Using the functions HOUGH and HOUGHPEAKS from the Image Processing Toolbox, here's the code starting with a cropped version of your image from step 1:
img = imread('bar_code.jpg'); %# Load the image
img = im2bw(img); %# Convert from RGB to BW
[H, theta, rho] = hough(img); %# Perform the Hough transform
peak = houghpeaks(H); %# Find the peak pt in the Hough transform
barAngle = theta(peak(2)); %# Find the angle of the bars
slope = -tan(pi*(barAngle + 90)/180); %# Compute the perpendicular line slope
[y, x] = find(img); %# Find the coordinates of all the white image points
xMean = mean(x); %# Find the x centroid of the bar code
yMean = mean(y); %# Find the y centroid of the bar code
xLine = 1:size(img,2); %# X points of perpendicular line
yLine = slope.*(xLine - xMean) + yMean; %# Y points of perpendicular line
imshow(img); %# Plot bar code image
hold on; %# Add to the plot
plot(xMean, yMean, 'r*'); %# Plot the bar code centroid
plot(xLine, yLine, 'r'); %# Plot the perpendicular line
And here's the resulting image:

How to display labels above a histogram bin?

I have an array a(30,2) where the first column is a unique sample number and the second column is a value assigned to the sample. I plot a histogram of the 2nd column:
hist(a(:,2))
I have N bins and the y-axis tells me how many samples have a value of x,
but no information about which samples are in which bin.
How do I plot, above each bin, a list of samples (the number from the first column of my array a) that fall into each bin?
As was shown by both #Jonas and #Itamar Katz, the idea is to use HISTC to get the bin indices that each sample belongs to, then use BAR to plot the result (note that we use the 'histc' mode of display for the BAR function). My answer below is a variation of #Jonas's post:
[EDITED]
%# random data
a = [(1:30)' rand(30,1)]; %'#
%# compute edges (evenly divide range into bins)
nBins = 10;
edges = linspace(min(a(:,2)), max(a(:,2)), nBins+1);
%# compute center of bins (used as x-coord for labels)
bins = ( edges(1:end-1) + edges(2:end) ) / 2;
%# histc
[counts,binIdx] = histc(a(:,2), edges);
counts(end-1) = sum(counts(end-1:end)); %# combine last two bins
counts(end) = []; %#
binIdx(binIdx==nBins+1) = nBins; %# also fix the last bin index
%# plot histogram
bar(edges(1:end-1), counts, 'histc')
%#bar(bins, counts, 'hist') %# same thing
ylabel('Count'), xlabel('Bins')
%# format the axis
set(gca, 'FontSize',9, ...
'XLim',[edges(1) edges(end)], ... %# set x-limit to edges
'YLim',[0 2*max(counts)], ... %# expand ylimit to accommodate labels
'XTick',edges, ... %# set xticks on the bin edges
'XTickLabel',num2str(edges','%.2f')) %'# round to 2-digits
%# add the labels, vertically aligned on top of the bars
hTxt = zeros(nBins,1); %# store the handles
for b=1:nBins
hTxt(b) = text(bins(b), counts(b)+0.25, num2str(a(b==binIdx,1)), ...
'FontWeight','bold', 'FontSize',8, 'EdgeColor','red', ...
'VerticalAlignment','bottom', 'HorizontalAlignment','center');
end
%# set the y-limit according to the extent of the text
extnt = cell2mat( get(hTxt,'Extent') );
mx = max( extnt(:,2)+extnt(:,4) ); %# bottom+height
ylim([0 mx]);
If the ticks on the x-axis become too crowded, you can display them rotated with an angle using XTICKLABEL_ROTATE function (submission on FEX).
First, create a histogram using HISTC as suggested by #Itamar Katz. To make the bins the same as with HIST, you need to properly calculate bin edges. Then you can plot the distribution and add the labels using TEXT and NUM2STR.
%# get the edges, bin centers
nBins = 10;
edges = linspace(min(a(:,2),max(a(:,2),nBins+1); %# edges go from minimum to maximum of distribution
bins = (edges(1:end-1)+edges(2:end))/2;
%# get the counts and the bin-index
[counts,binIdx] = histc(a(:,2),edges);
%# plot the counts and bins (not edges) with `bar`
figure
bar(bins,counts);
%# Set the axes limits such that you have enough space for the labels
ylim([0,2*max(counts)]);
%# add the labels. Vertically align such that the text goes from the y-coordinate
%# down (as opposed to being centered on the y-coordinate).
for b = 1:nBins
text(bins(b),counts(b)*2,num2str(a(b==binIdx,1)),'VerticalAlignment','top')
end
Use histc, which return an index for each entry, to which bin did it 'fall':
[n, bin] = histc (a(:, 2), bins);
Then the samples above the k'th bin is:
a(bin==k, 1);
Pay attention, you must specify the bins' boundaries yourself (unlike hist which uses the mid-value between boundaries).