Skip to content

Feature Request: Filled Polygon #2080

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
peterdsharpe opened this issue Jan 15, 2020 · 6 comments · Fixed by #2089
Closed

Feature Request: Filled Polygon #2080

peterdsharpe opened this issue Jan 15, 2020 · 6 comments · Fixed by #2089

Comments

@peterdsharpe
Copy link

peterdsharpe commented Jan 15, 2020

Hey all,

First off - thanks for all your hard work on this beautiful visualization library. I have a feature request that would be very handy in my scientific computing work, and I wanted to submit it to you for your consideration:

Matplotlib has a very useful command to draw a 2D filled polygon (matplotlib.pyplot.fill), exhibited here:
https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/fill.html

It would be very helpful to have an equivalent command available through Plotly. As far as I'm aware, this feature does not yet exist. The closest equivalent that I could find is an SVG Path, exhibited here:
https://plot.ly/python/shapes/#svg-paths

However, the input syntax is pretty clunky, especially if one wants to plot a filled polygon with many vertices defined from, say, a NumPy ndarray:

        go.layout.Shape(
            type="path",
            path=" M 3,7 L2,8 L2,9 L3,10, L4,10 L5,9 L5,8 L4,7 Z",
            fillcolor="PaleTurquoise",
            line_color="LightSeaGreen",
        ),

A more streamlined interface, with syntax that lends itself to scientific computing (like matplotlib.pyplot.fill), would be greatly appreciated. Is this a feature the Plotly team would consider adding?

Thank you so much!
-Peter

@empet
Copy link

empet commented Jan 16, 2020

@peterdsharpe It's not so awful to define a filled polygon with many points. You can use f-string concatenation:

n_points =50
polygon = f'M {x[0]}, {y[0]}'
for k in range(1, n_points):
    polygon += f' L{x[k]}, {y[k]}'
polygon += ' Z'

fig = go.Figure(go.Scatter())
fig.update_layout(width=600, height=600,
                  shapes=[dict(path =polygon,
                          fillcolor='RoyalBlue',
                          line_width=2,
                          line_color='RoyalBlue')])

@nicolaskruchten
Copy link
Contributor

You can also use a scatter trace with the fill="toself" option to create closed, filled shapes out of arrays. This approach gives you hover-labels which you can fill with useful information.

image

@nicolaskruchten
Copy link
Contributor

A single trace can also encode many shapes if you interrupt the series with None:

image

@nicolaskruchten
Copy link
Contributor

@Mahdis-z could you please add something like the two examples above to https://plot.ly/python/shapes/ with a little explanation about the difference between this approach and layout.shapes?

@peterdsharpe
Copy link
Author

@empet, I'm well aware that a string concatenation with SVG shapes is possible, but as previously mentioned, it's clunky and not very scalable.

@nicolaskruchten, thank you so much for the scatter suggestion - this is perfect and exactly what I was looking for! Not sure how I missed the fill="toself" option in the documentation! Thanks!

@nicolaskruchten
Copy link
Contributor

Not sure how I missed the fill="toself" option in the documentation

I think that's on us... I don't think it's really front and center!

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

Successfully merging a pull request may close this issue.

3 participants