微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 构建条形图时不支持的操作数类型 – :’str’和’float’

以前,我问过get week numbers on multiple year that is ready for plotting,根据jezrael的回答,我做了这个:

sheet2['device_create_week'] = sheet2['device_create_at'].dt.strftime('%Y-%V')
sheet2.groupby(['device_create_week']).size().reset_index(na‌​me='device created count weekly')

然后,我转向绘图

import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['figure.figsize'] = (10, 6)
rcParams['figure.dpi'] = 150
fig = plt.figure()
plt.bar(pre['user_create_week'],pre['user created count weekly'], align='center', alpha=0.5)
plt.xlabel('week')
plt.ylabel('frequency')
plt.show()
fig.savefig('test.jpg')

然后我看到一个错误

TypeError: unsupported operand type(s) for -: ‘str’ and ‘float’

我应该做些什么?我有一个预期的解决方案,可以绘制每周频率的条形图.

如果您想要数据尝试,可以尝试以下数据:

136     2014-08-27 10:19:46
245     2014-09-25 09:13:22
257     2014-09-29 15:22:16
258     2014-09-29 15:22:16
480     2015-02-02 10:01:25
481     2015-02-02 10:01:25
482     2015-02-02 10:01:25
483     2015-02-02 10:01:25
484     2015-02-02 10:01:25
485     2015-02-02 10:01:25
486     2015-02-02 10:01:25
487     2015-02-02 10:01:25
488     2015-02-02 10:01:25
536     2015-02-09 08:00:29
589     2015-02-02 10:01:25
590     2015-02-02 10:01:25
591     2015-02-02 10:01:25
592     2015-02-02 10:01:25
593     2015-02-02 10:01:25
594     2015-02-02 10:01:25
595     2015-02-02 10:01:25
596     2015-02-02 10:01:25
597     2015-02-02 10:01:25
694     2015-02-27 16:02:23
1573    2015-04-20 16:44:20
1574    2015-04-20 16:44:20
1779    2015-04-27 03:06:09
2119    2015-05-07 06:32:38
2120    2015-05-07 06:32:38
2306    2015-05-13 03:29:19
89876   2017-10-04 06:20:09
89933   2017-10-06 11:04:38
89943   2017-10-06 12:12:58
89947   2017-10-06 12:50:30
89952   2017-05-22 12:47:37
89957   2017-10-06 14:37:23
89958   2017-10-06 14:38:43
89984   2017-10-06 18:41:46
90022   2017-10-07 01:19:39
90053   2017-10-07 01:48:46
90117   2017-10-03 05:36:33
90122   2017-10-07 06:52:07
90129   2017-08-21 14:59:27
90145   2017-10-07 11:04:50
90157   2017-10-07 11:51:09
90164   2017-10-07 12:08:38
90202   2017-10-08 01:01:45
90216   2017-10-08 03:12:07
90222   2017-10-08 04:41:01
90228   2017-10-08 05:27:24
90238   2017-10-08 06:22:46
90250   2017-10-08 07:10:12
90266   2017-10-08 09:01:40
90276   2017-10-08 10:15:59
90291   2017-10-08 11:47:35
90294   2017-10-08 11:50:42
90298   2017-08-29 04:21:27
90313   2017-10-08 16:01:15
90363   2016-11-26 13:00:18

解决方法:

我想你可以改用:

plt.bar(pre['user_create_week'],pre['user created count weekly'], align='center', alpha=0.5)

DataFrame.plot.bar

ax = pre.plot.bar(x='device_create_week', 
                  y='device created count weekly', 
                  align='center', 
                  alpha=0.5)

还需要保存到图片

fig = ax.get_figure()
fig.savefig('test.jpg')

graph

如果需要追加所有可能的组合年周需要reindex

import matplotlib.pyplot as plt
from matplotlib import rcParams

rcParams['figure.figsize'] = (10, 6)
rcParams['figure.dpi'] = 150
fig = plt.figure()

sheet2['device_create_at'] = pd.to_datetime(sheet2['device_create_at'])
sheet2['device_create_week'] = sheet2['device_create_at'].dt.strftime('%Y-%V')
pre = sheet2.groupby(['device_create_week']).size()

a = sorted(['{}-{:02d}'.format(x, y) for y in range(1, 54) for x in range(2014, 2017)])
print (a[:10])
['2014-01', '2014-02', '2014-03', '2014-04', '2014-05', '2014-06',
 '2014-07', '2014-08', '2014-09', '2014-10']

pre = pre.reindex(a, fill_value=0)
print (pre.head())

ax = pre.plot.bar(align='center', alpha=0.5, width=1.0)

plt.xlabel('week')
plt.ylabel('frequency')
plt.show()

fig = ax.get_figure()
fig.savefig('test.jpg')

graph

可能省略轴x中的某些值:

spacing = 10
visible = ax.xaxis.get_ticklabels()[::spacing]
for label in ax.xaxis.get_ticklabels():
    if label not in visible:
        label.set_visible(False)

plt.show()

graph3

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐