How can I change the default white for zero for the output of a contour plot to a different color in MATLAB?
longitude = [80 82 95]
latitude = [30 32 35]
temp = [0 0 0; 0 0 0; 0 0 0]
contourf(longitude,latitude,temp)
Thanks,
Amanda
The key seems to be in the "renderer/rendering" mode from the figure properties. It must be set to either 'painters' or 'zbuffer'. I am not an expert and I didn't understand how to set it by default. I changed it manually through the figure editor (click on the outer figure frame --> More properties --> Renderer) and then I clicked "generate code" from the menu bar to make a function that automatically creates all plots in the same way.
Related
Hello i need to make background color black and foreground color white. As u can see i did this with transfering image to 2 dimension. I want to make this color changes in 3 dimension, so we are nor allowed to transfer it bw. Is there any way to do this ?
logo=imread('logo.png');
subplot(2,2,1);
imshow(logo);
b=rgb2gray(logo);
subplot(2,2,2);
imshow(b);
c=im2bw(b,0.92)
subplot(2,2,3);
imshow(c);
c = 1-c;
subplot(2,2,4);
imshow(c);
Preface:
To set the pixel to white or black each layer of the pixel needs to be set to an intensity value of 0 (black) or 255 (white).
White Pixel → rgb(255,255,255)
Black Pixel → rgb(0,0,0)
The colon can be used to obtain all the indices in the 3rd dimension (grab all the layers). To grab one RGB-pixel in the top-left corner of the image:
RGB_Pixel = Image(1,1,:);
Method 1:
If you wish to retain the three colour channels you can use matrix indexing to change the white background to black. Matrix indexing can also be used to change anywhere that isn't white to white. This method may, unfortunately, break down if you have a coloured component with a 255 intensity component. This doesn't seem to be the case for your image though. You can use method 2 for a more safe approach.
logo = imread('logo.png');
[Image_Height,Image_Width,Depth]= size(logo);
new_logo = zeros(Image_Height,Image_Width,Depth);
new_logo(logo == 255) = 0;
new_logo(logo ~= 255) = 255;
imshow(new_logo);
Method 2:
Checks each pixel (RGB-triplet) using a set of for-loops that scan through the entire image. If the RGB-intensities of the pixel are rgb(255,255,255) then the pixels are set to 0 (black). If the RGB-intensities of the pixel are anything else the pixels are set to 255 (white). The ~ismember() function is used to check if the RGB-pixel has an intensity that is not 255 (not-white).
logo = imread('logo.png');
%Grabbing the size of the image%
[Image_Height,Image_Width,~]= size(logo);
for Row = 1: Image_Height
for Column = 1: Image_Width
%Grabbing RGB pixel%
RGB_Pixel = logo(Row,Column,:);
if(~ismember(255,RGB_Pixel))
%RGB pixel is white change
logo(Row,Column,:) = 255;
else
%RGB pixel is coloured change to black%
logo(Row,Column,:) = 0;
end
end
end
imshow(logo);
Using the repmat() function is also a great solution that the above comment suggested. Which possibly may be the quickest method since you already have the code that generates one layer from the greyscale image.
Ran using MATLAB R2019b
In Matlab 2017b, the default heapmap color ranges from light blue to dark blue. How can I make zero values white instead of light blue (it is difficult to distinguish between low numbers and zero in the current form).
cdata = [0 0.005 1; 1 0 0.0006; 0.4 0.20 0.1];
h = heatmap(cdata);
You can change the color map zero values to white by changing the colormap values by
map=colormap(heatmap(cdata));
map(1,:)=1;
And when plotting the figure map use the defined color map as follows
h = heatmap(cdata,'Colormap',map);
I would like to programmatically (or in GUIDE) fix the matlab-uitable to be the whole width of the panel on initialisation. How can I do this?
All I've managed is to change the size in pixels of single columns. I would like the uitable to be 100% the width of the page at that time. When my window is in minimized form the GUI looks fine, but when I maximize it, the uitable is only about half the width of the page. (The uitable's height is not that of the whole panel though, around 1/2 of the whole page)
The GUI is written in matlab-guide.
This is the code that I am trying right now:
data = populateTable;
parentPosition = get(handles.uipanel4, 'position');
uitablePosition = [x y parentPosition(3)-2 parentPosition(4)-2];
set(handles.uitable, 'Position', uitablePosition);
set(handles.uitable, 'Visible', 'on');
set(handles.uitable, 'Data',data, 'ColumnFormat',{'numeric'});
A possible solution could be to set both the position of the figure and of the table to normalized, then to set the position of the table to [0 0 1 1]
f = figure
set(f,'unit','normalized')
set(f,'position',[0.1 0.1 0.5 0.5])
data = rand(13);
for i=1:length(data)
col_names{i}=['Col. # ' int2str(i)];
end
for i=1:length(data)
row_names{i}=['Row. # ' int2str(i)];
end
t = uitable(f, 'Data', data,'unit','normalized', ...
'Position', [0 0 1 1],'columnname',col_names, ...
'rowname',row_names);
You need to set the uitable's position property when you initialize it.
First, get the position of the uitable's parent object. That could be a uipanel or a figure. Assume hParent is a handle to the uitable's parent.
parentPosition = get(hParent, 'position');
Then, create your position vector for the new uitable, and use the parent's width and height. You might want to subtract a few pixels for padding. I'll subtract two pixels here.
uitablePosition = [x y parentPosition(3)-2 parentPosition (4)-2];
You can probably set x and y to 1 or 2 as well, depending on where the table is positioned.
uitable( ... 'units','pixels','position', uitablePosition )
You can implement the same idea in your figure's resize function.
Using M_map package for Matlab, I have created a stereographic projection map of the Arctic. I went on further to create boundary to specify a certain area. Unfortunately, using m_line gives me a straight line from point to point when it should be a curved line. how do I fix this?
figure()
m_proj('stereographic','lat',90,'rad',22,'lon',0)
m_coast('patch',[.5 .5 .5],'edgecolor',[0 0 0]); hold
m_grid ('box', 'fancy', 'ytick',(70:5:90));
[cs,h]=m_tbase('contour',[-250 -1000 -2000 -3000], 'edgecolor', 'k', 'linewidt', 0.00001);
[LongitudeProj, LatitudeProj]=m_ll2xy(longitude, latitude);
bndry_lon=[0 125 125 0 0 ];
bndry_lat=[86 86 90 90 86 ];
m_line(bndry_lon,bndry_lat,'linewi',2,'color','g');
The green line in the image attached is how the code draws the m_line... but the brown line is how it should be drawn since it is a stereographic projection.
I think the function is just made to find points on a map, and connect them with a straight line (As you currently see happening).
A way to get what you want might be to work around this with a polygon that consists of many points, for example:
bndry_lon=[linspace(0,125,100) 125 0 0 ];
bndry_lat=[linspace(86,86,100) 90 90 86 ];
If you believe the function is capable of producing the curved line that you seek directly, please find/create a simple example so we can pick it up from there.
Hi I am trying to costomize my colormap according to tick values of the of the colorbar. I defined my own colorbar with 5 colors eg 5 colors from blue to red
cmap_my=[0 0 1;0 1 1;0 1 0; 1 1 0; 1 0 0];
than i would like that the colour changes for every tick i put. By using cbfit the colors are changing according to the ticks if ticks are evenly distributed in the range of 0:40, but my ticks are
h = colorbar;
set(h,'YTick',[5,10,15,22,30,35,40]);
and my range is up to 45. In addition to that the colors i have defined dont show anymore when i use cbfit. Is there a possibility to give certain ranges for colors and eg from 5 to 10 dark blue 10 to 15 light blue 15 to 22 green 22 to 30 lightyellow 30 to 35 yellow 35 to 40 orange and over 40 red and than makes clear cuts at the ticks?
I hope I understood your problem. Here goes one example. You can work from here for you specific colors.
mri=load('mri');
mri=double(mri.D(:,:,1,13));
%making my image within the same range as yours
mri=(mri-min(mri(:)))./(max(mri(:))-min(mri(:)))*45;
figure,imagesc(mri),axis off,axis image,h_bar=colorbar;
%h_map(1) is related with the min(mri), and h_map(end) with max(mri)
h_map=colormap;
min_mri=0;
max_mri=45;
lim=eps*100;
my_map_int=[0-lim,5-lim,5+lim,10-lim,10+lim,40-lim,40+lim,45+lim];
my_map_color=[0,0,0;0,0,0;0,0,1;0,0,1;0,1,0;0,1,0;1,0,0;1,0,0];
new_map_color(:,1)=interp1(my_map_int,my_map_color(:,1),linspace(min_mri,max_mri,64),'nearest');
new_map_color(:,2)=interp1(my_map_int,my_map_color(:,2),linspace(min_mri,max_mri,64),'nearest');
new_map_color(:,3)=interp1(my_map_int,my_map_color(:,3),linspace(min_mri,max_mri,64),'nearest');
figure,imagesc(mri),axis off,axis image
colormap(new_map_color)
colorbar