plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

Long labels are getting cut off when using write_image in plotly when creating polar plots.

Open saurabhpre opened this issue 2 years ago • 2 comments

I created spider plots using plotly. The labels are shown properly when visualizing the image but during write_image the labels are cut off.


import plotly.express as px
import pandas as pd
import plotly.graph_objects as go

if 1:
    radii=[0.5, 0.8, 0.3 , 0.7, 0.9]
    labels=['left -A long label 1', 'left -A long label 2','left -A long label 3','left -A long label 4', 'left -A long label 5'] 
    percentiles=[0.25,0.5,0.75]

    colors = ['rgba(255, 0, 0, 0.8)', 'rgba(0, 255, 0, 0.8)', 'rgba(0, 0,255,  0.8)']

    #create the background circles
    barpolar_plots = [go.Barpolar(r=[percentiles[t]], width=360, marker_color=[colors[t]], opacity=0.3,showlegend=False) for t in range(0, len(colors))]
    layout = go.Figure()
    layout.add_traces(barpolar_plots)
    layout.update_polars(radialaxis_autorange=False, radialaxis_range=[0,1])
    #this removes anglular and radial ticks  on the plot
    layout.update_layout(polar = dict(radialaxis = dict(showgrid = False, showticklabels=True), angularaxis = dict(showticklabels = False, showgrid=False)))
    layout.update_layout(polar=dict(radialaxis=dict(tick0=0,dtick=0.25)))
    #bg layout ends

    df = pd.DataFrame(dict(r=radii, theta=labels))

    #add line plot on top of spider plot and replace degree angles from numbers ot labels
    fig=px.line_polar(df, r='r', theta='theta', line_close=True)
    fig.update_traces(subplot="polar2")

    #this line is suppose to remve ticklabels but it does not
    fig.update_layout(polar = dict(radialaxis = dict(showticklabels = False)))

    layout = layout.add_traces(fig.data).update_layout({ax:{"domain":{"x":[0,1]}} for ax in ["polar","polar2"]})
    layout.update_layout(polar2={"bgcolor":"rgba(0,0,0,0)"})
    layout.update_layout(polar2 = dict(radialaxis = dict(showticklabels  = False)))
    layout.update_layout(font=dict(family="Arial Black",size=20, color="black"))
    layout.write_image( 'test_spider.png', scale=2.5,width=800, height=800)

saurabhpre avatar Aug 01 '23 22:08 saurabhpre

same problem here did you manage to solve ?

WBP20 avatar Apr 19 '24 21:04 WBP20

I do not remember but try adjusting the margin and see if it helps:

    val=150
    layout.update_layout( margin=dict(l=val, r=val, t=val, b=val))
    layout.write_image(os.path.join(os.path.dirname(__file__), 'images',   prefix+'_spider.png'), scale=2.5,width=800, height=800)

saurabhpre avatar Apr 20 '24 00:04 saurabhpre

Hi - we are tidying up stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for a while, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. If you'd like to submit a PR, we'd be happy to prioritize a review, and if it's a request for tech support, please post in our community forum. Thank you - @gvwilson

gvwilson avatar Jul 11 '24 23:07 gvwilson