Why are the scatterplot colors in MATLAB R2021a different? - matlab

I have installed Matlab R2021a, and when I run the command scatterplot for a vector, I get a figure as below:
I mean black and yellow. However the default colors in older versions is as follows:
I mean the colors are white and blue.
My concern, I need my MATLAB shows the colors of the figure as shown in older version, I mean with white and blue colors.

The behavior indeed changed in R2021a as mentioned in the release notes:
Visual appearance updates to plots generated with eyediagram and scatterplot functions.
The eyediagram and scatterplot functions now provide black plot backgrounds by default.
You can change the colors as desired by you by modifying the properties of axis/figure as shown below:
%Taking the example from the documentation
d = (0:63)';
s = qammod(d,64);
scatterplot(s);
%Modifying the colors
h=gca; %Axis handle
h.Title.Color='k' %Color of title
h.Children.Color='b'; %Color of points
h.YColor='k'; %Y-axis color including ylabel
h.XColor='k'; %X-axis color including xlabel
h.Color ='w'; %inside-axis color
h.Parent.Color='w' %outside-axis color
Without modification, we get this:
After modification, as desired, we get this:

Related

How to set the background colour of different legends in plotyy (Matlab)?

If I execute following code:
figure
t=0:.1:10;
dummy=plotyy(t,sin(t),t,2*cos(t));
set(dummy(1),'ylim',[-1 1]);
set(dummy(2),'ylim',2*[-1 1]);
axes(dummy(1)); xlabel('xlabel'); ylabel('ylabel');
axes(dummy(2)); ylabel('ylabel2');
dummy(1).YTick=[-1:.5:1];
dummy(2).YTick=[-2:1:2];
legend(dummy(1),'Legend1','Location','NorthWest')
legend(dummy(2),'Legend2','Location','NorthEast')
I obtain following figure:
It seems that the default colour of the right-hand legend is grey instead of white. Which command do I need to enter in order to make it white?
It is a good question, I can only assume that MATLAB try's to match some colors for readability or something like that. If you want to know it exactly, maybe MATLAB provides a legend.m-file within the program directory which you could open to have a look at the specific source.
In case you only want to know how to work around it to make both legends white, use this code:
l1 = legend(...);
l2 = legend(...);
set(l2, 'color' 'white');

Matlab Image thresholding

The following code shows an image that is combination of blue and red colors. But if I remove the close statement it yields a grayscale image (which is what I want).
Why does it happen, so that I can avoid it in the future?
I am following this tutorial on youtube.
clear;
animal1=imread('animal1.jpg');
%% GrayScale
animal2=rgb2gray(animal1);
%% scale
bright=animal2*1.5;
imshow(bright);
close;
%% threshold
binary= bright>220;
imagesc(binary);
When you call the imshow function, a new figure is created, and default colormap is set to grayscale. If you do not call close, the imagesc uses the same colormap, and uses gray levels to show the binary image.
Otherwise, the current figure is destroyed, a new one is created, and the imagesc function defines a new colormap. The default colormap in this case is parula, which shades from blue to yellow.
Note that you can display the binary image by using the imshow function directly.
As #dlegland has pointed out, it's an issue with colormaps.
In MATLAB a colormap defines the way that data (in your image, plot, whatever) is mapped to a color on the screen. This is done via a linear mapping which can be different for each axes.
When you call imshow, it is a relatively high-level function which alters a number of properties of the axes on which is it displayed. This includes the colormap, color limits, and other things like the tick marks. In your case, since you fed it a grayscale image (you created using rgb2gray), it set the figure/axes to use the gray colormap.
imagesc, however, is a lower-level function that doesn't make any changes to the current axes with the exception that it alters the color limits to span the entire dynamic range of the image. Because of this, when you use imagesc to plot an image on an axes that was previously used by imshow. It simply uses the colormap that imshow was using (gray).
If imshow hadn't been called, then the figure would be using the default colormap (typically parula) and your image would be displayed using this colormap.
Now the nice thing is that you can change the colormap that is being used with the colormap command. For example to use grayscale, you would do
colormap gray
Or if you wanted to specify that colormap for only a specific axes you could do the following
ax = axes();
colormap(ax, gray)
Your only options aren't gray or parula. MATLAB has a number of built-in colormaps or you can even specify your own custom colormap.

How to setup step colorbar in matlab?

I want to change the default color bar (jet color) generated by Matlab, especially the step of the color (just like the figure below). How to do that?
Here's my code
[hC hC] = contourf(interp2(sal,2,'spline'),[0:0.5:5]);
set(hC,'LineStyle','none','YTick',0:4);
colorbar;
If you are looking to reduce the number of colors in the contour plot and colorbar then you can set a new colormap with a reduce color set.
%Get 10 colors from jet
numColors = 10;
colormap(jet(numColors))
data = peaks;
contourf(data)
% Optionally you can set yTicks in conjunction with the number of items in your colormap to line up
colorbar('YTick',linspace(min(data(:)),max(data(:)),numColors+1))
EDIT:
If you want more control over where the contour lines are drawn then use the function in this form countourf(data,v) where v is an monotonically increasing vector of your desired contour levels. Example:
contourf(data,linspace(-7,8,numColors))
c = colorbar('YTick',linspace(-7,8,numColors+1));
The will draw 10 contour lines at -7, -5.33, -3.66 ... 8. Replace -7 and 8 with whatever you wish ex. min/max of data or whatever makes sense for your application
You can adjust the color bar properties by using:
c=colorbar;
c.Ticks=[vector of tick locations]
or alternately you could try
c.Limits=[min max]
See the MATLAB documentation for colorbar properties: http://www.mathworks.com/help/matlab/ref/colorbar-properties.html?refresh=true
this explains color bar customization in more detail

Matlab Plot with Customize color

does anyone know what's the command or way to plot with different colors besides the preset color, i.e., b,g,r,c,m,y,k? I would prefer to use my own customize colors instead of these....
Cheers
Use the Color property with a ColorSpec triple:
plot(x, y, 'Color', [0.5, 1.0, 0.0], 'LineStyle', '--')
The vector here contains RGB values between 0.0 and 1.0. The Lineseries and ColorSpec documentation entries have more information about this.
From the matlab docs:
ColorOrder: m-by-3 matrix of RGB values
Colors to use for multiline plots. Defines the colors used by the plot
and plot3 functions to color each line plotted. If you do not specify
a line color with plot and plot3, these functions cycle through the
ColorOrder property to obtain the color for each line plotted. To
obtain the current ColorOrder, which might be set during startup, get
the property value:
get(gca,'ColorOrder') Note that if the axes NextPlot property is
replace (the default), high-level functions like plot reset the
ColorOrder property before determining the colors to use. If you want
MATLAB to use a ColorOrder that is different from the default, set
NextPlot to replacechildren. You can also specify your own default
ColorOrder.
All together, this means you want the commands:
figure();
axis();
set(gca, 'colororder', <color matrix>, 'nextplot', 'replacechildren');
plot(x,y);
I had the same problem with the limited set of preset colours MATLAB made available. So, I created this page on my website, where you can choose from a large palette of non-standard colours, and the colour code is pasted directly to your clipboard:
http://www.shirt-ediss.me/matlab-octave-more-colours/

An explanation for this MATLAB code snippet

Consider:
%# load a grayscale image
img = imread('coins.png');
%# display the image
figure
imshow(img,[]);
%# false-color
colormap('hot')
The above code is from here:
Infrared image processing in Matlab
But I don't understand how figure (what's the difference with/without it?) and colormap (how does it affect the already shown image?) work?
figure is not required, imshow just displays img on it. If a figure hadn't been opened, imshow would've created a new one.
The colormap colors the intensities of the image. The hot map colors values in increasing intensity with black, red, yellow, and white-hot. Another popular colormap is jet which has a number of interesting colors.
False colors
So the matrix you want to see has intensities which can have any range of values. For better visualization, the intensities are displayed in a range of colors or a set of false colors. Normally, a grayscale image will display an image is shades of grey, where white is maximum and black is minimum. False color is an extension of that concept with several colors in between (like jet) and an effect of metal being heated in hot.
Colormap at the pixel level
Suppose you have a matrix with pixel values ranging from [cmin xmax]. Now, normalize the values so that the range is [0,1]. Also, suppose you have a color map, such that a range of colors are mapped to some values from 0 to 1 (e.g. 0.5 is mapped to RGB(100,200,100))- then you get the false color mapping by finding the closest intensity in the map and display the corresponding color.
More on colormap in the MATLAB documentation. I've included some picture from that link here:
Jet
(source: mathworks.com)
Bone
alt text http://www.mathworks.com/access/helpdesk/help/techdoc/ref/bone_spine.gif