How to set the background colour of different legends in plotyy (Matlab)? - 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');

Related

Different color line with plotpc in matlab

I am plotting two plotpc charts (i want to see decision boundaries from perceptron and from Bayesian net) and I need them to have different color.
plotpv(P,T);
hold all;
plotpc(net.IW{1,1},0,'r');
plotpc([w1(maxind(1)), w2(maxind(2))],0,'g');
title('Decision boundaries');
However all my trials ended up with failure and I always get same colors like this:
Thank you for help.
You need to assign the output of plotpc (line handle) to some variable, which you can then use to alter line appearance, e.g.
hPlot = plotpc(net.IW{1,1},0);
set(hPlot, 'Color', 'r');
Also, I don't think plotpc accepts a color as a third argument - you should get a warning when you do that.

Overlapping graphs in matlab

I am drawing several graphs in the same plot in matlab by using the command, hold on.
My problem is that I am drawing points with big markersize on top of some of the graphs, and I would like theses points to have some specific color. Only what happens is that some of them will take some color and some others take another color, my guess is that the colors of my points get mixed with the colors of the graphs on top of which i put them. Is there a mean to ask matlap to overwrite whatever is under my points so that they get the color I assign to them?
Example:
x= 0:1:10;
plot(x,x,'r',x,-x,'b','linewidth',2)
hold on
plot(5,-5,'.',10,10,'.','MarkerColor',[0,0.5,0],'Markersize',24)
hold on
plot(5,5,'.',10,-10,'.','MarkerFaceColor',[0,0.75,0],'MarkerSize', 24)
Imagine that the curves are much more complicated than theses simple lines so no way to start having fun cutting them each time I want to represent a point...
Now my problem is that I would like that the points 5,-5 and 10,10 to have the same color. Namely 0 0.5 0 dark green. but their color get mixed depending on which line they lie.
If I specify their color as '.g' I don't get this problem, but the problem is that I got too many points to get covered by the few number of colors that are labeled by letters (eg 'r' 'b' 'k' etc..).
Thankfully
Note that 'MarkerColor' doesn't exist, so I suppose it's a type and you meant 'MarkerFaceColor', just like in the other plot.
Then, hold on once is enough, you don't need to repeat it every time you want overlap another plot.
Finally, I suggest you simply use 'Color' instead of MarkerFaceColor. This should display your dots with the wanted color.
x= 0:1:10;
plot(x,x,'r',x,-x,'b','linewidth',2)
hold on
plot(5,-5,'.',10,10,'.','Color',[0,0.5,0],'Markersize',24)
plot(5,5,'.',10,-10,'.','Color',[0,0.75,0],'MarkerSize', 24)

Use another variable to color-code single quiver arrows

how would one go about to get and use a color-coding from a variable to color a single quiver3 line?
I append a picture where i made a scatter plot in variable z that i would like the blue line emanating from there to be the same color. Is there a possibility?
Thanks.
Not with standard quiver. There's a file available on the File Exchange called quiverc, which uses the magnitude of the vector as the line color.
It's also not too hard to roll your own:
function colQuiver(xyz, uvw, cdata)
%// this makes it suitable for both 2D and 3D
xyz = num2cell(xyz);
uvw = num2cell(uvw);
for ii = 1:size(xyz,1)
L = cellfun(#(x,y) [0;x] + [y;y], ...
uvw(ii,:), xyz(ii,:),...
'Uniformoutput', false);
L = line(L{:});
set(L, 'color', cdata(ii,:));
end
end
Note that I didn't test this at all, there's no error checking, and you might want to allow other ways of specifying the color than just RGB values (use a LineSpec, via current colormap, ...)...But the essence it there ^_^
In case you are wondering for colored arrows:
QuiverS and Uquiver and Streakarrow3d. But none of them does what i want. They all have the arrows the same length and color coded, however i'd rather have them different lengths and color coded with yet another variable.

Images not being displayed properly

I am working with MATLAB R2012b. I am trying to get 7 images to display on one figure but I can get the image that MATLAB to displays to look exactly like the original file. I set the color map to gray in hopes that you make it look the same but no its still different. I have included both the original and what I get from MATLAB so you can see what is happening.
Here is my code:
w8 = imread('Winter8','jpg');
subplot(2,4,1), image(w8);
title('Winter8.jpg');
axis('off','image');
colormap('gray');
truesize;
And here are the images:
Orirginal:
Result from MATLAB:
Thanks for you help.
imagesc seems to work better than image
imagesc(w8);
colormap('gray');
imagesc makes a nicer looking image in your case because you seem interested in using a gray color map as a filter. You can specify a range with clims, but you don’t have as much control as with image.
If you run colorbar on your figure, you’ll see what I’m talking about.
image would be better to use in a situation where you want much finer control over your data. For example, if you wanted to plot your data in true color instead of with a colormap, it would be easier to hack that together with the image function compared to the imagesc function because you wouldn’t be worried about scaling clims with a true color image.

How to vary the line color of a matlab plot (like colormap)?

I have a 2D space in which a function value is defined (you can think of it as a manifold). Now I plotted the function value using contourf and changed the colormap to something softer than jet. So far it looks quite good.
Now I want to draw a line representing the state over time in my space. That is also possible using the the plot command. But I want some more improvements: There is an additional state that is hidden for now (value 0...50). I would like the line color to change according to this hidden state. So in a sense to apply a separate colormap to the line plotted by plot like for example in a waterfall plot.
Is this (or something similar) possible using matlab?
Thanks
If you want to either use interpolated shading or have the colours change with the colour map, then you want to plot your data as a mesh and set the edgecolor property appropriately. Note that in order to plot it as a mesh, then you will need to duplicate it so that it has a size of at least 2 in each direction.
h = mesh([X(:) X(:)], [Y(:) Y(:)], [Z(:) Z(:)], [C(:) C(:)], ...
'EdgeColor', 'interp', 'FaceColor', 'none');
You may also want to look at the MeshStyle property, if you want to plot multiple lines simultaneously.
This solution is also much nicer than the one used in cline because it only creates one graphics object, rather than n.
Have a look into the cline.m function from file exchange, I think it is exactly what you need.
I can recommend the Colored line entry from the file exchange. It has good feedback and uses the color map to define the displayed colours, I've use it sucessfully on a number of projects.