MATLAB Answers

Colormap for multline plot

조회 수: 123(최근 30일)
Matt 25 Feb 2020
편집: Matt 25 Feb 2020
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?

  댓글 수: 0

댓글을 달려면 로그인하십시오.

채택된 답변

Bhaskar R
Bhaskar R 25 Feb 2020
"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
But you are trying to apply colormap to line data instead of surface, image, patch data objects.

  댓글 수: 0

댓글을 달려면 로그인하십시오.

추가 답변(2개)

Sindar 25 Feb 2020
편집: Sindar 25 Feb 2020
I believe this should work:
mylines = plot(t, x);
For 2019b+, colororder is the function that controls this. I think the way you'd pass a colormap is
but you might need

  댓글 수: 2

Sindar 25 Feb 2020
BTW, lines is the default
Matt 25 Feb 2020
This does not work
Error using

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 25 Feb 2020
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 25 Feb 2020
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."

댓글을 달려면 로그인하십시오.


Community Treasure Hunt

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

Start Hunting!

Translated by