pandas - an instance of :class:`plotly.graph_objs.Bar`
问题描述
Plotting Pandas in Plotly Subplots throwing this error
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Bar(worldData_2, x='TotalCases', y='Country', orientation='h'),row=1, col=1)
fig.add_trace(go.Bar(worldData_2, x='TotalDeaths', y='Country', orientation='h'),row=1, col=2)
fig.update_yaxes(categoryorder='total ascending')
fig.update_layout(height=600, width=800, title_text="COVID19", showlegend=False)
fig.show()
Error, Whats the error
2644 The first argument to the plotly.graph_objs.Bar
2645 constructor must be a dict or
-> 2646 an instance of :class:`plotly.graph_objs.Bar`"""
2647 )
2648
ValueError: The first argument to the plotly.graph_objs.Bar
constructor must be a dict or
an instance of :class:`plotly.graph_objs.Bar`
How to pass the Pandas DataFrame
解决方案
从 go.Bar 上的 plotly 文档中,第一个位置参数是:
arg - 与此构造函数或 plotly.graph_objects.Bar 实例兼容的属性字典
这是错误消息的来源,因为 DataFrame 是意外的。
这与接受DataFrame作为参数的 px.Bar 不同:
data_frame (DataFrame or array-like or dict) – 需要传递这个参数才能使用列名(而不是关键字名)。Array-like 和 dict 在内部转换为 pandas DataFrame。可选:如果缺少,则使用其他参数在后台构建 DataFrame。
由于go.Bar
只接受x
andy
参数的坐标集,因此将and传递Series
给。x
y
就像是:
fig = make_subplots(rows=1, cols=2)
fig.add_trace(
go.Bar(x=worldData_2['TotalCases'], y=worldData_2['Country'],
orientation='h'),
row=1, col=1
)
fig.add_trace(
go.Bar(x=worldData_2['TotalDeaths'], y=worldData_2['Country'],
orientation='h'),
row=1, col=2
)
fig.update_yaxes(categoryorder='total ascending')
fig.update_layout(height=600, width=800, title_text="COVID19", showlegend=False)
fig.show()
推荐阅读
- python - Django Channels - self.channel_layer.group_send 不调用函数
- php - php列表并使用连接表选择数据
- reactjs - React Native:通过向下滑动关闭模态后屏幕冻结问题
- laravel - 如何将 Laravel 自定义请求中的错误返回给 Ajax
- macos - Microsoft 自定义翻译器:“文档 '._..._...txt' 不是有效的文本文件,因为它包含一个或多个无效字符”
- bash - 在bash中调用函数后有条件地执行步骤
- linux - thread_local 怎么做!在 rust 中使用动态库?
- unity3d - Unity pixelart多个精灵没有正确切割
- azure-devops - Azure DevOps 管道仅用于部署已更改的 arm 模板
- javascript - React 中的 Onclick 方法影响表格中的每个列表项,应该只影响被点击的那个