how to plot 2D intensity plot in matplotlib? - arrays

I have an Nx3 array which stores the values in N coordinates. The first and second column correspond to x and y coordinate respectively, and the third column represents the value at that coordinates. I want to plot a 2D intensity plot, what's the best way to do it?
If the coordinates are evenly spaced, then I can use meshgrid and then use imshow, but in my data the coordinates are not evenly spaced. Besides, the array is very large N~100000, and the values (third column) span several orders of magnitude (so I should be using a logplot?). What's the best way to plot such a graph?

You can use griddata to interpolate your data at all 100000 points to a uniform grid (say 100 x 100) and then plot everything with a Log scaling of the colours,
x = data[:,0]
y = data[:,1]
z = data[:,2]
# define grid.
xi = np.linspace(np.min(x),np.max(x),100)
yi = np.linspace(np.min(y),np.max(y),100)
# grid the data.
zi = griddata(x,y,z,xi,yi,interp='linear')
#pcolormesh of interpolated uniform grid with log colormap
plt.pcolormesh(xi,yi,zi,norm=matplotlib.colors.LogNorm())
plt.colormap()
plt.show()
I've not tested this but basic idea should be correct. This has the advantage that you don't need to know your original (large) dataset and can work simply with the grid data xi, yi and zi.
The alternative is to colour a scatterplot,
plt.scatter(x, y, c=z,edgecolors='none', norm=matplotlib.colors.LogNorm())
and turn off the outer edges of the points so they make up a continuous picture.

Related

How to add element to array in MatLab?

I am trying to make a graph of the brightness of a pixel vs the distance from center of that pixel. To do so I used for loops to check each pixel for these values. But when adding them to my array I find that I can't. One of the issues is I have to define the array size first so no values get placed in the right spot. I believe everything else to be working except adding values to the arrays.
I've tried various methods of concatenation to add the values of each pixel to the array. I didn't have any more solutions to try.
folder3 = 'C:\Users\slenka\Desktop\Image_Analysis\Subtracted';
cd('C:\Users\slenka\Desktop\Image_Analysis\Subtracted');
subtractedFiles = [dir(fullfile(folder3,'*.TIF')); dir(fullfile(folder3,'*.PNG')); dir(fullfile(folder3,'*.BMP')); dir(fullfile(folder3,'*.jpg'))];
numberOfSubImages= length(subtractedFiles);
for b = 1 : numberOfSubImages
subFileName=fullfile(folder3, subtractedFiles(b).name);
chartImage=imread(subFileName);
[chartY, chartX, chartNumberOfColorChannels] = size(chartImage);
ccY= chartY/2;
ccX= chartX/2;
c=[ccX,ccY];
distanceArray=zeros(1,chartX);
intensityArray=zeros(1,chartY);
f=1;
g=1;
for y=1:chartY
for x=1:chartX
D = sqrt((y - c(1)) .^ 2 + (x - c(2)) .^ 2);
grayScale= impixel(chartImage, x, y);
distanceArray(f)=[D];
intensityArray(g)=[grayScale];
f=f+1;
g=g+1;
end
end
xAxis=distanceArray;
yAxis=intensityArray;
plot(xAxis,yAxis);
end
I'm expecting 2 arrays one full of the data values for the light intensity of each pixel in the image, and another for that pixels distance from the center of the image. I am wanting to plot these two arrays as the y and x axis respectively. At the moment the actual results is an entirely empty array full of zeros.

Matlab signal plot ,not expected X axis

I got a 2d array ,that each row represents a signal (for wiener filtering)
Arr(10,45).
I want to plot,all signals (all column) in same figure, with X axis the K coefficient of wiener fieltering that is
K=(-11:0.5:11);
Which is of size=45.I also want it to be with logarithmic in both axis , x and y.
But when i plot with
loglog(Arr.');
set(gca,'xtick',(-11:0.5:11);
The result is not what i need.
What's going on?thanks in advance.
When plotting it, you should specify the k values as the first input to plot and Arr.' as the second. If you do not do this, MATLAB will simply use 1:size(Arr, 2) as the x coordinates of your plot.
hplot = plot(k, Arr.');

Scatter plot: Using different colour for different data set

I have 3 Nx1 arrays, say X,Y,Z. I'd like to create a 3D plot such that each of the arrays I can assign different colours to improve the visibility of points. My main objective now is to change only the colour of array Z, such that elements of array X and Y have different colours as that of elements of Z .
I tried scatter3 function and gscatter MATLAB functions, but not able to achieve what I desire.
If we see the Vertical axis in image (Z axis), the colour of points is varying from Blue to Orange/Yellow. I want to set the colour of all these Z points as Red. Rest, all the colours of X and Y points remain same
scatter(x, y, a, c) takes arguments x and y, and then a for size, and c for colour. a can either be a single scalar, or a vector with a size for each (x,y) point. c can be an RGB triplet, or a vector, the same size as x and y. For example:
x = 1:4;
scatter(x, x, 10*x, x);
results in
scatter3(x,y,z,s,c) is similar, so in your case, perhaps
scatter3(X,Y,Z,[],Z)
will result in your data having a different colour determined by its z value.
One little example which I think is what you're looking for:
X = rand(100,1);
Y = rand(100,1);
Z = rand(100,1);
scatter3(X,Y,Z,[],Z)
Produces:
I got the answer by trying different MATLAB functions.
I used the gscatter function. As I had to colour all the Z points(consider 'Z' array as the 3rd data set) as Red and rest X and Y data set (the other 2 data set) as green or other colour, I used the following code:
group = Z(:,1);
gscatter(X(:,1),Y(:,1),group, 'gr', 'xo');
It creates a 2D plot but serves my purpose.
The Image is made of 3 data sets, but the first 2 data sets are intentionally combined (in Green Cross) and 3rd data set is kept Red, to visualize the relation of 3rd data set with 1st and 2nd data sets combined.
Click here for Image.

Making coordinates out of arrays

So, I have two arrays:
X'
ans =
2.5770 2.5974 2.1031 2.7813 2.6083 2.9498 3.0053 3.3860
>> Y'
ans =
0.7132 0.5908 1.9988 1.0332 1.3301 1.1064 1.3522 1.3024
I would like to combine n-th members of two arrays together, and than plot those coordinates on graph.
So it should be:
{(2.5770,0.7132), (2.5974,0.5908)...}
Is this possible to do? If so, how?
Schorsch showed that it is simple to plot, but just to answer the question as asked in the title, you can combine the arrays into coordinates by just arranging the vectors like rectangles.
Your x and y are vertical, so you can put them side-by-side in a 2-column matrix:
combined = [x y]
or transform and have 2 rows: combined = [x' ; y']
(Because they're vertical, what you don't want is these, which would concatenate them out into one long column or row: [x ; y] or [x' y'])
Just to be clear, though, this is not needed for plotting.
Edit: A suggested edit asked what happens if you plot(combined). That depends if it's the horizontal or vertical version. In any case, plotting a 2x? matrix won't plot x vs. y. It plots all of the columns versus the simple indices 1,2,3,... So the first way I defined combined will make two lines, plotting x and y on the y-axis against their indices on the x-axis, and the second version of combined will make a strange plot with the all of the values of x plotted in a vertical column where x=1 and all of the points of y beside those at x=2.

matrix indexing to make new matrix

I am trying to use imported data from a Scanning Transmission X-Ray Microscope raster scan in Matlab to create an intensity image in the form of an array. That is, the y-poisition is held constant and all x values are scanned and an intensity value is given for the relative (x,y) position. Then the next y value is taken, held constant and all x values are scanned, etc. The data is read as the column vectors into Matlab is as follows:
x = x-position ranging in values from 0-326 [104640x1 double]
y = y-position ranging in values from 0-319 [104640x1 double]
I = intensity at position (x,y) [104640x1 double]
I wish to create a a 326x319 array with entries corresponding to intensity vaules at the point (m0,n0)=(x0,y0)
I believe there is an easy way to do this using either matrix indexing or a for statement but I am relatively new to Matlab.
If I understand correctly:
result = reshape(I,327,320);
reshape() function on co-ordinates with respect to intensity should work.

Resources