TypeError:bar()为参数'x'获得了多个值

问题描述 投票:0回答:1

我想绘制一个小节,我正在使用它:

import plotly.express as px

fig = plt.bar(df_last_plot, y='Deaths', x='Country')
fig.update_layout(title='No.of Deaths for Top 10 Countries',
                   xaxis_title='Country',
                   yaxis_title='No.of Deaths Cases')
fig.show()

但是它引发了一个错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-158-7f5e5e905f6e> in <module>
      1 import plotly.express as px
      2 
----> 3 fig = plt.bar(df_last_plot, y='Deaths', x='Country')
      4 fig.update_layout(title='No.of Deaths for Top 10 Countries',
      5                    xaxis_title='Country',

TypeError: bar() got multiple values for argument 'x'

我的数据框看起来像:

df_last:

+-----------------+------------+
| Country         |  Deaths    |
+-----------------+------------+
|  Belgium        |  35312     |
|  France         |  167548    |
|  Germany        |  34001     |
|  Iran           |  88912     |
|  Italy          |  358872    | 
|  Mainland China |  192276    | 
|  Netherlands    |  35618     | 
|  Spain          |  253417    | 
|  UK             |  106910    | 
|  US             |  219105    |  
+-----------------+------------+

如何克服该错误?

python plotly data-visualization
1个回答
0
投票

基于the docs,该函数具有签名

matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, \*, align='center', data=None,  \*\*kwargs)

如您所见,x是第一个参数,因此将函数调用为

fig = plt.bar(df_last_plot, y='Deaths', x='Country')

您将df_last_plot传递为x,然后再次传递为'Country'。我认为您的意图接近以下内容

fig = plt.bar(df_last_plot['Country'], df_last_plot['Deaths'])
© www.soinside.com 2019 - 2024. All rights reserved.