does anyone know what's the command or way to plot with different colors besides the preset color, i.e., b,g,r,c,m,y,k? I would prefer to use my own customize colors instead of these....
Cheers
Use the Color property with a ColorSpec triple:
plot(x, y, 'Color', [0.5, 1.0, 0.0], 'LineStyle', '--')
The vector here contains RGB values between 0.0 and 1.0. The Lineseries and ColorSpec documentation entries have more information about this.
From the matlab docs:
ColorOrder: m-by-3 matrix of RGB values
Colors to use for multiline plots. Defines the colors used by the plot
and plot3 functions to color each line plotted. If you do not specify
a line color with plot and plot3, these functions cycle through the
ColorOrder property to obtain the color for each line plotted. To
obtain the current ColorOrder, which might be set during startup, get
the property value:
get(gca,'ColorOrder') Note that if the axes NextPlot property is
replace (the default), high-level functions like plot reset the
ColorOrder property before determining the colors to use. If you want
MATLAB to use a ColorOrder that is different from the default, set
NextPlot to replacechildren. You can also specify your own default
ColorOrder.
All together, this means you want the commands:
figure();
axis();
set(gca, 'colororder', <color matrix>, 'nextplot', 'replacechildren');
plot(x,y);
I had the same problem with the limited set of preset colours MATLAB made available. So, I created this page on my website, where you can choose from a large palette of non-standard colours, and the colour code is pasted directly to your clipboard:
http://www.shirt-ediss.me/matlab-octave-more-colours/
Related
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
I have a data set, contained in three vectors say xx, yy and zz. I want to plot yy vs xx with the marker color face according to zz, so I use the scatter function such as:
scatter(xx,yy,50,zz,'s','filled')
Unfortunately zz has some very extreme values, so I cannot see any difference in the marker face color: all the dots are dark blue!
Is there a possibility to solve this issue? I was thinking of a possibility to impose a lower and an upper value for the color scale, so that any dot with a zz value out of the authorized range would be grey (or of the color of the closest bound)...?
Thank you for your help!
You can try changing the CLim property of the axes.
This example uses the MatLab example data seamount an changes the colorscale range
from the original [-4250 -490]
to the new [-1000 -100]
Default color scale
load seamount
figure
scatter(x,y,5,z)
colorbar
Modified color scale
figure
scatter(x,y,5,z)
set(gca,'clim',[-1000 -100])
colorbar
Default color scale
Nodified color scale
I want to change the color of my curve ploted by fnplt. The document here makes it sound incapable of setting colors.
However, I tried fnplt(s, 'k'); the color of my line is indeed set to black. Then, I tried setting the line to any RGB color by fnplt(s, [0.5 0.5 0.5]), which doesn't work.
The solution given here doesn't work either, because if you do h = fnplt(s), h will be the points to be plotted, instead of the line handle.
Unfortunately, fnplt does not accept the standard Color parameter/value pair for a line plot and also doesn't actually store any graphics handles for the plots that it creates. What you can do is get a list of all plot handles before and after you call fnplt to get the handles to the objects that were created and then manipulate their properties directly.
% Get a list of all graphics within the current axes
before = findall(gca);
% Plot your function
fnplt(s);
% Figure out all of the graphics that were added to the axes by fnplt
added = setdiff(findall(gca), before);
% Alter their appearance.
set(added, 'Color', [1 0 0])
The following code shows an image that is combination of blue and red colors. But if I remove the close statement it yields a grayscale image (which is what I want).
Why does it happen, so that I can avoid it in the future?
I am following this tutorial on youtube.
clear;
animal1=imread('animal1.jpg');
%% GrayScale
animal2=rgb2gray(animal1);
%% scale
bright=animal2*1.5;
imshow(bright);
close;
%% threshold
binary= bright>220;
imagesc(binary);
When you call the imshow function, a new figure is created, and default colormap is set to grayscale. If you do not call close, the imagesc uses the same colormap, and uses gray levels to show the binary image.
Otherwise, the current figure is destroyed, a new one is created, and the imagesc function defines a new colormap. The default colormap in this case is parula, which shades from blue to yellow.
Note that you can display the binary image by using the imshow function directly.
As #dlegland has pointed out, it's an issue with colormaps.
In MATLAB a colormap defines the way that data (in your image, plot, whatever) is mapped to a color on the screen. This is done via a linear mapping which can be different for each axes.
When you call imshow, it is a relatively high-level function which alters a number of properties of the axes on which is it displayed. This includes the colormap, color limits, and other things like the tick marks. In your case, since you fed it a grayscale image (you created using rgb2gray), it set the figure/axes to use the gray colormap.
imagesc, however, is a lower-level function that doesn't make any changes to the current axes with the exception that it alters the color limits to span the entire dynamic range of the image. Because of this, when you use imagesc to plot an image on an axes that was previously used by imshow. It simply uses the colormap that imshow was using (gray).
If imshow hadn't been called, then the figure would be using the default colormap (typically parula) and your image would be displayed using this colormap.
Now the nice thing is that you can change the colormap that is being used with the colormap command. For example to use grayscale, you would do
colormap gray
Or if you wanted to specify that colormap for only a specific axes you could do the following
ax = axes();
colormap(ax, gray)
Your only options aren't gray or parula. MATLAB has a number of built-in colormaps or you can even specify your own custom 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.