Comments on: Plot line transparency and color gradient https://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient Charting Matlab's unsupported hidden underbelly Thu, 02 May 2024 08:00:01 +0000 hourly 1 https://wordpress.org/?v=4.4.1 By: alberthttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-457256 Thu, 10 Jan 2019 19:22:12 +0000 https://undocumentedmatlab.com/?p=5200#comment-457256 Hi,

Take a look at this function, it fades a plot and add new data on it.
https://www.mathworks.com/matlabcentral/fileexchange/69816-fadeit

]]>
By: Yaroslavhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-452442 Tue, 20 Nov 2018 12:29:06 +0000 https://undocumentedmatlab.com/?p=5200#comment-452442 Hi Yair,

There is an issue when there are too many points in the plot as Sebastian pointed out above. Below 25000 points, everything works as expected:

% define
N   = 25000;
t   = linspace(0,4*pi,N);
x   = 27*cos(t) + 29*cos(27/2*t);
y   = 27*sin(t) - 29*sin(27/2*t);
 
% plot
hFig  = figure('Units','normalized','Position',[0.2 0.2 0.4 0.5]);
hAxes = axes('XTick',{},'YTick',{},'NextPlot','replacechildren','Box','on');
hPlot = plot(x,y);
axis equal tight;
drawnow(); % as required
%
hPlot.Edge.ColorBinding = 'interpolated';
hPlot.Edge.ColorData    = [uint8(parula(N)*255) 255*ones(N,1,'uint8')]';

Above this magic number, Matlab tries to optimize the figure by plotting only a subset of the vertices. Repeating the procedure with more points than that results in a warning and no color gradient:

N   = 25001;
...
hPlot.Edge.ColorData    = [uint8(parula(N)*255) 255*ones(N,1,'uint8')]';
Warning: Error creating or updating LineStrip
 Error in value of property  ColorData
 Array is wrong shape or size

To fix the issue, we need to probe the number of vertices in hPlot.Edge:

N   = 25001;
...
N2  = size(hPlot.Edge.VertexData,2);
hPlot.Edge.ColorData = [uint8(parula(N2)*255) 255*ones(N2,1,'uint8')]';

But, if we try, say, to resize the figure, the warning reappears (though the gradient stays):

drawnow(); % just in case...
hFig.Position = [0.2 0.2 0.5 0.6];
Warning: Error creating or updating LineStrip
 Error in value of property  ColorData
 Array is wrong shape or size

Similar unexpected behavior occurs in zoom, copyobj etc.

P.S, my Matlab version is R2018a, so it looks like a rather persistent problem.

]]>
By: Ondrejhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-431667 Wed, 04 Jul 2018 10:09:04 +0000 https://undocumentedmatlab.com/?p=5200#comment-431667 Hi Yair,

I am trying to plot a line with a gradient which would be “perpendicular” to the line. For example:

N = 10;
p = plot(1:N, ones(1, N), ‘Linewidth’, 20);
cd = [uint8(jet(N)*255) uint8(ones(N, 1))].’;
drawnow
set(p.Edge, ‘ColorBinding’,’interpolated’, ‘ColorData’,cd)

This is a horizontal line with color gradient going from left to right (in the direction of the sample points). Is there a way to have a gradient going from top to bottom? (if the line is tilted, then the gradient would be still perpendicular to that line). The effect should look like the line is fading out in the perpendicular direction.

Thanks for any hints and ideas.

]]>
By: Yair Altmanhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-425108 Tue, 08 May 2018 11:06:18 +0000 https://undocumentedmatlab.com/?p=5200#comment-425108 @Sina – you need to access the individual contour lines, and then change the lines’ ColorType from ‘truecolor’ to ‘truecoloralpha’, as I explained multiple times on this page.

[c,h] = contour(peaks,'LevelStep',1,'ShowText','on'); drawnow; 
lines = h.EdgePrims;
newColor = uint8([0,0,0,100]');  % 100/255 = 39% transparency
set(lines, 'ColorType','truecoloralpha', 'ColorData',newColor);
]]>
By: Yonihttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-425033 Mon, 07 May 2018 20:51:56 +0000 https://undocumentedmatlab.com/?p=5200#comment-425033 Hi-
I am wondering if anyone else has trouble saving lines made with a color gradient?
Specifically, I am trying to save a vector format (pdf, eps, or similar) with h2b-style multicolor lines. The color gradient lines don’t show up, and other programs (Illustrator) will throw an error when you open the file. Matlab itself has a problem saving to pdf that it doesn’t have with solid lines.
I am looking to get a vector graphic version of one of these figures, any help would be great!
Thanks!

]]>
By: Sina M.https://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-424407 Mon, 30 Apr 2018 12:32:01 +0000 https://undocumentedmatlab.com/?p=5200#comment-424407 Hi,
I would like to define the transparency of a m_contour-plot, meaning the transparency of the contour-line. I have tried it as follows:

[p1, p2] = m_contour(x, y, z, 'color',[0.0 0.5 1.0], 'linewidth',1.5)
hold on
p2.Color(4) = 0.3

This doesn’t work. Is there a possibility to solve that problem?
Thanks!

]]>
By: Yair Altmanhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-415462 Tue, 24 Oct 2017 10:51:08 +0000 https://undocumentedmatlab.com/?p=5200#comment-415462 You cannot get the transparency component via the Color property, you need to use the line’s edge’s ColorData property instead: x = h2b.Edge.ColorData(4) – this returns a uint8 number between 0-255. You can convert it into a 0-1 double value using (for example) double(h2b.Edge.ColorData(4))/255.

]]>
By: Mhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-415447 Tue, 24 Oct 2017 05:28:36 +0000 https://undocumentedmatlab.com/?p=5200#comment-415447 Hi,

Is it possible to load the value of transparency and store it in some variable? e.g. x = h2b.Color(4).
I get the error ‘Index exceeds matrix dimensions.’ when I try this. Is there anyway to load this value?

Thanks!

]]>
By: Yair Altmanhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-410806 Tue, 25 Jul 2017 01:20:41 +0000 https://undocumentedmatlab.com/?p=5200#comment-410806 I don’t think the lines will look disjointed if you ensure that the endpoints are exactly the same.

]]>
By: Vlad Atanasiuhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-410793 Mon, 24 Jul 2017 21:12:37 +0000 https://undocumentedmatlab.com/?p=5200#comment-410793 Correct, Yair. But you will have problems where lines meet: their extremities will look disjoined.

]]>
By: Yair Altmanhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-410719 Sun, 23 Jul 2017 05:20:42 +0000 https://undocumentedmatlab.com/?p=5200#comment-410719 @Vlad – I am not aware of an outline property, but you should be able to replicate it by using two overlapping lines that have different colors/transparency levels, one line slightly wider than the other.

]]>
By: Vlad Atanasiuhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-410718 Sun, 23 Jul 2017 04:32:21 +0000 https://undocumentedmatlab.com/?p=5200#comment-410718 Hello Yair,

Do you known if there is an “outline.Color” property for the “line” object? It would make 3D objects much more readable in terms of what is front and what is back (get rid of the Necker illusion). See, e.g., https://en.wikipedia.org/wiki/File:4Asterane.png

Thanks!

]]>
By: Tomhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-398203 Sat, 31 Dec 2016 04:32:23 +0000 https://undocumentedmatlab.com/?p=5200#comment-398203 Unfortunately it seems that the legend cannot show the corresponding line as a gradient. The line color appears to match one end of the gradient. I tried a bunch of things but wasn’t able to change the lines in the legend to gradients to match the lines in the figure.

]]>
By: Colored longitudinal river profiles « TopoToolboxhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-391550 Tue, 25 Oct 2016 07:53:32 +0000 https://undocumentedmatlab.com/?p=5200#comment-391550 […] I came across when reading through Yair Altman’s undocumented MATLAB blog. He reports on some undocumented line properties that were introduced with MATLAB’s new graphics engine in R2014b. I implemented the surface […]

]]>
By: Yair Altmanhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-387516 Tue, 06 Sep 2016 16:37:04 +0000 https://undocumentedmatlab.com/?p=5200#comment-387516 @Vince – yes:

hLine = plot(x,y,'.g');
hLine.MarkerHandle.FaceColorType = 'truecoloralpha';  % enable transparency
hLine.MarkerHandle.EdgeColorData(4) = 128;  % 255 = opaque (default), 0 = fully trasparent
drawnow
]]>
By: vincehttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-386205 Sun, 21 Aug 2016 07:35:33 +0000 https://undocumentedmatlab.com/?p=5200#comment-386205 Is it possible to fade a cluster of points held in two arrays x,y?

h = plot(x,y,'.g')
h.Color(4) = 0.5?
]]>
By: Yair Altmanhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-383803 Fri, 22 Jul 2016 06:29:02 +0000 https://undocumentedmatlab.com/?p=5200#comment-383803 yes

]]>
By: Jerryhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-383778 Fri, 22 Jul 2016 01:50:53 +0000 https://undocumentedmatlab.com/?p=5200#comment-383778 Great, it works. Thanks!
I guess the reason is that when it runs the setting process, the figure is still unfinished?

]]>
By: Yair Altmanhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-383699 Thu, 21 Jul 2016 07:42:01 +0000 https://undocumentedmatlab.com/?p=5200#comment-383699 @Jerry – adding drawnow; pause(0.1) after the plot command and before setting the color should solve the problem.

]]>
By: Jerryhttps://undocumentedmatlab.com/blog_old/plot-line-transparency-and-color-gradient#comment-383694 Thu, 21 Jul 2016 05:37:03 +0000 https://undocumentedmatlab.com/?p=5200#comment-383694 Hello Yair.
I used your code to draw a transparent line. But there are some problems. I wrote the below codes in a .m file.

xlim([1,5]);
hold('on');
cd = uint8([255,200,250,50,0; 0,50,250,150,200; 0,0,0,100,150; 179,150,200,70,50]);
h2b = plot(2:6,  15:-1:11, '.-r', 'LineWidth',8, 'DisplayName',' 0.7'); 
set(h2b.Edge, 'ColorBinding','interpolated', 'ColorData',cd , 'ColorType','truecoloralpha');

The codes are almost exactly as yours, except that I set the ‘ColorType’ since it is ‘truecolor’ in default.
However, the line is not a transparent one. I checked the h2b.Edge and found that the ColorBinding is still ‘object’. Then I run the last sentence of the above codes in Command Window again. This time it works and the line is transparent as your results.
Could you please tell me why would this happen and how to fix it? Because I would like to use it to generate some pictures and it’s a disaster if I have to run the last sentence in Command Window each time.
Thanks!
PS, the version of my MATLAB is R2015a.

]]>