How to prevent axis off from removing the subtitle? Nemely, I just want to remove the x, y axes but not the subtitle itself.
img = imread('cameraman.tif');
figure;
imagesc(img);
title('A');
subtitle('B');
If I add axis off at the end it gives me: (The subtitle B is gone...)
You can do the following:
img = imread('cameraman.tif');
figure;
imagesc(img);
title('A');
subtitle('B');
set(gca,'xtick',[]);
set(gca,'xticklabel',[]);
set(gca,'ytick',[]);
set(gca,'yticklabel',[]);
Related
I am using MATLAB I would like to plot a colourful trajectory on top of a grayscale png image whilst retaining the colour information of the trajectory. For example with the data below, I would like to plot Data B over Image A. Without Data B turning gray and without making the colourbar represent the grayscaled image. Any help would be greatly appreciated!
%Image A
RGB = imread('peppers.png');
I = rgb2gray(RGB);
figure
imshow(I)
hold on
%Data B
x = 1:1000;
y = x;
z = zeros(size(x));
lineColor = x;
surface([x;x], [y;y], [z;z], [lineColor;lineColor],...
'FaceColor', 'no',...
'EdgeColor', 'interp',...
'LineWidth', 8);
cc = colorbar();
Many thanks!
MATLAB doesn't seem to like to do more than one colourmap pet axes. By using hold on we're plotting both the image (gray colormap) and surface (e.g. jet colormap) to the same plot. By default from the non-RGB image in imshow the colormap is set to gray, and so it is the same for the surface plot. Trying to change the colourmap by invoking colormap('jet') changes the colormap for both the image and surface.
Seems that there have been others with the same problem:
https://uk.mathworks.com/matlabcentral/answers/194554-how-can-i-use-and-display-two-different-colormaps-on-the-same-figure
The best solution appears to be defining two seperate axes to the same figure and linking them so that positional information matches. You need to specify which axis to plot to, so imshow has been replaced with imagesc which has more flexibility.Then you can define a different colourmap to each axis. Unfortunately, the colorbar probably won't play ball everytime so you gotta fiddle with its positional information a bit.
In practice for your code:
figure
%Image A
ax1 = axes;
RGB = imread('peppers.png');
I = rgb2gray(RGB);
imagesc(ax1,I)
colormap(ax1,'gray')
%Data B
ax2 = axes;
ax2.Visible = 'off'; % make transparent
x = 1:1000;
y = x;
z = zeros(size(x));
lineColor = x;
h = surface(ax2, [x;x], [y;y], [z;z], [lineColor;lineColor],...
'FaceColor', 'no',...
'EdgeColor', 'interp',...
'LineWidth', 8);
colormap(ax2,'jet') % colourful colourmap
% Gotta reposition the colourbar, tedious bit!
% Position: [left bottom width height]
cb2 = colorbar(ax2,'Position',[.91 .11 .0375 .815]);
linkaxes([ax1,ax2]) % same positions in 1st and 2nd plot
Result:
If we have a figure
plot(x, y);
grid on;
We get something like this
But now, I wish to hide the axis, so I tried the commands below:
axis off
set(gca,'xtick',[])
set(gca,'ytick',[])
set(gca,'visible','off')
Together they successfully hid the axis, but the grid was also deleted!
set(gca, 'xticklabel', []) can hide the label, but not the axis.
So, how do I hide the axis, ticks and labels, leaving only the plot and grid?
You can set Xcolor and Ycolor to none so the axis won't be displayed:
%dummy data
x = [-5:.1:5];
y = normpdf(x,0,1);
plot(x, y);
%grid on
grid on;
%Set the axis color to none.
set(gca,'XColor','none','Ycolor','none')
I'm not sure I understood what you wanted to achieve, but if this is what you meant,
here's how to do it:
function [] = q57076281()
% Plot some data with a grid:
x = linspace(0,2*pi,100);
y = sin(x);
figure(); hP = plot(x,y); hAx = hP.Parent;
grid(hAx, 'on');
% Remove the box:
box(hAx, 'off');
% Hide the labels:
set(hAx, 'XTickLabel', [], 'YTickLabel', []);
% Hide the axes:
hXl = struct(hAx.XAxis).Axle; hXl.Visible = 'off';
hXl = struct(hAx.YAxis).Axle; hXl.Visible = 'off';
% Hide the ticks:
hAx.TickLength = [0,0];
I want to create a plot with pcolor plot with an contour plot on top. Both with different colormaps - pcolor with "hot", the contour with "gray".
I newer Matlab version multiple colormaps are possible.
The code works, however both axis do not overlap, even if the axes positions are in sync.
%% prepare Data
Data2D = peaks(100);
Data2D = Data2D -min(Data2D(:));
Data2D = Data2D/max(Data2D(:)) * 100;
steps = 0:05:100;
xAxis = 1:size(Data2D,2);
yAxis = 1:size(Data2D,1);
figure(1); clf
ax1 = axes;
hold on;
% 3D flat plot
caxis([0 100]);
cmap = fliplr(jet(1000));
colormap(ax1, cmap(1:800,:));
hplot = pcolor(ax1, xAxis, yAxis, Data2D);
shading flat; % do not interpolate pixels
set(ax1,'XLim',[xAxis(1) xAxis(end)]);
set(ax1,'YLim',[yAxis(1) yAxis(end)]);
% colorbar
hcb = colorbar('location','EastOutside');
set(hcb, 'Ylim', [0 100]);
%% contour plot
ax2 = axes; linkaxes([ax1,ax2])
colormap(ax2, flipud(gray(1000)));
[C,hfigc] = contour(ax2, xAxis, yAxis, Data2D,steps);
% Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
set(hfigc, 'LineWidth',1.0);
hold off;
drawnow
If you didn't use ax2.Visible = 'off' you would probably see that the axes' positions are different, since the first axes are squashed to allow room for the colorbar which the second axes don't have.
TL;DR
You need to set the position properties to be equal
ax2.Position = ax1.Position
Demo
You can simulate this with a blank figure:
1.
% Create figure and first axes, which have a colorbar
figure(1)
ax1 = axes();
colorbar('location', 'eastoutside');
Output:
2.
% Add new axes
hold on;
ax2 = axes();
Output (notice the second axes fills the space of the first + colorbar):
3.
% Make the same, so that the second axes also allow for the colorbar
ax2.Position = ax1.Position;
Output (notice thicker numbers showing they are overlapping fully):
I am trying to overlay a colour image on a grey level image. However, when I try to plot the 'colorbar' and set the 'clim'. Matlab always produce a colorbar according to the underneath grey level image.
However, I want to get the colorbar for the overlaid colour image. Any suggestions would be appreciate. Thanks a lot.
%% Example codes:
greyImage = imread('AT3_1m4_08.tif');
colorImage = imread('hestain.png');
figure,
greyImagePlot = image(greyImage); colormap(gray); hold on;
overlayImage = imagesc(colorImage, ...
'CDataMapping', 'scaled', 'HitTest', 'off');
alF = 0.5.*ones(size(colorImage, 1), size(colorImage, 2));
set(overlayImage, 'AlphaData', alF);
colorbar; % This will show a grey scale colorbar not the colour one I want
set('CLim', [0 100]); % Also, the colormap limit here is not working
axis off
axis image
A reference for single figure/multiple colormaps can be found here
http://www.mathworks.fr/support/solutions/en/data/1-GNRWEH/index.html
Using images in particular, one can use 'subimage' function.
I also use 'FreezeColor' and 'cbfreeze' functions from 'matlabcentral' when a home-made solution is too tricky.
http://www.mathworks.com/matlabcentral/fileexchange/7943-freezecolors-unfreezecolors
http://www.mathworks.com/matlabcentral/fileexchange/24371
A straightforward - and lazy - solution for keeping a colorbar across multiple plots within the same axes: first plot the colour image and its colorbar, freeze the colorbar, then plot over the grey level image (no transparency), and finally plot the colour image again (transparency).
Here is a piece of code.
figure;
%first step: RGB image and colorbar
overlayImage = imagesc(colorImage, 'CDataMapping', 'scaled', 'HitTest', 'off');
alF = 0.5.*ones(size(colorImage, 1), size(colorImage, 2));
set(overlayImage, 'AlphaData', alF);
colorbar;
set(gca, 'CLim', [0 100]);
cbfreeze; %from 'COLORMAP and COLORBAR utilities' in Matlab Central
%second step: gray image (no transparency)
greyImagePlot = image(greyImage); colormap(gray); hold on;
%third step: plot colour image
overlayImage = imagesc(colorImage, ...
'CDataMapping', 'scaled', 'HitTest', 'off');
alF = 0.5.*ones(size(colorImage, 1), size(colorImage, 2));
set(overlayImage, 'AlphaData', alF);
axis off
axis image
How to plot something outside the axis with MATLAB? I had like to plot something similar to this figure;
Thank you.
Here is one possible trick by using two axes:
%# plot data as usual
x = randn(1000,1);
[count bin] = hist(x,50);
figure, bar(bin,count,'hist')
hAx1 = gca;
%# create a second axis as copy of first (without its content),
%# reduce its size, and set limits accordingly
hAx2 = copyobj(hAx1,gcf);
set(hAx2, 'Position',get(hAx1,'Position').*[1 1 1 0.9], ...
'XLimMode','manual', 'YLimMode','manual', ...
'YLim',get(hAx1,'YLim').*[1 0.9])
delete(get(hAx2,'Children'))
%# hide first axis, and adjust Z-order
axis(hAx1,'off')
uistack(hAx1,'top')
%# add title and labels
title(hAx2,'Title')
xlabel(hAx2, 'Frequency'), ylabel(hAx2, 'Mag')
and here is the plot before and after:
You can display one axis with the scale you want, then plot your data on another axis which is invisible and large enough to hold the data you need:
f = figure;
% some fake data
x = 0:20;
y = 23-x;
a_max = 20;
b_max = 23;
a_height = .7;
%% axes you'll see
a = axes('Position', [.1 .1 .8 a_height]);
xlim([0 20]);
ylim([0 20]);
%% axes you'll use
scale = b_max/a_max;
a2 = axes('Position', [.1 .1 .8 scale*a_height]);
p = plot(x, y);
xlim([0 20]);
ylim([0 b_max]);
set(a2, 'Color', 'none', 'Visible', 'off');
I had similar problem and I've solved it thanks to this answer. In case of bar series the code is as follows:
[a,b] = hist(randn(1000,1)); % generate random data and histogram
h = bar(b,a); % plot bar series
ylim([0 70]) % set limits
set(get(h,'children'),'clipping','off')% turn off clippings