Overlapping graphs in matlab - 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)

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');

Set legend from looped data points matlab

I would like to scatter plot different sets of data points. Each point should have a different markerfacecolor, but within a given set, they would all have the same markeredgecolor.
I got this to work by looping over single scatter points individually, because markerfacecolor seems to only be able to take scalar points. The way I do it is that I loop through my data, and look for the appropriate color.
This works fine because I can define each point separately, but it becomes a problem when trying to set up the legend. It tries to list all the different points, but what I'd like is just an empty circle (markerfacecolor white or transparent), with each set having their specific markeredgecolor.
I hope this is clear enough. Thank you.
Mike
I've had luck using patches, setting 'FaceColor', 'EdgeColor' to 'none', 'MarkerEdgeColor' to 'flat' and setting the 'FaceVertexCData' to a Nx3 matrix where each row is a color corresponding the the points specified by you Nx1 'XData', 'YData' and 'ZData'.
h = patch('parent',gca,...
'XData',x,...
'YData',y,...
'ZData',z,...
'FaceColor','none',...
'EdgeColor','none',...
'MarkerFaceColor',faceColor,...
'MarkerEdgeColor','flat',... This is what sets the Edge of each marker
'FaceVertexCData',C) % C is a Nx3
I don't have access to Matlab at the moment, and Octave does not seem to have exactly the same functionality. Check out the Matlab patch properties documentation for more information.

How to give different colors when I loop for plot in MATLAB?

I have some data say X with size (100,2). This X is composed of data for 10 categories (set of 10). Now I would like to look the pattern in the data for each category. For this I need to have different colors assigned to each category. I am trying to loop instead of doing 10 different plots. I tried the below.
hold on
for i=1:10:100
plot(X(i:i+9,1),X(i:i+9,2),'.')
end
hold off
This gave me a plot with same color. How can I assign different colors for different range?
The answers mentioning hold all are correct and useful for cycling through the colors specified by the ColorOrder axes property (even though just hold on is now equivalent to hold all). However, by default MATLAB only specifies a short list of colors (just 7 as of R2013b) to cycle through, and on the other hand it can be problematic to find a good set of colors for more data series. For 10 plots, you obviously cannot rely on the default ColorOrder, so a great way to define N visually distinct colors is with the "Generate Maximally Perceptually-Distinct Colors" (GMPDC) submission on the MATLAB Central File File Exchange. It is best described in the author's own words:
This function generates a set of colors which are distinguishable by reference to the "Lab" color space, which more closely matches human color perception than RGB. Given an initial large list of possible colors, it iteratively chooses the entry in the list that is farthest (in Lab space) from all previously-chosen entries.
For example, here are the colors generated when 25 are requested:
The GMPDC submission was chosen on MathWorks' official blog as Pick of the Week a few years ago in part because of the ability to request an arbitrary number of colors (in contrast to MATLAB's built in 7 default colors). They even made the excellent suggestion to set MATLAB's ColorOrder on startup to,
distinguishable_colors(20)
Of course, you can set the ColorOrder for a single axis or simply generate a list of colors to use in any way you like. For example, to generate 10 "maximally perceptually-distinct colors" and use them for 10 plots on the same axis (not using ColorOrder):
% Starting with X of size 100x2
X = reshape(X,10,10,2); % for clarity, column is category, row is observation
mpdc10 = distinguishable_colors(10) % 10x3 color list
hold on
for ii=1:10,
plot(X(:,ii,1),X(:,ii,2),'.','Color',mpdc10(ii,:));
end
Alternatively, using the ColorOrder axis property simplifies the process:
X = reshape(X,10,10,2); % for clarity, and to avoid loop
mpdc10 = distinguishable_colors(10) % 10x3 color map
ha = axes; hold(ha,'on')
set(ha,'ColorOrder',mpdc10)
plot(X(:,:,1),X(:,:,2),'-.') % loop NOT needed, 'Color' NOT needed
APPENDIX
To get the ColorOrder RGB array used for the current axis,
get(gca,'ColorOrder')
To get the default ColorOrder for new axes,
get(0,'DefaultAxesColorOrder')
Example of setting new global ColorOrder with 10 colors on MATLAB start, in startup.m:
set(0,'DefaultAxesColorOrder',distinguishable_colors(10))
The easiest solution is to replace hold on by hold all.
If you want more control you have to manually define your line specifications (more info here) and then pass them to plot:
linespec = {'b.', 'r-', 'g--o'}; % define your ten linespecs in a cell array
hold on
for i=1:10:100
plot(X(i:i+9,1),X(i:i+9,2),linespec{i})
end
hold off
hold on makes sure the new plot command adds to the plot instead of replacing it. However, each command works as if it were generating a fresh plot, including starting with the first line color (blue). If you want subsequent plots use different colors, use hold all instead. That way the standard 7 line colors are used in turn.
Since you have 10 lines to plot, you might want to specify the colors explicitly to make sure they are all different. For that, use the syntax
plot(..., 'Color', [r g b])

Matlab: Gradient color spots according to percentage

I want to paint some edges on my plot using gradient color according to their repetition percentage.
So the most repeated edge on my graph to be red, the next lesser to be orange and the edge with the less repetitions to be light beige.
The Percentage of the repetition can be obtained from a txt file.
The rest area of the plot i would like to stay intact in white color. Something like the next image (consider objects shape and size irrelevant, just the color gradient is what i am interested for).
How can i do this with matlab?
My approach so far:
EDIT it works with the addition of the hold all cmd
for jkl=1:size(edges,1)
plot(edges(jkl,1), edges(jk,2),'^','Color',[edgespercentage(jkl)/100 0 1], 'LineWidth', 2.5,'DisplayName', 'Edges with gradient color'); hold all
end
But as i see plot cant keep each iteration's color and plots at the end the last calculated color only (as expected).
Thank you in advance.
Solution found with the tip of David K (Thank you!)
I'm not sure if this is exactly what you want, but give it a shot:
mesh(xvals,yvals,zvals,repititionVals);
colormap('hot');
You can play around with colormap to get the exact shading you want, but I think either hot or autumn is the closest predefined map to what you're looking for

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.