在 Dash 中获取条形图而不是散点图
Getting a barplot instead of a Scatterplot in Dash
使用此代码
app.layout = html.Div([dcc.Graph(id='electricity',
figure= {'data':[
go.Scatter(
x=df['dayofweek'],
y=df['Consumption [Wh]'],
mode='markers')],
'layout':go.Layout(title='Consumption per day',
xaxis = {'title':'dayofweek'},
yaxis = {'title':'Consumption'})})])
我使用 Dash 绘制了散点图。这没关系。但是我想知道如何在这里获得条形图而不是散点图?我尝试用 go.Bar 替换 go.Scatter 但得到了一个 ValueError:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-18-5b5a80625894> in <module>
1 app.layout = html.Div([dcc.Graph(id='electricity',
2 figure= {'data':[
----> 3 go.Bar(
4 x=df['dayofweek'],
5 y=df['Consumption [Wh]'],
~\Downloads\WPy64-3920\python-3.9.2.amd64\lib\site-packages\plotly\graph_objs\_bar.py in __init__(self, arg, alignmentgroup, base, basesrc, cliponaxis, constraintext, customdata, customdatasrc, dx, dy, error_x, error_y, hoverinfo, hoverinfosrc, hoverlabel, hovertemplate, hovertemplatesrc, hovertext, hovertextsrc, ids, idssrc, insidetextanchor, insidetextfont, legendgroup, legendgrouptitle, legendrank, marker, meta, metasrc, name, offset, offsetgroup, offsetsrc, opacity, orientation, outsidetextfont, selected, selectedpoints, showlegend, stream, text, textangle, textfont, textposition, textpositionsrc, textsrc, texttemplate, texttemplatesrc, uid, uirevision, unselected, visible, width, widthsrc, x, x0, xaxis, xcalendar, xhoverformat, xperiod, xperiod0, xperiodalignment, xsrc, y, y0, yaxis, ycalendar, yhoverformat, yperiod, yperiod0, yperiodalignment, ysrc, **kwargs)
3250 # Process unknown kwargs
3251 # ----------------------
-> 3252 self._process_kwargs(**dict(arg, **kwargs))
3253
3254 # Reset skip_invalid
~\Downloads\WPy64-3920\python-3.9.2.amd64\lib\site-packages\plotly\basedatatypes.py in _process_kwargs(self, **kwargs)
4335 self[k] = v
4336 elif not self._skip_invalid:
-> 4337 raise err
4338 # No need to call _raise_on_invalid_property_error here,
4339 # because we have it set up so that the singular case of calling
ValueError: Invalid property specified for object of type plotly.graph_objs.Bar: 'mode'
错误说明问题所在
ValueError: Invalid property specified for object of type plotly.graph_objs.Bar: 'mode'
go.Scatter
有一个 mode
属性,但是
go.Bar
没有。因此,如果您想要条形图,请删除 属性。
使用此代码
app.layout = html.Div([dcc.Graph(id='electricity',
figure= {'data':[
go.Scatter(
x=df['dayofweek'],
y=df['Consumption [Wh]'],
mode='markers')],
'layout':go.Layout(title='Consumption per day',
xaxis = {'title':'dayofweek'},
yaxis = {'title':'Consumption'})})])
我使用 Dash 绘制了散点图。这没关系。但是我想知道如何在这里获得条形图而不是散点图?我尝试用 go.Bar 替换 go.Scatter 但得到了一个 ValueError:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-18-5b5a80625894> in <module>
1 app.layout = html.Div([dcc.Graph(id='electricity',
2 figure= {'data':[
----> 3 go.Bar(
4 x=df['dayofweek'],
5 y=df['Consumption [Wh]'],
~\Downloads\WPy64-3920\python-3.9.2.amd64\lib\site-packages\plotly\graph_objs\_bar.py in __init__(self, arg, alignmentgroup, base, basesrc, cliponaxis, constraintext, customdata, customdatasrc, dx, dy, error_x, error_y, hoverinfo, hoverinfosrc, hoverlabel, hovertemplate, hovertemplatesrc, hovertext, hovertextsrc, ids, idssrc, insidetextanchor, insidetextfont, legendgroup, legendgrouptitle, legendrank, marker, meta, metasrc, name, offset, offsetgroup, offsetsrc, opacity, orientation, outsidetextfont, selected, selectedpoints, showlegend, stream, text, textangle, textfont, textposition, textpositionsrc, textsrc, texttemplate, texttemplatesrc, uid, uirevision, unselected, visible, width, widthsrc, x, x0, xaxis, xcalendar, xhoverformat, xperiod, xperiod0, xperiodalignment, xsrc, y, y0, yaxis, ycalendar, yhoverformat, yperiod, yperiod0, yperiodalignment, ysrc, **kwargs)
3250 # Process unknown kwargs
3251 # ----------------------
-> 3252 self._process_kwargs(**dict(arg, **kwargs))
3253
3254 # Reset skip_invalid
~\Downloads\WPy64-3920\python-3.9.2.amd64\lib\site-packages\plotly\basedatatypes.py in _process_kwargs(self, **kwargs)
4335 self[k] = v
4336 elif not self._skip_invalid:
-> 4337 raise err
4338 # No need to call _raise_on_invalid_property_error here,
4339 # because we have it set up so that the singular case of calling
ValueError: Invalid property specified for object of type plotly.graph_objs.Bar: 'mode'
错误说明问题所在
ValueError: Invalid property specified for object of type plotly.graph_objs.Bar: 'mode'
go.Scatter
有一个 mode
属性,但是
go.Bar
没有。因此,如果您想要条形图,请删除 属性。