plotly title_side position not working properly

64 Views Asked by At

I'm greenhand with Python plots but I was trying to do a PCA for different human populations; while I'm still working on the actual data, the main issue I'm having with visualizations in Python, as opposed to R, is that doesn't make things very easy...

Specifically, I spent some time figuring out how to set the layout of my plot but I can't get the legend title to show in the middle of the legend itself.

Supposedly, there is an option in title_side which should do so; however, instead, it corrupts the plot legend. If anyone has any experience with this, any help would be much appreciated. Below the code for plotting and the PCA plot.

fig = px.scatter(pca, x='PC1', y='PC2', color='#LOC', template="seaborn")

fig.update_layout(legend=dict(
    title="<i> metapopulation <i>",
    title_side="top",
    orientation="h",
    entrywidthmode='fraction',
    entrywidth=.2,
    x=.5),
    autosize=False,
    height=800,
    width=800
)

fig.update_xaxes(title_text = f"PC1 ( {pve.at[pve.index[0], 0]} %)",
                 range=(-0.05, 0.2),
                 constrain='domain')
fig.update_yaxes(title_text = f"PC2 ( {pve.at[pve.index[1], 0]} %)",
                 scaleanchor="x",
                 scaleratio=1)

fig.show()

EDIT for @Naren Murali This is what I mean by corrupted legend and figure too, I just realized the use of a dict somehow shifts the whole scatter plot... enter image description here

Original Image enter image description here

1

There are 1 best solutions below

3
Naren Murali On

Could you try changing title to a dict containing two properties text and side. Since we are using legend as a dict then we cannot use legend_title_side since its not a part of the root dict of fig.update_layout

As per the docs:

side

Code: fig.update_layout(legend_title_side=<VALUE>)

Type: enumerated , one of ( "top" | "left" | "top left" | "top center" | "top right" )

Determines the location of legend's title with respect to the legend items. Defaulted to "top" with orientation is "h". Defaulted to "left" with orientation is "v". The "top left" options could be used to expand top center and top right are for horizontal alignment legend area in both x and y sides.


text

Code: fig.update_layout(legend_title_text=)

Type: string

Default: "" Sets the title of the legend.

fig.update_layout(legend=dict(
    title=dict(
        text="<i> metapopulation </i>",
        side="top center"
    ),
    orientation="h",
    entrywidthmode='fraction',
    entrywidth=.2,
    x=.5),
    autosize=False,
    height=800,
    width=800
)