我想绘制一个小节,我正在使用它:
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 |
+-----------------+------------+
如何克服该错误?
基于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'])