当我在 plotly 中绘制气泡图时出现错误

Posted

技术标签:

【中文标题】当我在 plotly 中绘制气泡图时出现错误【英文标题】:When I'm plotting bubble chart in plotly I'm getting an error 【发布时间】:2021-07-19 01:27:23 【问题描述】:

我正在绘制气泡图。我的代码如下。

fig = px.scatter(data, x="gdp_per_capita", y="extreme_poverty",
             size="total_cases", color="continent",
                 hover_name="location")
fig.show()

我收到了这个错误

    Invalid element(s) received for the 'size' property of scattergl.marker
    Invalid elements include: [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]

The 'size' property is a number and may be specified as:
  - An int or float in the interval [0, inf]
  - A tuple, list, or one-dimensional numpy array of the above

我已经用过了

error_bad_lines=False

在导入数据时。

有没有办法解决这个错误? 谢谢。

【问题讨论】:

确保要适应大小的数据是数字的。如果还有更多的 NA,请尝试将其替换为 0。 【参考方案1】:

您在数据框中有 nan 值。您需要删除这些值。

您可以在“total_cases”列中删除包含 nan 的行

data = data.dropna(subset=['total_cases'])

或者你可以用 0 填充 NA

data['total_cases'] = data.fillna(0)

【讨论】:

以上是关于当我在 plotly 中绘制气泡图时出现错误的主要内容,如果未能解决你的问题,请参考以下文章