Plotly:使用模板删除图例标题
Posted
技术标签:
【中文标题】Plotly:使用模板删除图例标题【英文标题】:Plotly: Remove legend title using template 【发布时间】:2021-08-09 20:39:25 【问题描述】:即使在模板中将'title':None
传递到layout.legend
中后,图表仍会显示图例标题,但它应该将默认设置更改为无图例标题。
如果我使用 fig.update_layout()
手动传递它,它会删除标题。
为什么会发生这种情况,如何将默认设置更改为无图例标题?
这是重新创建图形的代码(手动传入update.layout()
已被注释掉)-
import plotly.graph_objects as go
import plotly.io as pio
import plotly.express as px
import pandas as pd
pio.templates['my_theme'] = go.layout.Template(
'layout': 'annotationdefaults': 'arrowcolor': '#2a3f5f', 'arrowhead': 0, 'arrowwidth': 1,
'autotypenumbers': 'strict',
'coloraxis': 'colorbar': 'outlinewidth': 0, 'ticks': '',
'colorscale': 'diverging': [[0, '#8e0152'], [0.1, '#c51b7d'],
[0.2, '#de77ae'], [0.3, '#f1b6da'],
[0.4, '#fde0ef'], [0.5, '#f7f7f7'],
[0.6, '#e6f5d0'], [0.7, '#b8e186'],
[0.8, '#7fbc41'], [0.9, '#4d9221'], [1,
'#276419']],
'sequential': [[0.0, '#0d0887'],
[0.1111111111111111, '#46039f'],
[0.2222222222222222, '#7201a8'],
[0.3333333333333333, '#9c179e'],
[0.4444444444444444, '#bd3786'],
[0.5555555555555556, '#d8576b'],
[0.6666666666666666, '#ed7953'],
[0.7777777777777778, '#fb9f3a'],
[0.8888888888888888, '#fdca26'], [1.0,
'#f0f921']],
'sequentialminus': [[0.0, '#0d0887'],
[0.1111111111111111, '#46039f'],
[0.2222222222222222, '#7201a8'],
[0.3333333333333333, '#9c179e'],
[0.4444444444444444, '#bd3786'],
[0.5555555555555556, '#d8576b'],
[0.6666666666666666, '#ed7953'],
[0.7777777777777778, '#fb9f3a'],
[0.8888888888888888, '#fdca26'],
[1.0, '#f0f921']],
'colorway': ["#db2b39","#3d405b","#2fbf71","#faa613","#00a6fb"],
'font': 'color': '#2a3f5f',
'geo': 'bgcolor': 'white',
'lakecolor': 'white',
'landcolor': '#E5ECF6',
'showlakes': True,
'showland': True,
'subunitcolor': 'white',
'hoverlabel': 'align': 'left',
'hovermode': 'closest',
'legend': 'orientation': 'v',
'bordercolor': '#000000',
'borderwidth': 0.7,
'itemwidth': 30,
'x': 0.01,
'y': 1.075,
'title': None,
'bgcolor':'#F6F5F4',
'mapbox': 'style': 'light',
'paper_bgcolor': 'white',
'plot_bgcolor': 'white',
'polar': 'angularaxis': 'gridcolor': 'white', 'linecolor': 'white', 'ticks': '',
'bgcolor': '#E5ECF6',
'radialaxis': 'gridcolor': 'white', 'linecolor': 'white', 'ticks': '',
'scene': 'xaxis': 'backgroundcolor': '#E5ECF6',
'gridcolor': 'white',
'gridwidth': 2,
'linecolor': 'white',
'showbackground': True,
'ticks': '',
'zerolinecolor': 'white',
'yaxis': 'backgroundcolor': '#E5ECF6',
'gridcolor': 'white',
'gridwidth': 2,
'linecolor': 'white',
'showbackground': True,
'ticks': '',
'zerolinecolor': 'white',
'zaxis': 'backgroundcolor': '#E5ECF6',
'gridcolor': 'white',
'gridwidth': 2,
'linecolor': 'white',
'showbackground': True,
'ticks': '',
'zerolinecolor': 'white',
'separators':'.',
'shapedefaults': 'line': 'color': '#2a3f5f',
'ternary': 'aaxis': 'gridcolor': 'white', 'linecolor': 'white', 'ticks': '',
'baxis': 'gridcolor': 'white', 'linecolor': 'white', 'ticks': '',
'bgcolor': '#E5ECF6',
'caxis': 'gridcolor': 'white', 'linecolor': 'white', 'ticks': '',
'title': 'x': 0.5,
'font_size':30,
'xaxis': 'automargin': True,
'gridcolor': '#eeeeee',
'linecolor': 'white',
'ticks': '',
'title': 'standoff': 15,
'zerolinecolor': 'white',
'zerolinewidth': 2,
'yaxis': 'automargin': True,
'gridcolor': '#eeeeee',
'linecolor': 'white',
'ticks': '',
'title': 'standoff': 15,
'zerolinecolor': 'white',
'zerolinewidth': 2
)
pio.templates.default = 'my_theme'
df = pd.DataFrame('date': 27: '2020-01-28',
28: '2020-01-29',
29: '2020-01-30',
30: '2020-01-31',
31: '2020-02-01',
'new_cases': 27: 2651.0, 28: 589.0, 29: 2068.0, 30: 1692.0, 31: 2111.0,
'new_cases_smoothed': 27: 717.286,
28: 801.429,
29: 1082.857,
30: 1283.714,
31: 1515.0)
fig = px.line(df, x='date', y=['new_cases','new_cases_smoothed'],title='New cases',
color_discrete_sequence = ['#DB2B39','#0D0628'])
fig.update_traces(hovertemplate=None)
fig.update_layout(hovermode='x unified')#, legend=dict(title=None))
fig.show()
【问题讨论】:
我将您提供的代码复制并粘贴到新的 google colab 上,但它无法运行。第 102 行显示ValueError: Invalid properties specified for object of type plotly.graph_objs.layout.Legend: ('itemwidth', 'title')
。就是这一行:'zerolinewidth': 2
。您能否更正一下,以便我们帮助您解决图例问题?
@KarenPalacio 我无法重现您面临的错误。这对我来说可以。也许检查你的情节版本?我的是4.14.3
【参考方案1】:
我确信以下方法可以解决问题:
'title': 'text': None
但令我惊讶的是,文本'variable'
仍然弹出。空字符串''
不起作用,'title': 'text': False
也不起作用。
我觉得这很有趣,因为您可以编辑图例标题的所有其他属性除了标题文本本身。比如颜色,用:
'title': 'font': 'color':'blue'
这为次优解决方案开辟了道路:
'title': 'font': 'color':''rgba(0,0,0,0'
这给了你:
但这可以说看起来有点奇怪,因为您仍然有额外的文本空间。 所以这似乎是某种错误。
【讨论】:
哈哈,空白处看起来确实很奇怪。我想我只会求助于手动将它传递给每个图表,直到找到最佳解决方案。 刚刚发现模板内定义的轴标题也存在此错误。轴的所有其他属性都可以正常工作,只是标题不行。 如果我的模板定义了'xaxis': 'title': 'text': "ayy"
,然后我将fig.update_layout(xaxis=dict(title=None))
或fig.update_xaxes(title=None)
与图形调用一起传递。然后,它将xaxis
title
显示为“ayy”。发现这很有趣。似乎模板属性被图形调用覆盖,只有当您将图形调用设置为None
时,它才会显示隐藏在下面的模板属性。【参考方案2】:
我使用的是“plotly-express”,版本是“4.14.3”。
以下是我实际删除标题的方法:
from plotly import express as px
# make your plot
fig = px.scatter(...)
# udpate the legend's title by setting it to none
fig.update_layout(legend='title_text':'')
## fig.update_layout('legend_title_text': '') worked too.
# display it
fig.show()
【讨论】:
以上是关于Plotly:使用模板删除图例标题的主要内容,如果未能解决你的问题,请参考以下文章
Plotly:如何使用类似于在图例中单击它们的按钮来切换轨迹?
如何在 plotly & R Language 中关闭特定的图例类型?
在基于 HTML 模板的 Shiny App 中使用 Plotly 失败