Plotly:如何处理重叠的颜色条和图例?
问题描述
我有一个简单的图表,我正在使用 Plotly Express 库来绘制它.图片如下,其中有两个图例重叠Rank"和Genre".
px.scatter_ternary(data_frame = data, a='Length.', b='Beats.Per.Minute', c='Popularity',颜色='排名',符号='流派',标签 = {'Length.': 'Len', 'Beats.Per.Minute':'Beats'},color_continuous_midpoint = 15,symbol_sequence = ['circle-open-dot', 'cross-open','triangle-ne'])
如何避免重叠?
解决方案简答:
您可以使用以下方法移动颜色条:
fig.update_layout(coloraxis_colorbar=dict(yanchor="top", y=1, x=0,滴答声=外面"))
详情:
由于您没有提供包含数据样本的完全可执行代码片段,我将不得不基于
这似乎与您面临的问题完全相同.为了使绘图可读,我只需像这样使用 fig.update_layout(coloraxis_colorbar()
移动颜色条:
完整代码:
# 导入将 plotly.express 导入为 px# 数据df = px.data.election()# 图形设置fig = px.scatter_ternary(df, a="Joly", b="Coderre", c="Bergeron", hover_name="district",color=total",size=total",size_max=15,symbol='Coderre',color_discrete_map = {Joly":蓝色",Bergeron":绿色",Coderre":红色"},)# 移动颜色条fig.update_layout(coloraxis_colorbar=dict(yanchor="top", y=1, x=0,蜱=外面",记号后缀="账单"))图.show()
我希望这能解决您的实际问题.如果没有,请随时告诉我!
I have a simple graph and I am using Plotly Express Library to draw it. The image is as follows which have two legends overlapping 'Rank' and 'Genre'.
px.scatter_ternary(data_frame = data, a='Length.', b='Beats.Per.Minute', c='Popularity',
color = 'Rank',
symbol = 'Genre',
labels = {'Length.': 'Len', 'Beats.Per.Minute':'Beats'},
color_continuous_midpoint = 15,
symbol_sequence = ['circle-open-dot', 'cross-open','triangle-ne'])
What can be done to avoid overlapping?
解决方案Short answer:
You can move the colorbar with:
fig.update_layout(coloraxis_colorbar=dict(yanchor="top", y=1, x=0,
ticks="outside"))
The details:
Since you haven't provided a fully executable code snippet with a sample of your data, I'm going to have to base a suggestion on a dataset and an example that's at least able to reproduce a similar problem. Take a look:
This seems to be the exact same problem that you're facing. To make the plot readable, I would simply move the colorbar using fig.update_layout(coloraxis_colorbar()
like this:
Complete code:
# imports
import plotly.express as px
# data
df = px.data.election()
# figure setup
fig = px.scatter_ternary(df, a="Joly", b="Coderre", c="Bergeron", hover_name="district",
color="total", size="total", size_max=15, symbol ='Coderre',
color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"},
)
# move colorbar
fig.update_layout(coloraxis_colorbar=dict(yanchor="top", y=1, x=0,
ticks="outside",
ticksuffix=" bills"))
fig.show()
I hope this solves your real-world problem. Don't hesitate to let me know if not!
相关文章