Draw over legend in a Matlab figure - matlab

I would like to place an arrow over a legend in a matlab plot but when I add the arrow, the legend defaults to being "on top" (see the picture, the black line being covered by the legend).
Is there a way to push a subfigure, such as an arrow, to the "top" so that it appears over all other component of the figure, including the legend? I've tried to use uistack but that doesn't seem to work with legends. uistack as the doc says should "Reorder visual stacking of UI components".
edit:
Very simple example: the line that I'm drawing should appear on top of the legend.
figure;
b = bar(1:3,rand(3));
hold on;
p = plot([0,3],[0,.5],'Color','k','linewidth',1.5); % my arrow
l = legend(b,'value','Location','SouthWest','AutoUpdate','off');
uistack(l,'bottom');

You can make the legend background transparent - so you will see your arrow through the legend
figure;
b = bar(1:3,rand(3));
hold on;
p = plot([0,3],[0,.5],'Color','k','linewidth',1.5); % my arrow
l = legend(b,'value','Location','SouthWest','AutoUpdate','off');
l.BoxFace.ColorData = uint8([255 255 255 127]');
l.BoxFace.ColorType = 'truecoloralpha';
The ColorData Property is [R G B Transparency]
For Info: This was done using R2015b.

You can copyobj the current graphical axes gca and set its Color property to none. This method will draw the line over the legend's patches and associated texts.
Explanation : Copyobj will copy and display all the axes related to the bar and line but not the legend (legends have axes on their own). The display of the copied axes will overlay perfectly with the original one. And 'Color','none' makes the white background of the copied axes transparent, thus making the legend visible again but visible under the line.
Here is the code
f = figure;
b = bar(1:3,rand(3));
hold on;
p = plot([0,3],[0,.5],'Color','k','linewidth',1.5); % my arrow
l = legend(b, 'Location','SouthWest');
% add some magic
hax = copyobj(gca, f); % copy the current axes to the figure
set(hax, 'Color', 'none') % set the new axes's background transparent

What MATLAB version do you use? uistack seems to not work on legends anymore, since MATLAB 2015b (see this similar problem).
If, as you say, the line can be appear at any place, the best workaround may be choosing the best legend location
l = legend(b,'value','Location','Best','AutoUpdate','off');

Related

Change existing plot lines according to colormap

I have a plot with a few lines in Matlab and I want to control the line colors post-hoc:
figure; hold on;
for ind=1:4
plot(rand(1,10))
end
I know I can use
set(0,'DefaultAxesColorOrder',summer(4))
before plotting, to change the plot line colors, but (how) can I achieve the same thing after looking at the plot? Possibly try-out a few different colorschemata?
Each plot by default takes its color from the property 'ColorOrder' of their axis, which in turn is taken by default from the 'DefaultAxesColorOrder' of the groot object.
Once the plots have been created, you need to modify their colors individually; changing the above mentioned properties will not affect them:
g = findobj(gca, 'Type', 'line'); % children of current axis that are lines
c = summer(numel(g)); % desired color scheme, with that many colors
for k = 1:numel(g)
set(g(k), 'color', c(k,:));
end

Setting alpha of colorbar in MATLAB R2014b

I have a colorplot (from imagesc) with an alpha map. I'd like the colorbar to reflect the alpha (notice that in the image below the colormaps are the same). I found solutions online but none seem to work in R2014b.
Code is here:
subplot(2,1,1)
A = imagesc(meshgrid(0:10,0:5));
alpha(A,1)
colorbar
subplot(2,1,2)
B = imagesc(meshgrid(0:10,0:5));
alpha(B,.7)
colorbar
James
You could add a textbox with alpha on top of the colorbar. This works for later versions of MATLAB.
cb=colorbar
annotation('textbox',...
cb.Position,...
'FitBoxToText','off',...
'FaceAlpha',0.5,...
'EdgeColor',[1 1 1],...
'BackgroundColor',[1 1 1]);
In pre-R2014b MATLAB, the colorbar is itself an axis containing an image for which you can set alpha:
hb = findobj(gcf,'Type','axes','Tag','Colorbar');
hi = findobj(hb,'Type','image');
alpha(hi,0.7)
Instead of gcf, use the handles of the individual subplots.
Or save its handle when you make it:
hb = colorbar;
From R2014b on, the colorbar is creating using the new handle graphics system, where there is no longer a child image to modify. The colorbar is created internally with colorbarHGUsingMATLABClasses, which is an obfuscated .p file, so it's no clear how it's constructed.

Paint each graph trace the same color in MATLAB

I have the following MATLAB code and I'm trying to make all of the plot traces black:
x = 20:0.01:30;
m1 = 25;
s1 = 2.5;
pdfNormal_1 = normpdf(x, m1, s1);
m2 = 25.478;
s2 = 0.1637;
pdfNormal_2 = normpdf(x, m2, s2);
m3 = 25.478;
s3 = 0.189;
pdfNormal_3 = normpdf(x, m3, s3);
set(gcf,'color','w');
g=findobj(gca,'Type','patch');
%set(g(1),'FaceColor',[0 .5 .5],'EdgeColor','w')
%set(g(2),'FaceColor',[0 1 1],'EdgeColor','w')
%set(g(3),'FaceColor',[0 1 1],'EdgeColor','w')
set(gca,'Fontsize',12,'Fontname','euclid')
xlabel(' ') %título eixo xx
hold all;
%plot(x, pdfNormal_1, x, pdfNormal_2, x, pdfNormal_3);
%set(gcf,'Color',[0 0 0])
plot(x,pdfNormal_1,'-.')
plot(x,pdfNormal_2,':')
plot(x,pdfNormal_3,'-','LineWidth',2)
Can someone help me? I removed the % from set(...);, but it plots nothing.
I don't quite know what exactly you want. You can either interpret this as:
You wanting all of your lines to be black
You wanting the background pane to be black.
Let's answer each question anyway so we have our bases covered.
Wanting all lines black
For the third parameter in plot, you can use a single letter that specifies what colour you want for your lines within your plot. Therefore, if you want all three of your plots black, use k after each invocation to plot. b is actually reserved for blue. Also, because you are calling plot more than once, each call to plot by default will overwrite the contents of the current figure with the latest invocation to plot, and so if you want all three plots to appear at the same time, you need to use hold on. Therefore, place this at the end of your code:
hold on;
plot(x,pdfNormal_1,'k-.')
plot(x,pdfNormal_2,'k:')
plot(x,pdfNormal_3,'k-','LineWidth',2);
You can also get rid of any set commands as these aren't useful. What you're actually doing is setting the background of the figure to white, which is probably not what you want to do. By background, I mean the area where the axes appear, not the drawing area of the plot itself.
Wanting the background pane black
If you want the background of where the plot appears to be black, it's a very simple one line statement. You need to set the current axes colour, not the current figure. Therefore, replace your set(gcf...) statement with set(gca...). Therefore:
set(gca,'Color',[0 0 0])
Now, if this is what you want, it'll be up to you to figure out what colours will appear on this plot nicely. Red certainly appears nice here!
For more information about how plot works, check out the documentation page on MathWorks: http://www.mathworks.com/help/matlab/ref/plot.html. It's actually very well explained!

matlab remove only top and right ticks with leaving box on

In Matlab figure, I would like to remove ticks only from the top and right axes with keeping the plot box on.
I know if I make the plot box off, the ticks on the top and right go away. But, this is not what I want. In other words, I want to keep ticks only at the bottom and left and, at the same time, want to keep the plot box on.
My workaround similar to #j_kubik proposition:
plot(1:10)
% get handle to current axes
a = gca;
% set box property to off and remove background color
set(a,'box','off','color','none')
% create new, empty axes with box but without ticks
b = axes('Position',get(a,'Position'),'box','on','xtick',[],'ytick',[]);
% set original axes as active
axes(a)
% link axes in case of zooming
linkaxes([a b])
You can use box off to remove the ticks and then draw the box back using plot. For example:
figure
hold on
box off
plot(1:10)
plot([1,10],[10, 10],'k')
plot([10,10],[1,10],'k')
Now in 2022, if anyone is still interested in a quick resolution other than the box option, here is my answer:
figure
plot(1:10) ;
ax = gca ;
ax.Box = 'off' ;
xline(ax.XLim(2),'-k', 'linewidth',ax.LineWidth);
yline(ax.YLim(1),'-k', 'linewidth',ax.LineWidth);

Remove border around Matlab plot

Matlab is displaying a black border around a plot and I would like to remove it. I think i should be using something like:
set(Figure#,'PropertyName',PropertyValue);
But I'm not sure what PropertyName or PropertyValue should be used...
Edit:
I tried all of the suggestions including:
set(gca,'box','off');
set(gca,'xcolor','w','ycolor','w','xtick',[],'ytick',[]);
axis off;
The figure still has a black boarder and looks like this:
Edit 2:
This is a simplified package that reproduces the black box. Run the script called "runPlot". Here it is:
http://dl.dropbox.com/u/8058705/plotTest.zip
I can't figure out why the box is still visible. This might be due to the line in "plotTEC.m"
axis([-.65 .6 .25 1.32]) % hardwiring axis length since the coastline runs off of the plot
#Pursuit: If I use "plot browser" I get a recursive error....I am not familiar with the matlab plotting package but this seems strange. Does anyone else get this error? Again, thank you guys for your advise!
Does anyone have any other suggestions?
Thanks in advance!
You want to experiment with the properties of the axis. Some properties of interest.
xcolor %The color of the x-axis line and the x axis labels
ycolor %
box %'on', or 'off' indicating if one or both sides of a plot should have lines
xtick %Where to place the labels
ytick
For a completely bare plot, use:
figure
set(gca,'xcolor','w','ycolor','w','xtick',[],'ytick',[])
To set the figure background to white as well
set(gcf,'color','w')
Depending on your exact problem, you can try the 'box' property, to see how it affects your plots
set(gca,'box','on')
set(gca,'box','off')
If you want to turn off the actual plots lines but keep the plot labels then I am not aware of a simple solution. I think that I would need to remove the axes as described above, and then manually add labels using the text function.
Edit: As I just learned from this question, Plot Overlay MATLAB you can also try
axis off
axis on
Which I think is equivalent to
set(gca,'visible','off')
set(gca,'visible','on')
Edit 2:
If nothing else works, activate the "plot browser" in your figure. Use "view" --> "plot browser". From the plot browser panel, uncheck each object until you figure out which object is drawing the lines that you need to remove.
Then double click on the offending object to bring up the "property editor" panel, and mostly likely click "More properties" to view all possible properties of that object. From that view you can (hopefully) figure out what object is drawing the offending lines.
After you have figured out the object and property to edit, you can probably figure out where in the code that object is created, and set the property programmatically.
Try:
set(gca, 'Box', 'off');
Solution to remove 'gray' background in imagesc
I = imread('imgname.jpg');
[rows columns] = size(I);
posX = 100; posY = 100; %you can set any value for posX and posY - try to keep it on screen
f = figure (1);
imagesc(I);
set(gcf,'Position',[posX posY columns rows]);
set(gca,'units','pixels');
set(gca,'units','normalized','position',[0 0 1 1]);
axis off;
axis tight;
This should save the image with same size as that of the original, using imagesc.
Cheers!
set( gca , 'Visible' , 'off' );