Defines X and Y in axes limit matlab - matlab

I have my histogram with this appearance
And he wanted to get this way, but I don't know which fields should I change or where
I wanted to like this
How can i make this definition?

I understand you want to plot the histogram of an image in a way to limit the x axis to 255 shades of gray and y to the number of pixels in the image. This should do the work:
ima=[1 2 255;0 23 78;3 60 200;255 0 78]
plot([0:255],hist(ima(:),[0:255]))
set(gca,'xLim',[0 255])
set(gca,'yLim',[0 numel(ima)])

Related

Keep subplot in square while having (1,3) tiledlayout [duplicate]

So I have this matrix in MATLAB, 200 deep x 600 wide. It represents an image that is 2cm deep x 6cm wide. How can I plot this image so that it is locked into proper dimensions, i.e. 2cm x 6cm? If I use the image or imagesc commands it stretches it all out of shape and shows it the wrong size. Is there a way to lock it into showing an image where the x and y axes are proportional?
Second question, I need to then set this image into a 640x480 frame (20 pixel black margin on left and right, 280 pixel black margin on bottom). Is there a way to do this?
To keep aspect ratio, you can use axis equal or axis image commands.
Quoting the documentation:
axis equal sets the aspect ratio so that the data units are the same in every direction. The aspect ratio of the x-, y-, and z-axis is adjusted automatically according to the range of data units in the x, y, and z directions.
axis image is the same as axis equal except that the plot box fits tightly around the data`
For second question:
third_dimension_size=1; %# for b&w images, use 3 for rgb
framed_image=squeeze(zeros(640,480,third_dimension_size));
framed_image(20:20+600-1,140:140+200-1)= my_600_200_image;
imagesc(framed_image'); axis image;
set(gca,'DataAspectRatio',[1 1 1])
Second question:
new_image = zeros(480,640);
new_image(20:(200+20-1),20:(600+20-1)) = old_image;
As an alternative to the other answers, you might want:
set(gca, 'Units', 'centimeters', 'Position', [1 1 6 2])
Make sure you do this after plotting the image to get the other axis properties correct.
For the second question, take care with the number of colour channels:
new_image = zeros(480,640, size(old_image));
new_image(20:(200+20-1),20:(600+20-1),:) = old_image;

Painting all bars less lower than a threshold with a single color

I have a bivariate histogram plot created using bar3. I'm trying to change the color of the bars that have a height less than a certain threshold, but to no avail. I got this code:
h = bar3(dataSample, 0.5);
for n=1:numel(h)
cdata=get(h(n),'zdata');
set(h(n),'cdata',cdata,'facecolor','interp')
end
I can't figure out how to make the plot look like the one below, where the bars less than say 0.001 are gray:
Any ideas?
here's how:
z=peaks(20);
h=bar3(z)
for n=1:numel(h)
cdata=get(h(n),'zdata');
set(h(n),'cdata',cdata,'facecolor','interp')
end
colormap([0.5.*ones(128,3); parula(128)]);
I've arbitrarily decided to cut the colormap in the middle, first 128 intensities as gray the next 128 intensities in color. you can cut it however you want. You can find the threshold you want by setting the colormap binning (say to 256 bins) and the place in that partition below which it'll be gray.

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:

How to change axis scale on imagesc in Matlab

I have a 15x15 image of a grid of numbers which I have shown using imagesc. However, the axis goes up to 450 in both directions, when I only want it to go up to 15. I tried:
axis/30;
But that doesn't do anything? All I want to do is divide the x and y axes by 30!
The problem is, I presume, that although your image shows 15 numbers along in each axes, the total size of your image in pixels is 450 x 450 - and this is what imagesc is using.
So, what you really have is an image with 15 x 15 blocks of 30 x 30 pixels. You can set the axes ticks and labels manually using XTick and XTickLabel:
atick = 15:30:415; %assuming you want the ticks in the centre of each block
set(gca,'XTick',atick);
set(gca,'XTickLabel', 1:15);
set(gca,'YTick',atick);
set(gca,'YTickLabel', 1:15);

Surf command Axis Scaling

I'm having an issue with moving the datum while plotting a 3D surface using the surf. I have a matrix RES with values < 10^-5 (e.g. RES(30,40)=0.000043245). If I plot this matrix using surf:
surf(RES); axis([0 70 0 70 0 0.0008]);
Then I get a nice smooth image.
Now I need to add a constant value of 10 to the entire matrix and then to plot it with a new datum. So I want the exact same plot as the above figure except I want the z-axis to go from 10 to 10.0008. In this case, all the numbers are more like RES(30,40)=10.000043245.
So I try it:
surf(RES+10); axis([0 70 0 70 10 10.0008]);
And I get this really chunky, blocky output.
Does anyone know why it does this? Both figures should look exactly the same shouldn't they? All I've done is move the z-axis up by 10.