python - plotly :使用模板删除图例标题

标签 python plotly data-visualization plotly-python

即使通过后 'title':Nonelayout.legend在模板中,图表仍显示图例标题,而应将默认设置更改为无图例标题。 passed in template
如果我手动通过 fig.update_layout() ,然后删除标题。
passed separately
为什么会发生这种情况,如何将默认设置更改为无图例标题?
这是重新创建图形的代码(手动传入 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()

最佳答案

我确信以下方法可以解决问题:

'title': {'text': None}
但令我惊讶的是,文本 'variable'仍然弹出。空字符串 ''不起作用,'title': {'text': False} 也不起作用.
我觉得这很有趣,因为您可以编辑图例标题的所有其他属性,除了标题文本本身。像颜色,例如,与:
'title': {'font': {'color':'blue'}}
enter image description here
这为次优解决方案开辟了道路:
'title': {'font': {'color':''rgba(0,0,0,0'}}
这给了你:
enter image description here
但这可以说看起来有点奇怪,因为您仍然有额外的文本空间。
所以这似乎是某种错误。

关于python - plotly :使用模板删除图例标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67622972/

相关文章:

pandas - 如何对 pandas 时间戳进行分组,在一张图中绘制多个图并将它们在 matplotlib 中堆叠在一起?

d3.js - 如何使用非树数据创建 d3.js 可折叠力布局?

带有游标的Python mysql错误

Python:将 Python 字典列表转换为 JSON 对象数组

python - 如何从本地系统加载 TF hub 模型

python - 在 Eventlet 页面抓取器中维护 session ?

python-3.x - 如何在 plotly.express 中添加痕迹

Python、破折号 : how to add a KPI Card component to a dashboard

python - Plotly:如何使用 facet 设置 plotly.express 图表的位置?

data-visualization - 具有预定义深度的 D3 树