Remove text Backgroundcolor in matlab plot - matlab

I have added some text in my plot. But I want to move its background to be as all other background which is gray. I know the following:
'Backgroundcolor',[0.7 0.7 0.7]
However, [0.7 0.7 0.7] gives color different than the backgroud. How can I make it same ?!

As #FranckDernoncourt said, background colour is changed to white when you export the figure.
However, you can use this to change the colour of the x-axis to be the same as the background colour of the figure:
h_fig1 = figure;
x = [1:4]; y = [2:2:8];
plot(x,y);
set(gca,'xcolor',get(h_fig1,'color'))
Replace 'xcolor' with 'ycolor' to change the colour of the y-axis.
Alternatively, if it is the axis label you want to change the colour of:
h_fig1 = figure;
x = [1:4]; y = [2:2:8];
plot(x,y);
h_2 = xlabel('x label');
set(h_2,'color',get(h_fig1,'color'))
(Though why you would want to do this, I'm not sure. You might as well just not set the xlabel property.)
Or if it is text you added to the plot, this will set the text colour to the background colour of the figure:
h_fig1 = figure;
x = [1:4]; y = [2:2:8];
plot(x,y);
h_3 = text(x(2),y(2),'string')
set(h_3,'color',get(h_fig1,'color'))

Related

Making a colorbar's frame white while keeping the labels black

How can I make the rectangular line around the box of the colorbar white, but keep the tick labels black?
Reproducible code below:
colormap(flipud(autumn(6))); % Create Colormap
cb = colorbar; % Create Colorbar
cb.Ticks = [.1667 .3334 .5001 .6668 .8335]; % Create ticks
cb.TickLabels = ({'0%','5%','10%','15%','20%'});
cb.TickLength = 0;
cb.Color = 'w'; % Everything becomes white, I only want the rectangular color of the box to be white
You could get the wanted visual effect by overlaying a white annotation box on the colorbar, e.g.
colormap(flipud(autumn(6))); % Create Colormap
cb = colorbar; % Create Colorbar
cb.Ticks = [.1667 .3334 .5001 .6668 .8335]; % Create ticks
cb.TickLabels = ({'0%','5%','10%','15%','20%'});
cb.TickLength = 0;
% cb.Color = 'w'; % Everything becomes white, I only want the rectangular color of the box to be white
annotation('rectangle',cb.Position,'Color','w','FaceColor','none','LineWidth',2)
From the ColorBar properties documentation page we learn that the Color property changes too many things:
Color — Color of tick marks, text, and box outline
So we have to try something else. Fortunately, if you are comfortable with using an undocumented feature, the desired effect can be achieved by modifying the colorbar's Ruler property:
function [] = q66408322()
hF = figure();
hAx = axes(hF);
colormap(hAx, flipud(autumn(6)));
hCb = colorbar(hAx,...
'Ticks', [.1667 .3334 .5001 .6668 .8335],...
'TickLabels', {'0%','5%','10%','15%','20%'}, ...
'TickLength', 0, ...
'Color', 'w');
hCb.Ruler.Color = 'k'; % <<<<<< Solution
Resulting in:

Determine the white point in the image

RGB = imread('originalimg.png');
MyImrgb = reshape(im2double(RGB), [],3);
Imrgb = MyImrgb.^2.2; %gamma correction
[x, y, z] = size(Imrgb);
% ld % rg %yv
ldrgyv2rgbMat = [1,1,0.236748566577269;
1,-0.299338211934457,-0.235643322285071;
1,0.0137437185685517,1]; % B
MyImrgbCol = reshape(Imrgb * ldrgyv2rgbMat, size(RGB));
MyImldrgyvCol = reshape(reshape(MyImrgbCol, [],3) *inv(ldrgyv2rgbMat),size(MyImrgbCol));
imshow(MyImldrgyvCol)
I need to find the white point in the color image in a particular axis say x axis. How do I proceed about it?
In an image, every axis has its own color. The transition point from that color, say for example red to green, the midpoint would be the white point. How do I write a Matlab code to get that point?

python matplotlib styles: How to make custom colored markers + lines using matlab style format string

I am using MATLAB-style formating to change the style of plot lines, like
mystyle = '-r.'
ax1.plot(x1, y1, mystyle)
ax2.plot(x2, y2, mystyle)
...
axN.plot(xN, yN, mystyle)
which draws red line + red dot markers. But how do I specify grey color for lines + markers using MATLAB string?
If it is not feasible, what mystyle should look like so that I can control style of all plots sharing it?
Simply define all properties separately. As grey doesn't exist as predefined color, you need to use an normalized RGB-vector, like [0.2 0.2 0.2]
x = 1:42;
y = sin(x);
plot(x, y, 'color',[0.2 0.2 0.2],'LineStyle','-','Marker','.')
if you want multiple plots with the same style you can save it an cell array and access it with an comma-separated-list:
grey = [0.2 0.2 0.2];
myStyle = {'color',grey,'LineStyle','-','Marker','.','MarkerEdgeColor',grey,'MarkerFaceColor',grey}
plot(x, y, myStyle{:})
Try:
plot(xdata,ydata,'.-','Color',[.5 .5 .5])

Colouring specific pixels in an image

Say I have an image. How can I colour some specific pixels in that image using MATLAB?
Thanks.
RGB Pixels
I'd suggest working with an RGB image, so that you can easily represent color and gray pixels. Here's an example of making two red blocks on an image:
img = imread('moon.tif');
imgRGB = repmat(img,[1 1 3]);
% get a mask of the pixels you want and set an RGB vector to those pixels...
colorMask = false(size(imgRGB,1),size(imgRGB,2));
colorMask(251:300,151:200,:) = true; % two discontiguous blocks
colorMask(50:100,50:100,:) = true;
redPix = permute([255 0 0],[1 3 2]);
imgRGB(repmat(colorMask,[1 1 3])) = repmat(redPix, numel(find(colorMask)),1);
AlphaData image property
Another cool way of doing this is with an image's AlphaData property. See this example on a MathWorks blog. This essentially turns color on or off in certain parts of the image by making the gray image covering the color image transparent. To work with a gray image, do like the following:
img = imread('moon.tif');
influenceImg = abs(randn(size(img)));
influenceImg = influenceImg / (2*max(influenceImg(:)));
imshow(img, 'InitialMag', 'fit'); hold on
green = cat(3, zeros(size(img)), ones(size(img)), zeros(size(img)));
h = imshow(green); hold off
set(h, 'AlphaData', influenceImg)
See the second example at the MathWorks link.

How to shift position of colorbar in 3d plot

I'm displaying a 3D scatterplot, but label on the z-axis is getting overlapped by the color bar. How can I shift the color bar a desired number of pixels to the right or left?
You can use findobjto get an handle to the colorbar, query its current position via get and then modify according to your needs and change it using set:
h = findobj('tag', 'Colorbar');
pos = get(h, 'position')
% modify pos according to your needs
set(h, 'position', pos)