Colormap for multline plot

432 次查看(过去 30 天)
How do I change the colormap of a multiline plot and have it change the colors of the existing lines? I am using Matlab 2018b. Here is a simple example.
x = rand(10,1000);
t = 0:999;
plot(t, x)
colormap winter
colormap white
colormap flag
fh = gcf;
fh.Colormap = colormap('flag');
fh.Children(1).Colormap = colormap('flag');
For me, the colors of the lines do not change. The following also did not work, the lines are plotted using the "default" color scheme.
fh2 = figure;
fh2.Colormap = colormap('flag');
ax = axes(fh);
ax = axes(fh2);
ax.Colormap = colormap('flag');
plot(ax, t, x)
Invoking "colorbar" shows a colorbar using the desired colormap. Using colormap with surf(peaks) does change the color of that plot. How do I change the colormap of a 2D multiline plot?

采纳的回答

Bhaskar R
Bhaskar R 2020-2-25
"A colormap is matrix of values between 0 and 1 that define the colors for graphics objects such as surface, image, and patch objects. MATLAB® draws the objects by mapping data values to colors in the colormap." from https://in.mathworks.com/help/matlab/ref/colormap.html
But you are trying to apply colormap to line data instead of surface, image, patch data objects.

更多回答(3 个)

Abby Skofield
Abby Skofield 2023-9-20
As @Walter Roberson mentioned, you can use the colororder command (starting in R2019b) to specify a color palette for multiple plots. Starting in R2023b, you can use one of several predefined palette names like 'reef', 'meadow' and 'glow' in the colororder command:
x = 0:0.1:20;
y = zeros(5,201);
for i = 0:4
y(i+1,:) = besselj(i,x);
end
plot(x,y,LineWidth = 2)
colororder('reef')
title('reef colororder')
The difference between "colororder" and "colormap" is sometimes a bit confusing. Colormaps are useful when color is used to indicate a value in a continous range, whereas colororder is useful when you simply want to differentate between different plots. For example, wtih the seamount data set, we can use a colormap to illustrate the depth of the ocean floor at each scatter point. More similar colors (red:orange) indicate depth measurements that are more similar, while further colors (red:blue) indicate depth measurements that are further apart.
load seamount.mat
scatter(x,y,[],z,"filled");
colormap turbo
title("Colormap Example")
cb = colorbar;
title(cb,"Depth (m)")
In this second example with the fisheriris dataset, the three scatter series are automatically asigned a different color from the colororder so we can differentiate measurements from the different species of iris. Nothing about the colors is meant to indicate any relationship between the series - there are simply three different species in the visualization and our only requirement is to tell which is which.
load fisheriris
scatter(meas(species=="setosa",1),meas(species=="setosa",2),"filled")
hold on
scatter(meas(species=="virginica",1),meas(species=="virginica",2),"filled")
scatter(meas(species=="versicolor",1),meas(species=="versicolor",2),"filled")
colororder dye
title("Color Order Example")
legend("setosa","virginica","versicolor")

Sindar
Sindar 2020-2-25
编辑:Sindar 2020-2-25
I believe this should work:
mylines = plot(t, x);
set(mylines,{'Color'},flag(length(mylines)))
For 2019b+, colororder is the function that controls this. I think the way you'd pass a colormap is
colororder(flag)
but you might need
colororder(flag(100))
  4 个评论
Mathias
Mathias 2021-5-31
This doesn't work for me in Matlab 2017b:
mylines = plot(t, x);
set(mylines,{'Color'},flag(length(mylines)))
Error using matlab.graphics.chart.primitive.Line/set
Invalid parameter/value pair arguments.
But this works:
myCmapCell = num2cell(myCmapArray,2);
mylines = plot(t, x);
[mylines.Color] = myCmapCell{:};
Walter Roberson
Walter Roberson 2021-5-31
mylines = plot(t, x);
set(mylines,{'Color'}, num2cell(flag(length(mylines)),2))
when you use a cell array of parameter names, like {'Color'}, then you need a cell array of values to set.
This is an appropriate way to set the entries to different colors.
If you were to try
set(mylines, 'Color', something)
then that would be attempting to set the Color parameter of all the entries in mylines to the same value stored in something -- for example, 'Color', 'g' to set them all to green.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2020-2-25
line() colors are not affected by the colormap, ever. They are affected by the axes ColorOrder property.
In the release you are using, changing the ColorOrder property never affects lines that have already been drawn: you have to set the Color property of each of the line() objects that already exists.
Starting in r2019b, there is a new colororder() function that can set the axes ColorOrder property. As well, by default changing the ColorOrder will affect lines that have already been drawn. (I do not recall at the moment how to turn that off)
  1 个评论
Matt
Matt 2020-2-25
编辑:Matt 2020-2-25
Looks like another reason to roll to the newer release. After reading the documentation tagged to the functions you mention I saw this wonderful statement "Changing the order has no effect on existing plots. However, many graphics functions reset the color order back to the default value before plotting."

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by