首页 > 解决方案 > 使用 groupby 绘制 pandas 数据框 - TypeError:没有要绘制的数字数据

问题描述

我在使用 matplotlib 在 python 中绘制 groupby 数据时遇到问题,作为 python 新手,我将不胜感激。

目的:按时间段(x 轴)绘制英格兰(y 轴)按地区分组的总翻倒事件。

首先,我对我的数据进行子集化:

  ##Total Per Region
    TotalPerRegion=data[(data['LA_Name']=='Total') & (data['Region'] != 'TotalEngland')]

转换数据并按区域分组

TotalPerRegion=TotalPerRegion.groupby(['Year','Region'])['Total_Incidents'].sum().unstack(level='Region')

OutputRegion    East East Midlands  London North East North West South East  \
Year                                                                    
2012-13  56484         40059  231894      56748     105201      66710   
2013-14  63829         44747  327484      50467     113596      70029   
2014-15  68373         48946  367075      50837     108536      74484   
2015-16  69461         50694  320137      48067     117529      69315   
2016-17  75447         60969  349878      53005     128193      79911   
2017-18  72832         68470  284250      62037     111920      79307   
2018-19  67792         69237  324261      63646     112069      83752   

Region  South West Total England West Midlands Yorkshire and The Humber  
Year                                                                     
2012-13      45182        714637         46425                    57536  
2013-14      53433        857655         56880                    67391  
2014-15      56166        905604         56895                    66051  
2015-16      34390        941896         56466                    68883  
2016-17      44745       1011199         63806                    76545  
2017-18      56563        997553         59593                    86697  
2018-19      59334       1072431         65379                    97649  

然后尝试绘制数据:

fig, ax = plt.subplots()
TotalPerRegion.plot()

然后出现此错误:

    raise TypeError("no numeric data to plot")
TypeError: no numeric data to plot

任何人都可以提供任何帮助来解决问题吗?谢谢。

标签: pythonpandasdataframematplotlibpandas-groupby

解决方案


推荐阅读