Search code examples
pythonpython-3.xplotlyplotly-python

adjust color scale location in plotly


I wanted to show the connections between two columns of my dataframe which have duplicated connections, using a parallel categories diagram. The thing is, I can't figure how to adjust the location of the colorbar so it won't interrupt the rest of the labels. I'm adding the code for more context:

import plotly.express as px
from Models.Linear import Linear
model = Linear('YAP1')
index = model.get_index()
m, n = model.get_full_A().shape

df = pd.DataFrame(index=np.arange(m), columns=['Expression', 'Methylation'])
df['Data Set'] = index.get_level_values(0).values.flatten()
df['Expression'] = index.get_level_values(1).values.flatten()
df['Methylation'] = index.get_level_values(2).values.flatten()
df['size'] = how_much_to_satisfy()

fig = px.parallel_categories(df, dimensions=['Expression', 'Methylation'],
                             color="size", color_continuous_scale=px.colors.sequential.Inferno,
                             width=100, )

fig.update_layout(autosize=False, width=1200, height=1500,
                  margin=dict(l=500, r=300, b=65, t=90, pad=4), font=dict(size=15))

enter image description here

Thanks!


Solution

  • You can move the position of the colorbar via its x attribute.

    fig.layout['coloraxis']['colorbar']['x'] = 1.15
    

    I don't have access to your dataset but here's an example from Plotly which shows the same problem, i.e. a colorbar overlapping with labels.

    import plotly.express as px
    
    df = px.data.tips()
    df['day'].replace({'Sun': 'Sunday יום ראשון', 'Sat': 'Saturday יום שבת', 'Thur': 'Thursday יום חמישי', 
                                   'Fri': 'Friday יום שישי'}, inplace=True)
    fig = px.parallel_categories(df, dimensions=['sex', 'smoker', 'day'],
                    color="size", color_continuous_scale=px.colors.sequential.Inferno,
                    labels={'sex':'Payer sex', 'smoker':'Smokers at the table', 'day':'Day of week'},
                                )
    fig.show()
    

    enter image description here

    fig.layout['coloraxis']['colorbar']['x'] = 1.15
    fig.show()
    

    enter image description here