Even after passing 'title':None
inside layout.legend
in the template, the chart still shows a legend title, whereas it should change the default setting to no legend title.
If I manually pass it though with fig.update_layout()
, it then removes the title.
Why is this happening and how do I change the default setting to no legend title?
Here's the code to recreate the graph (The manual passing in update.layout()
is commented out)-
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()
ValueError: Invalid properties specified for object of type plotly.graph_objs.layout.Legend: ('itemwidth', 'title')
for line 102. It's this line:'zerolinewidth': 2}}
. Could you correct that so we can help you with the legend question? – Jellicoe4.14.3
– Stepdame