Transparent lines

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Transparent lines

Felix Salazar
Hello community,

I was wondering if there would be any way to plot lines with tranasèarency, or to set an alpha channel in the color spec for a plot. Or at least plans to implement it in the future.

a MWE

figure()
t=linspace(0,2*pi,10000);
hold on
p1=plot(t,sin(t)+0.25*randn(size(t)));
p2=plot(t,sin(t+pi/2)+0.25*randn(size(t)));
p3=plot(t,sin(t-pi/2)+0.25*randn(size(t)));
p4=plot(t,sin(t+pi)+0.25*randn(size(t)));
hold off

I would like to have the plot lines with some sort of translucency. In Matlab, I can then put
p1.Color(4)=0.05
to set the alpha channel to 5%

Octave result
not-transparent.jpg
Matlab result (with 5% transparency for p1...p4)
transparent.jpg
I just want to plot high frequency signals, and present the noise as a shadow of the filtered signal.

Thanks


-------------------------
Félix Salazar
[hidden email]

-------------------------


Reply | Threaded
Open this post in threaded view
|

Re: Transparent lines

mmuetzel
Am 15. März 2021 um 15:30 Uhr schrieb "Felix Salazar":

> Hello community,

> I was wondering if there would be any way to plot lines with tranasèarency, or to set an alpha channel in the color spec for a plot. Or at least plans to implement it in the future.

> a MWE

> figure()
> t=linspace(0,2*pi,10000);
> hold on
> p1=plot(t,sin(t)+0.25*randn(size(t)));
> p2=plot(t,sin(t+pi/2)+0.25*randn(size(t)));
> p3=plot(t,sin(t-pi/2)+0.25*randn(size(t)));
> p4=plot(t,sin(t+pi)+0.25*randn(size(t)));
> hold off

> I would like to have the plot lines with some sort of translucency. In Matlab, I can then put
> p1.Color(4)=0.05
> to set the alpha channel to 5%
>
> I just want to plot high frequency signals, and present the noise as a shadow of the filtered signal.
 
I wasn't aware that it was possible to pass the alpha channel as part of the color property. It seems to be undocumented afaics:
https://de.mathworks.com/help/matlab/ref/matlab.graphics.primitive.line-properties.html

But I can see where that feature can be useful. Thanks for pointing this out.

Getting transparency right is tricky. In particular, there is currently no code in Octave that could do the necessary back-to-front sorting to render 3d scenes with transparency correctly.
Nonetheless, having transparent lines in a 2d scene that is similar to the one you showed is probably possible without complicated back-to-front sorting -- if the user could print the transparent lines from the background before printing any solid lines in the foreground.

Currently, Octave doesn't accept RGBA quadruplets as values for the "color" property of lines.  Nor does it have other means to plot lines with transparency.

Since this would be a new feature, it won't be added before the next major release.

In the meantime: If you *really* need transparent lines, you could save the figure as a .svg file and add `stroke-opacity="0.05"` to the `<polyline>`s you'd like to be transparent.

HTH,
Markus



Reply | Threaded
Open this post in threaded view
|

Re: Transparent lines

Francesco Potortì
> I was wondering if there would be any way to plot lines with
> transparency, or to set an alpha channel in the color spec for a plot

You can use the FaceAplha property for patch and surface objects.  So
instead of a line you can create a patch which has the shape of the line
you need.

--
Francesco Potortì (ricercatore)        Voice:  +39.050.621.3058
ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa         Skype:  wnlabisti
(gate 20, 1st floor, room C71)         Web:    http://fly.isti.cnr.it


Reply | Threaded
Open this post in threaded view
|

Re: Transparent lines

nrjank
Administrator
In reply to this post by mmuetzel
> I would like to have the plot lines with some sort of translucency. In Matlab, I can then put
> p1.Color(4)=0.05
> to set the alpha channel to 5%

playing with this a bit myself, i was wondering what changed in p1.  it's an odd hack. checking the properties of p1 before changing Color(4):

>> get(p1)
    AlignVertexCenters: off
            Annotation: [1×1 matlab.graphics.eventdata.Annotation]
          BeingDeleted: off
            BusyAction: 'queue'
         ButtonDownFcn: ''
              Children: [0×0 GraphicsPlaceholder]
              Clipping: on
                 Color: [0 0.4470 0.7410]
             ColorMode: 'auto'
           ContextMenu: [0×0 GraphicsPlaceholder]
             CreateFcn: ''
       DataTipTemplate: [1×1 matlab.graphics.datatip.DataTipTemplate]
             DeleteFcn: ''
           DisplayName: ''
      HandleVisibility: 'on'
               HitTest: on
         Interruptible: on
              LineJoin: 'round'
             LineStyle: '-'
         LineStyleMode: 'auto'
             LineWidth: 0.5000
                Marker: 'none'
       MarkerEdgeColor: 'auto'
       MarkerFaceColor: 'none'
         MarkerIndices: [1×10000 uint64]
            MarkerMode: 'auto'
            MarkerSize: 6
                Parent: [1×1 Axes]
         PickableParts: 'visible'
              Selected: off
    SelectionHighlight: on
           SeriesIndex: 1
                   Tag: ''
                  Type: 'line'
              UserData: []
               Visible: on
                 XData: [1×10000 double]
             XDataMode: 'manual'
           XDataSource: ''
                 YData: [1×10000 double]
           YDataSource: ''
                 ZData: [1×0 double]
           ZDataSource: ''

>> p1.Color

ans =

         0    0.4470    0.7410


then 

>> p1.Color(4) = 0.05 

definitely changes the plot, but the object properties are unchanged:

>> p1.Color(4)=.05

p1 =

  Line with properties:

              Color: [0 0.4470 0.7410]
          LineStyle: '-'
          LineWidth: 0.5000
             Marker: 'none'
         MarkerSize: 6
    MarkerFaceColor: 'none'
              XData: [1×10000 double]
              YData: [1×10000 double]
              ZData: [1×0 double]
Show all properties

    AlignVertexCenters: off
            Annotation: [1×1 matlab.graphics.eventdata.Annotation]
          BeingDeleted: off
            BusyAction: 'queue'
         ButtonDownFcn: ''
              Children: [0×0 GraphicsPlaceholder]
              Clipping: on
                 Color: [0 0.4470 0.7410]
             ColorMode: 'manual'
           ContextMenu: [0×0 GraphicsPlaceholder]
             CreateFcn: ''
       DataTipTemplate: [1×1 matlab.graphics.datatip.DataTipTemplate]
             DeleteFcn: ''
           DisplayName: ''
      HandleVisibility: 'on'
               HitTest: on
         Interruptible: on
              LineJoin: 'round'
             LineStyle: '-'
         LineStyleMode: 'auto'
             LineWidth: 0.5000
                Marker: 'none'
       MarkerEdgeColor: 'auto'
       MarkerFaceColor: 'none'
         MarkerIndices: [1×10000 uint64]
            MarkerMode: 'auto'
            MarkerSize: 6
                Parent: [1×1 Axes]
         PickableParts: 'visible'
              Selected: off
    SelectionHighlight: on
           SeriesIndex: 1
                   Tag: ''
                  Type: 'line'
              UserData: []
               Visible: on
                 XData: [1×10000 double]
             XDataMode: 'manual'
           XDataSource: ''
                 YData: [1×10000 double]
           YDataSource: ''
                 ZData: [1×0 double]
           ZDataSource: ''

and checking on the transparency: 

>> p1.Color(4)
Index exceeds the number of array elements (3).

>> p1.Color(4) = 0.01
(changes image fine, same behavior)

I'm guessing they added some hack to catch the element 4 assignment and perform the transparency change without actually making it a 4 element color assignment. Odds are a similar 'catch the deviant assignment' hack would work if we had a way to apply transparency in our graphics toolkits.


Reply | Threaded
Open this post in threaded view
|

Re: Transparent lines

nrjank
Administrator
In reply to this post by Francesco Potortì
You can use the FaceAplha property for patch and surface objects.  So
instead of a line you can create a patch which has the shape of the line
you need.


would that eventually run into scaling issues?


Reply | Threaded
Open this post in threaded view
|

Re: Transparent lines

mmuetzel
In reply to this post by nrjank
Am 15. März 2021 um 18:17 Uhr schrieb "Nicholas Jankowski":
> > I would like to have the plot lines with some sort of translucency. In Matlab, I can then put
> > p1.Color(4)=0.05
> > to set the alpha channel to 5%
>
> playing with this a bit myself, i was wondering what changed in p1.

They seem to be storing that information in this (hidden) hg2 property of the line:
>> hl = plot([0,1]);
>> set(hl, 'Color', [0, 0.447, 0.741, 0.5])
>> hl.Edge.ColorData

ans =

  4×1 uint8 column vector

     0
   114
   189
   128

See also: https://undocumentedmatlab.com/articles/plot-line-transparency-and-color-gradient

Octave doesn't support hg2 yet. So, if we implemented this, we'd need to store it somehow differently.

Markus




Reply | Threaded
Open this post in threaded view
|

Re: Transparent lines

Francesco Potortì
In reply to this post by nrjank
>> You can use the FaceAplha property for patch and surface objects.  So
>> instead of a line you can create a patch which has the shape of the line
>> you need.
>>
>would that eventually run into scaling issues?

Who knows?  If someone tries and reports the results here, everyone will
learn from it, and it will be remain archived for future searches.

--
Francesco Potortì (ricercatore)        Voice:  +39.050.621.3058
ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa         Skype:  wnlabisti
(gate 20, 1st floor, room C71)         Web:    http://fly.isti.cnr.it