首页 > 解决方案 > 在情节中调整色标位置

问题描述

我想使用并行类别图显示具有重复连接的数据框的两列之间的连接。问题是,我不知道如何调整颜色条的位置,这样它就不会打断其余的标签。我正在添加更多上下文的代码:

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

在此处输入图像描述

谢谢!

标签: pythonpython-3.xplotlyplotly-python

解决方案


x您可以通过其属性移动颜色条的位置。

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

我无权访问您的数据集,但这是 Plotly 的一个示例,它显示了相同的问题,即颜色条与标签重叠。

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

在此处输入图像描述

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

在此处输入图像描述


推荐阅读