python – 构建条形图时不支持的操作数类型 – :’str’和’float’
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 构建条形图时不支持的操作数类型 – :’str’和’float’,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3741字,纯文字阅读大概需要6分钟。
内容图文
![python – 构建条形图时不支持的操作数类型 – :’str’和’float’](/upload/InfoBanner/zyjiaocheng/754/942bccf73c864384aeef68334504f84d.jpg)
以前,我问过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)
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')
如果需要追加所有可能的组合年周需要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')
可能省略轴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()
内容总结
以上是互联网集市为您收集整理的python – 构建条形图时不支持的操作数类型 – :’str’和’float’全部内容,希望文章能够帮你解决python – 构建条形图时不支持的操作数类型 – :’str’和’float’所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。