Can I reduce the space between a sunburst chart and its color scale in Dash?

855 Views Asked by At

This is my code:

fig = px.sunburst(df2, path=['Year', 'Quarter', 'Month'], values='V',
                        color='R',
                        color_continuous_scale='RdYlGn', color_continuous_midpoint=0
                        )

I want to reduce the space between the chart and its scale:

Sunburst Chart

Thanks in advance!

2

There are 2 best solutions below

0
On

You can set the x-position of a plotly color bar with

fig.update_layout(coloraxis_colorbar_x=0.7)
0
On

I cannot find anything in the documentation about the padding between the sunburst chart and the colorbar except for setting a tight layout, but this doesn't reduce the padding by very much if at all. In general, the Plotly library gives you great interactive visualizations at the cost of tunability - especially plotly.express instead of plotly.graph_objects.

A good starting point might be to poke around in fig.layout.template as this shows you exactly what charts are used to create the sunburst chart and which of these parameters you might be able to modify (especially to change a parameter outside of the scope of the documentation)

A pretty hacky solution I thought of that might work, but might also change your aesthetic, would be increasing the thickness of the colorbar to decrease the space between the sunburst chart and colorbar itself. Here is an example:

import plotly.express as px
import numpy as np
df = px.data.gapminder().query("year == 2007")
fig = px.sunburst(df, path=['continent', 'country'], values='pop',
                  color='lifeExp', hover_data=['iso_alpha'],
                  color_continuous_scale='RdBu',
                  color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop']))

## use a tight layout ##
fig.update_layout(margin = dict(t=0, l=0, r=0, b=0))

## increase the thickness of the colorbar in pixels
fig.layout.coloraxis.colorbar['thickness'] = 200
fig.show()

enter image description here