首页 > 解决方案 > 我可以在 Dash 中缩小旭日形图与其色标之间的间距吗?

问题描述

这是我的代码:

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

我想减少图表及其比例之间的空间:

旭日图

提前致谢!

标签: pythonplotlyplotly-dashplotly-express

解决方案


除了设置紧凑的布局外,我在文档中找不到关于旭日形图和颜色条之间填充的任何内容,但这并没有减少填充量。一般来说,Plotly 库以可调性为代价为您提供了出色的交互式可视化效果——尤其是plotly.express代替plotly.graph_objects.

一个很好的起点可能是四处寻找,fig.layout.template因为这可以准确地向您展示用于创建旭日形图的图表以及您可以修改哪些参数(尤其是更改文档范围之外的参数)

我想到的一个非常老套的解决方案可能会奏效,但也可能会改变你的审美,那就是增加颜色条的厚度以减少旭日形图和颜色条本身之间的空间。这是一个例子:

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()

在此处输入图像描述


推荐阅读