user979899
user979899

Reputation: 153

How to get matplolib to output SVGs with fill-opacity instead of opacity

I had a problem with getting transparency to work when exporting pdfs with jasper, using SVGs: Jasper export pdf with transparent SVG

The question is how to get matplotlib to export SVGs with fill-opacity instead of simply opacity.

Upvotes: 1

Views: 98

Answers (1)

user979899
user979899

Reputation: 153

I found the source of matplotlib relating to SVGs here: matplotlib svg backend

I found this:

            if (rgbFace is not None and len(rgbFace) == 4 and rgbFace[3] != 1.0
                and not forced_alpha):
            attrib['fill-opacity'] = short_float_fmt(rgbFace[3])

And this:

                if (len(rgbFace) == 4 and rgbFace[3] != 1.0
                    and not forced_alpha):
                attrib['fill-opacity'] = short_float_fmt(rgbFace[3])

This looked promising, and I started testing how to get that code to execute. It turned out you have to turn off transparency (alpha)! Additionally, you have to use RGBA instead of RGB hex for colour. In my case, I wanted semi-transparent boxes around legends and I initially had:

bbox_args = dict(boxstyle="square", fc="W", alpha=0.5)

To get it to work, I changed it to:

bbox_args = dict(boxstyle="square", fc="#FFFFFF80")

After that, the exported svg has fill-opacity instead of opacity. Note though, that both opacity and fill-opacity may work for your case. In Chrome, both worked, but only fill-opacity worked in the pdf I exported from jasperreports.

Upvotes: 0

Related Questions