Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Plot line transparency and color gradient

November 13, 2014 50 Comments

In the past few weeks, I discussed the new HG2 axes Backdrop and Baseline properties with their associated ability to specify the transparency level using a fourth (undocumented) element in their Color.
In other words, color in HG2 can still be specified as an RGB triplet (e.g., [1,0,0] to symbolize bright red), but also via a 4-element quadruplet RGBA, where the 4th element (Alpha) signifies the opacity level (0.0=fully transparent, 0.5=semi-transparent, 1.0=opaque). So, for example, [1, 0, 0, 0.3] means a 70%-transparent red.
This Alpha element is not documented anywhere as being acceptable, but appears to be supported almost universally in HG2 wherever a color element can be specified. In some rare cases (e.g., for patch objects) Matlab has separate Alpha properties that are fully documented, but in any case nowhere have I seen documented that we can directly set the alpha value in the color property, especially for objects (such as plot lines) that do not officially support transparency. If anyone finds a documented reference anywhere, please let me know – perhaps I simply missed it.
Here is a simple visualization:

xlim([1,5]);
hold('on');
h1a = plot(1:5,     11:15, '.-', 'LineWidth',10, 'DisplayName',' 0.5');
h1b = plot(1.5:5.5, 11:15, '.-', 'LineWidth',10, 'DisplayName',' 1.0', 'Color',h1a.Color);  % 100% opaque
h1a.Color(4) = 0.5;  % 50% transparent
h2a = plot(3:7,  15:-1:11, '.-r', 'LineWidth',8, 'DisplayName',' 0.3'); h2a.Color(4)=0.3;  % 70% transparent
h2b = plot(2:6,  15:-1:11, '.-r', 'LineWidth',8, 'DisplayName',' 0.7'); h2b.Color(4)=0.7;  % 30% transparent
h2c = plot(1:5,  15:-1:11, '.-r', 'LineWidth',8, 'DisplayName',' 1.0');  % 100% opaque = 0% transparent
legend('show','Location','west')

xlim([1,5]); hold('on'); h1a = plot(1:5, 11:15, '.-', 'LineWidth',10, 'DisplayName',' 0.5'); h1b = plot(1.5:5.5, 11:15, '.-', 'LineWidth',10, 'DisplayName',' 1.0', 'Color',h1a.Color); % 100% opaque h1a.Color(4) = 0.5; % 50% transparent h2a = plot(3:7, 15:-1:11, '.-r', 'LineWidth',8, 'DisplayName',' 0.3'); h2a.Color(4)=0.3; % 70% transparent h2b = plot(2:6, 15:-1:11, '.-r', 'LineWidth',8, 'DisplayName',' 0.7'); h2b.Color(4)=0.7; % 30% transparent h2c = plot(1:5, 15:-1:11, '.-r', 'LineWidth',8, 'DisplayName',' 1.0'); % 100% opaque = 0% transparent legend('show','Location','west')

Transparent HG2 plot lines
Transparent HG2 plot lines

Now for the fun part: we can make color-transition (gradient) effects along the line, using its hidden Edge property:

>> h2b.Edge.get
          AlignVertexCenters: 'off'
             AmbientStrength: 0.3
                ColorBinding: 'object'
                   ColorData: [4x1 uint8]
                   ColorType: 'truecoloralpha'
             DiffuseStrength: 0.6
            HandleVisibility: 'off'
                     HitTest: 'off'
                       Layer: 'middle'
                   LineStyle: 'solid'
                   LineWidth: 8
               NormalBinding: 'none'
                  NormalData: []
                      Parent: [1x1 Line]
               PickableParts: 'visible'
    SpecularColorReflectance: 1
            SpecularExponent: 10
            SpecularStrength: 0.9
                   StripData: [1 6]
                     Texture: []
                  VertexData: [3x5 single]
               VertexIndices: []
                     Visible: 'on'
       WideLineRenderingHint: 'software'
>> h2b.Edge.ColorData  %[4x1 uint8]
ans =
  255
    0
    0
  179

>> h2b.Edge.get AlignVertexCenters: 'off' AmbientStrength: 0.3 ColorBinding: 'object' ColorData: [4x1 uint8] ColorType: 'truecoloralpha' DiffuseStrength: 0.6 HandleVisibility: 'off' HitTest: 'off' Layer: 'middle' LineStyle: 'solid' LineWidth: 8 NormalBinding: 'none' NormalData: [] Parent: [1x1 Line] PickableParts: 'visible' SpecularColorReflectance: 1 SpecularExponent: 10 SpecularStrength: 0.9 StripData: [1 6] Texture: [] VertexData: [3x5 single] VertexIndices: [] Visible: 'on' WideLineRenderingHint: 'software' >> h2b.Edge.ColorData %[4x1 uint8] ans = 255 0 0 179

The tricky part is to change the Edge.ColorBinding value from its default value of ‘object’ to ‘interpolated’ (there are also ‘discrete’ and ‘none’). Then we can modify Edge.ColorData from being a 4×1 array of uint8 (value of 255 corresponding to a color value of 1.0), to being a 4xN matrix, where N is the number of data points specified for the line, such that each data point along the line will get its own unique RGB or RGBA value. (the data values themselves are kept as a 3xN matrix of single values in Edge.VertexData).
So, for example, let’s modify the middle (30%-transparent) red line to something more colorful:

>> cd=uint8([255,200,250,50,0; 0,50,250,150,200; 0,0,0,100,150; 179,150,200,70,50])
cd =
  255  200  250   50    0
    0   50  250  150  200
    0    0    0  100  150
  179  150  200   70   50
>> set(h2b.Edge, 'ColorBinding','interpolated', 'ColorData',cd)

>> cd=uint8([255,200,250,50,0; 0,50,250,150,200; 0,0,0,100,150; 179,150,200,70,50]) cd = 255 200 250 50 0 0 50 250 150 200 0 0 0 100 150 179 150 200 70 50 >> set(h2b.Edge, 'ColorBinding','interpolated', 'ColorData',cd)

HG2 plot line color, transparency gradient
HG2 plot line color, transparency gradient

As you can see, we can interpolate not only the colors, but also the transparency along the line.
Note: We need to update all the relevant properties together, in a single set() update, otherwise we’d get warning messages about incompatibilities between the property values. For example:

>> h2b.Edge.ColorBinding = 'interpolated';
Warning: Error creating or updating LineStrip
 Error in value of property ColorData
 Array is wrong shape or size
(Type "warning off MATLAB:gui:array:InvalidArrayShape" to suppress this warning.)

Markers

Note how the markers are clearly seen in the transparent lines but not the opaque ones. This is because the markers have the same color as the lines in today’s example. Since the lines are wide, the markers are surrounded by pixels of the same color. Therefore, the markers are only visible when the surrounding pixels are less opaque (i.e., lighter).
As a related customization, we can control whether the markers appear “on top of” (in front of) the line or “beneath” it by updating the Edge.Layer property from ‘middle’ to ‘front’ (there is also ‘back’, but I guess you won’t typically use it). This is important for transparent lines, since it controls the brightness of the markers: “on top” (in front) they appear brighter. As far as I know, this cannot be set separately for each marker – they are all updated together.
Of course, we could always update the line’s fully-documented MarkerSize, MarkerFaceColor and MarkerEdgeColor properties, in addition to the undocumented customizations above.
Next week I will describe how we can customize plot line markers in ways that you never thought possible. So stay tuned 🙂

Related posts:

  1. Plot markers transparency and color gradient – Matlab plot-line markers can be customized to have transparency and color gradients. ...
  2. Undocumented plot marker types – Undocumented plot marker styles can easily be accesses using a hidden plot-line property. ...
  3. Plot LineSmoothing property – LineSmoothing is a hidden and undocumented plot line property that creates anti-aliased (smooth unpixelized) lines in Matlab plots...
  4. Plot legend customization – Matlab plot legends and their internal components can be customized using a variety of undocumented properties that are easily accessible. ...
  5. Accessing hidden HG2 plot functionality – In HG2, some of the plot functionality is hidden in undocumented properties. ...
  6. Draggable plot data-tips – Matlab's standard plot data-tips can be customized to enable dragging, without being limitted to be adjacent to their data-point. ...
Handle graphics HG2 Hidden property Pure Matlab Undocumented feature
Print Print
« Previous
Next »
50 Responses
  1. Paul November 14, 2014 at 11:28 Reply

    Have you managed to get the same working for markers? I can’t set the alpha component for MarkerFaceColor, and setting it for Color doesn’t even affect the marker edge:

    >> x = plot(X, Y, 'o', 'MarkerFaceColor', [0.5, 0.5, 0.5]);
    >> x.Color(4) = 0.1;
    >> x.MarkerFaceColor(4) = 0.5;
    While setting the 'MarkerFaceColor' property of Line:
    Color value must be a 3 element numeric vector

    >> x = plot(X, Y, 'o', 'MarkerFaceColor', [0.5, 0.5, 0.5]); >> x.Color(4) = 0.1; >> x.MarkerFaceColor(4) = 0.5; While setting the 'MarkerFaceColor' property of Line: Color value must be a 3 element numeric vector

    I’m using 2013b, maybe it’s different in later versions?

    • Yair Altman November 15, 2014 at 12:29 Reply

      @Paul – I plan to cover Marker customizations (including transparency) in next week’s post. Stay tuned 🙂

  2. Yaroslav November 16, 2014 at 11:05 Reply

    That’s absolutely splendid, @Yair!

    All those years I used a 2×N mesh in order to make a gradient plot, and it turns out to be so simple in HG2. Keep up the good work!

  3. Plot markers transparency and color gradient | Undocumented Matlab November 19, 2014 at 09:42 Reply

    […] opens up Matlab plots to a whole new spectrum of customizations that were more difficult (although not impossible) to achieve earlier.Throughout today, we’ve kept the default FaceColorType/EdgeColorType […]

  4. Leslie February 3, 2015 at 18:59 Reply

    Is it possible to set the transparency of 2D contourf plot fills? I used to be able to do this before 2014b, but the latest update broke what I was using. I used allchild to get handles of the subobjects and set the FaceAlpha to less than 1

    • Yair Altman February 4, 2015 at 00:35 Reply
      [~, hContour] = contourf(peaks(20), 10);
      hFaces = hContour.FacePrims;
      for faceIdx = 1 : numel(hFaces)
         hFaces(faceIdx).ColorType = 'truecoloralpha';  % default = 'truecolor'
         hFaces(faceIdx).ColorData(4) = 150;   % default=255
      end

      [~, hContour] = contourf(peaks(20), 10); hFaces = hContour.FacePrims; for faceIdx = 1 : numel(hFaces) hFaces(faceIdx).ColorType = 'truecoloralpha'; % default = 'truecolor' hFaces(faceIdx).ColorData(4) = 150; % default=255 end

      • Leslie February 4, 2015 at 08:14

        Great, many thanks for this. When I try that code, hFaces comes out as 0x0. When I insert a pause(0.05) before the hFaces = hContour.FacePrims line it works. Strange.

      • Yair Altman February 4, 2015 at 08:16

        I assume that drawnow will also work…

      • Leslie February 4, 2015 at 08:28

        Yes, it does. Thanks again.

  5. matt dash February 5, 2015 at 11:04 Reply

    This is great, but I find that when i set an alpha for a line color, the line smoothing goes away and i get the old fashioned pixelated lines. Have you noticed that too or is it just me?

  6. Evaluate March 4, 2015 at 13:20 Reply

    It seems that the workaround doesn’t work on 2014a edition!
    I receive “Attempt to reference field f non-structure array.”
    Any comments?

    • Yair Altman March 4, 2015 at 14:49 Reply

      Of course it doesn’t – as stated in the top paragraph, this article (which is part of a series) only discusses HG2, in other words R2014b and later. R2014a still uses the previous version of HG, which is radically different.

  7. James April 21, 2015 at 12:43 Reply

    I’ve been able to make some beautiful figures using HG2 thanks to your help. Unfortunately, I am now unable to find a way to export to any sort of vector format that maintains my transparent lines (among other issues). I have tried export_fig, and exporting to .svg. When I do the export, I simply get the figure without my edited properties like alpha and color interpolation. Have you found any way around this? It’s driving me crazy and, from an academic standpoint, appears to make many of the advances of HG2 frustratingly useless. At this point I would put up with just about anything that would get me a super-high resolution image out.

    • Yair Altman April 21, 2015 at 14:19 Reply

      @James – try using the opengl (rather than painters) renderer (the ‘-opengl’ parameter in export_fig). Note that this might take a long time, consume much memory, and generate a huge output file.

      Also try using bitmap output with a large resolution (the -r# parameter) – it would not be vectorized, but at least it might have a large-enough resolution to be publication quality.

    • James April 22, 2015 at 05:39 Reply

      Hi Yair,

      Thanks for the quick response. The opengl renderer works for .png or .bmp format, yes, but when I change the resolution the output changes from my pretty figure with transparency, etc. to essentially the plot before I edit any of the color and alpha properties (still not working for vector outputs).

      What I’ve gathered so far is that this may be related the necessity of using the drawnow command in between plotting the line and getting the line marker handles. For example, if I do not use drawnow after plotting the line, I am unable to access the line.MarkerHandle properties. I do not need the drawnow to do to things such as changing the grid lines (so documented properties related to the axes rather than the line). When I try to render with vector graphics or .bmp with increased resolution, I get only the changes I made that don’t rely on the drawnow (so it renders the grid lines, but not the color and alpha values based on line.MarkerHandle.EdgeColorData).

      I have no current idea how to fix this. I wonder if it has to do with the way the exporter “re-renders” the figure for export, and if it’s as simple as getting it to add another drawnow in there somewhere so that it reads the MarkerHandle changes. I don’t know if export_fig would be able to deal with such a change. I guess any further advice, tips or even a solution would be greatly appreciated.

      Thanks again!

      • Yair Altman April 22, 2015 at 06:10

        @James – I suspect that you will find the same behavior with Matlab’s built-in print function, and in this case there is not much that export_fig can do since it uses print internally.

    • James April 22, 2015 at 09:28 Reply

      Thanks. I guess these hidden properties are unfortunately still useless when it comes to vector graphics (or even high quality non-vector formats unless you have a high-res display and effectively take a screenshot). Hopefully this gets updated soon.

  8. Sebastian June 16, 2015 at 04:54 Reply

    Quick comment: on my Computer (Win7, Matlab 2015a) the above syntax only works for lines up to exactly 25000 data points. Also, changing the ColorBinding and setting the ColorData is only possible, if the line has already been drawn. Therefore, it is necessary to issue a drawnow before setting these parameters:

    figure
    i = 25000;  % Will not work if changed to 25001 (at least on my computer)
    h = plot(rand(i,1));
    c = uint8(randi(255,4,i));
    drawnow % Will not work, if this command is not issued
    set(h.Edge,'ColorBinding','interpolated','ColorData',c)

    figure i = 25000; % Will not work if changed to 25001 (at least on my computer) h = plot(rand(i,1)); c = uint8(randi(255,4,i)); drawnow % Will not work, if this command is not issued set(h.Edge,'ColorBinding','interpolated','ColorData',c)

    • Yaroslav November 20, 2018 at 14:29 Reply

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

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

      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

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

      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];

      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

      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.

      • matt April 13, 2021 at 03:21

        There is no Edge in quiver, so this workaround will not help.

  9. Moshe September 5, 2015 at 13:00 Reply

    Hi Yair,

    I tried to use the code for varying the transparency and for some reason the following example code does not work as expected:

    close all
    t = linspace(0,2*pi,10);
    h = plot(t, sin(t), '-', 'LineWidth',10);
    drawnow;
    transp = 100*ones(size(t)); %abs(round(cos(t)*255));
    transp(1:5) = 200;
    h.Edge.ColorData
    newColor = repmat(h.Edge.ColorData, 1, numel(transp));
    newColor(4,:) = transp';
    newColor = uint8(newColor);
    set(h.Edge, 'ColorBinding','interpolated', 'ColorData',newColor);
    drawnow;

    close all t = linspace(0,2*pi,10); h = plot(t, sin(t), '-', 'LineWidth',10); drawnow; transp = 100*ones(size(t)); %abs(round(cos(t)*255)); transp(1:5) = 200; h.Edge.ColorData newColor = repmat(h.Edge.ColorData, 1, numel(transp)); newColor(4,:) = transp'; newColor = uint8(newColor); set(h.Edge, 'ColorBinding','interpolated', 'ColorData',newColor); drawnow;

    I expected first 5 points to have transparency of 200 and then 5 with transparency of 100. Can you explain why the result is with same transparency all over ?

    Thanks,
    -Moshe.

    • growl October 13, 2015 at 13:21 Reply

      I have the same problem.

      I wrote code essentially the same as yours.

      newColor(1:3,:) works as expected.

      newColor(4,:) does not change transparency.

      Not sure how to fix it, but I gave up trying.

  10. John Thompson July 16, 2016 at 03:03 Reply

    Hello Yair,

    Thanks for the useful tip.

    I was am having trouble creating a figure with multiple lines and having the color encoding remaining in the plot.

    I am plotting multiple lines in a for loop. If I dbstep through each iteration of the loop, then the color will show up, however, when the loop completes the gradient colors go away and the default uniform matlab colors remain. Any suggestions?

    example code:

    hold on
    for i = 1:3
         tractHandle = plot3(tctData{tmpTctID}(:,1),tctData{tmpTctID}(:,2),tctData{tmpTctID}(:,3));
         colorMat = uint8([transpose(tctColor{tmpTctID})*255 ; repmat(aValue,1,size(tctColor{tmpTctID},1))]);
         set(tractHandle.(['ti',num2str(ti)]).Edge, 'ColorBinding', 'interpolated', 'ColorData', colorMat)
    end

    hold on for i = 1:3 tractHandle = plot3(tctData{tmpTctID}(:,1),tctData{tmpTctID}(:,2),tctData{tmpTctID}(:,3)); colorMat = uint8([transpose(tctColor{tmpTctID})*255 ; repmat(aValue,1,size(tctColor{tmpTctID},1))]); set(tractHandle.(['ti',num2str(ti)]).Edge, 'ColorBinding', 'interpolated', 'ColorData', colorMat) end

    • Yair Altman July 16, 2016 at 21:12 Reply

      @John – perhaps after the loop you have some code that causes the axes to repaint and this will cause Matlab to ignore the undocumented color changes. Try to call drawnow at the very end of your plotting section and only then update the handles’ colors to the transparent values.

  11. davion July 21, 2016 at 07:41 Reply

    Hello, Yair,
    I have some problem in using your code to draw a transparent line.
    Here are the codes, which are mostly exactly as yours.

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

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

    I wrote it in a script file, but after running it, the output figure is not transparent. I check the properties in h2b.Edge, noticing that the ‘ColorBinding’ is still ‘object’. Then I run the last sentence again in the Command Window, which is shown below.

    set(h2b.Edge, 'ColorBinding','interpolated', 'ColorData',cd , 'ColorType', 'truecoloralpha');

    set(h2b.Edge, 'ColorBinding','interpolated', 'ColorData',cd , 'ColorType', 'truecoloralpha');

    And the figure turns to transparent. It is really confusing to me. Could you please tell me how to solve this problem?
    Thanks!
    PS, the version of my MATLAB is R2015a.

  12. Jerry July 21, 2016 at 08:37 Reply

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

    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.

    • Yair Altman July 21, 2016 at 10:42 Reply

      @Jerry – adding drawnow; pause(0.1) after the plot command and before setting the color should solve the problem.

    • Jerry July 22, 2016 at 04:50 Reply

      Great, it works. Thanks!
      I guess the reason is that when it runs the setting process, the figure is still unfinished?

      • Yair Altman July 22, 2016 at 09:29

        yes

  13. vince August 21, 2016 at 10:35 Reply

    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?

    h = plot(x,y,'.g') h.Color(4) = 0.5?

    • Yair Altman September 6, 2016 at 19:37 Reply

      @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

      hLine = plot(x,y,'.g'); hLine.MarkerHandle.FaceColorType = 'truecoloralpha'; % enable transparency hLine.MarkerHandle.EdgeColorData(4) = 128; % 255 = opaque (default), 0 = fully trasparent drawnow

  14. Colored longitudinal river profiles « TopoToolbox October 25, 2016 at 10:53 Reply

    […] 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 […]

  15. Tom December 31, 2016 at 06:32 Reply

    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.

  16. Vlad Atanasiu July 23, 2017 at 07:32 Reply

    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!

    • Yair Altman July 23, 2017 at 08:20 Reply

      @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.

      • Vlad Atanasiu July 25, 2017 at 00:12

        Correct, Yair. But you will have problems where lines meet: their extremities will look disjoined.

      • Yair Altman July 25, 2017 at 04:20

        I don’t think the lines will look disjointed if you ensure that the endpoints are exactly the same.

  17. M October 24, 2017 at 08:28 Reply

    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!

    • Yair Altman October 24, 2017 at 13:51 Reply

      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.

  18. Sina M. April 30, 2018 at 15:32 Reply

    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

    [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!

    • Yair Altman May 8, 2018 at 14:06 Reply

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

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

  19. Yoni May 7, 2018 at 23:51 Reply

    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!

  20. Ondrej July 4, 2018 at 13:09 Reply

    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.

  21. albert January 10, 2019 at 21:22 Reply

    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

  22. matt April 13, 2021 at 03:20 Reply

    I very much wanted this to work with Quiver, and it does as long as the number of points is not too big. Using 2018a, I get the warnings/errors above even if placed in a single call to set.

  23. Ben Abbott March 31, 2022 at 00:34 Reply

    The graded color is not working for me using R2021a. The plot “HG2 plot line color, transparency gradient” looks exactly like “Transparent HG2 plot lines”.

    Any chance there is a fix?

    • Yair Altman March 31, 2022 at 00:48 Reply

      @Ben – it looks perfectly ok (with color gradient and all) on my R2022a… Perhaps you missed some of the steps (e.g. setting the ColorBinding to 'interpolated') or perhaps you do not have a drawnow/pause before you modify the displayed line (i.e. you try to modify the line before Matlab has completed its on-screen rendering).

  24. Ben Abbott March 31, 2022 at 04:00 Reply

    Thanks Yair, it was the second. I didn’t not include the drawnow ()

  25. Cesim Dumlu May 20, 2022 at 15:06 Reply

    Hello. I am trying to do a gradient plot for multiple functions to be displayed on the same axes and each one is colorcoded by respective colordata, using the same scaling. The sample code is below:

    x = linspace(0,4*pi,150);
    y1 = sin(x);
    y2 = 1/10*sin(x);
    p1 = plot(x,y1);
    p2 = plot(x,y2);
     
    cd1 = colormap('parula');  %color code for y1
    cd1 = interp1(linspace(min(y1),max(y1),length(cd1)),cd1,y1); 
    cd1 = uint8(cd1'*255); % need a 4xN uint8 array
    cd1(4,:) = 255; % last column is transparency
     
    cd2 = colormap('parula');  %colorcode y2
    cd2 = interp1(linspace(min(y1),max(y1),length(cd2)),cd2,y2); %uses the same scaling as cd1  
    cd2 = uint8(cd2'*255); % need a 4xN uint8 array
    cd2(4,:) = 255; % last column is transparency
     
    P={p1.Edge;p2.Edge}; %not sure how this works
    NameArray={'ColorBinding','ColorData'};
    ValueArray={'interpolated','cd1';'interpolated','cd2'};
    drawnow
    set(P,NameArray,ValueArray)

    x = linspace(0,4*pi,150); y1 = sin(x); y2 = 1/10*sin(x); p1 = plot(x,y1); p2 = plot(x,y2); cd1 = colormap('parula'); %color code for y1 cd1 = interp1(linspace(min(y1),max(y1),length(cd1)),cd1,y1); cd1 = uint8(cd1'*255); % need a 4xN uint8 array cd1(4,:) = 255; % last column is transparency cd2 = colormap('parula'); %colorcode y2 cd2 = interp1(linspace(min(y1),max(y1),length(cd2)),cd2,y2); %uses the same scaling as cd1 cd2 = uint8(cd2'*255); % need a 4xN uint8 array cd2(4,:) = 255; % last column is transparency P={p1.Edge;p2.Edge}; %not sure how this works NameArray={'ColorBinding','ColorData'}; ValueArray={'interpolated','cd1';'interpolated','cd2'}; drawnow set(P,NameArray,ValueArray)

    I am getting the error messages.

    Invalid or deleted object.
    Error in trialv4 (line 20)
    P={p1.Edge;p2.Edge};

    Invalid or deleted object. Error in trialv4 (line 20) P={p1.Edge;p2.Edge};

    Sorry for the lengthy post. Thanks!

    • Yair Altman May 26, 2022 at 22:36 Reply

      When you use plot() without hold(‘on’), each new plot() clears the axes and draws a new line, so your second plot() of p2 caused the first plot() line (p1) to be erased.
      Add a call to hold(‘on’) before you plot to ensure that both lines are ploted and not deleted.

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitable (6) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top