Skip to content

SVG files saved with vector-effect:non-scaling-stroke XML option #1539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
eshort0401 opened this issue Apr 30, 2019 · 7 comments
Closed

SVG files saved with vector-effect:non-scaling-stroke XML option #1539

eshort0401 opened this issue Apr 30, 2019 · 7 comments

Comments

@eshort0401
Copy link

When SVG figure files are saved using plotly.offline, the SVG image's XML code uses the style option vector-effect:non-scaling-stroke for lines associated with scatter traces, but not for graph axes or other lines. When importing these SVG files into vector graphic software packages like Inkscape, this option results in trace lines remaining the same width regardless of the level of zoom, making it difficult to ensure lines are of the desired width. Below is a command line MWE, and some images showing how the generated SVG behaves in Inkscape.

import numpy as np
import plotly
from plotly.offline import iplot
plotly.offline.init_notebook_mode()

trace = {
                'x' : np.arange(0,10),
                'y' : np.arange(0,10),
                'type' : 'scatter',
                'mode' : 'lines+markers',
                'marker' : {'size': 4,  'symbol' : 0},
                'name' : 'Minimal Working Example',
                'line' : {'width' : 1.5,},
         }

fig = {'data': [trace]}

plotly.offline.plot(
        fig,  filename = 'mwe.html', image='svg', output_type='file',
        image_filename = 'mwe', auto_open = False
    )

Screen Shot 2019-04-30 at 1 19 43 pm
Screen Shot 2019-04-30 at 1 20 37 pm

In Inkscape, the vector-effect:non-scaling-stroke option also prevents you from editing the widths of lines. One solution is to manually change the XML code to vector-effect:none to stop this behaviour, although this is extremely tedious for figures with many traces.

I suspect the overwhelming majority of users would prefer vector-effect:none for their SVG images. If not, perhaps an option could be created so that the user could choose. I've had a brief look at the plotly source and suspect this needs to be implemented in Orca. If this sounds like a good idea, I would love to attempt the change myself, but will probably need some guidance as to where to look in the Orca code.

@eshort0401 eshort0401 changed the title SVG files saved with vector-effect:non-scaling-stroke option SVG files saved with vector-effect:non-scaling-stroke XML option Apr 30, 2019
@jonmmease
Copy link
Contributor

Thanks for the report and for sharing your experience here @eshort0401.

@plotly/plotly_js Do you all have any thoughts on whether the current difference in behavior between axis lines and scatter lines in SVG output is intentional? Would removing vector-effect:non-scaling-stroke result in any change in appearance for figures when displayed live?

@etpinard
Copy link
Contributor

Would removing vector-effect:non-scaling-stroke result in any change in appearance for figures when displayed live?

It wouldn't result in any visual changes on first draw, but it would on scroll zoom (see plotly/plotly.js#1899).

It sounds to me like we could remove that vector-effect:non-scaling-stroke on static exports.

@eshort0401
Copy link
Author

It wouldn't result in any visual changes on first draw, but it would on scroll zoom (see plotly/plotly.js#1899).

It sounds to me like we could remove that vector-effect:non-scaling-stroke on static exports.

That would be my preference as a user. I think the non-scaling zoom behaviour makes sense for live figures, but not for static ones.

@jonmmease
Copy link
Contributor

Thanks @etpinard, would you like this as a new plotly.js issue or as something to tack on to plotly/plotly.js#1899?

@gatoniel
Copy link

gatoniel commented Jun 13, 2022

Hi, I just stumbled upon this issue. Therefore, I wanted to ask if there is any progress on this, or a better workaround than rewriting manually the generated svg file. I am using this workaround:

import io


def plotly_to_svg(file, fig, **kwargs):
    buffer = io.BytesIO()
    fig.write_image(buffer, format="svg", **kwargs)
    with open(file, "bw") as f:
        f.write(
            buffer.getvalue().replace(
                b"vector-effect: non-scaling-stroke", b"vector-effect: none"
            )
        )


plotly_to_svg("test.svg", test_fig)

@alexcjohnson
Copy link
Collaborator

No progress as yet. If anyone is motivated to make a PR to plotly.js we would happily help get it to completion. The suggestion above from @etpinard is definitely the way to go:

It sounds to me like we could remove that vector-effect:non-scaling-stroke on static exports.

Ideally not as a post-processing step, but avoid adding that attribute in the first place when we see gd._context.staticPlot

@gvwilson
Copy link
Contributor

Hi - we are currently trying to tidy up Plotly's public repositories to help us focus our efforts on things that will help users most. Since this issue has been sitting for several years, I'm going to close it. If it's still a concern, we'd be grateful if you could open a new issue (with a short reproducible example if appropriate) so that we can add it to our backlog. Thanks for your help - @gvwilson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants