pandas Matplotlib:无法将字符串转换为浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38586488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:40:23 来源:igfitidea点击:
Matplotlib : Could not convert string to float
提问by Arij SEDIRI
I am trying to plot informations from this DataFrame :
我试图从这个 DataFrame 中绘制信息:
sold not_sold success_rate
category PriceBucket PriceBucketTitle
Papeterie 0 [0, 2] 42401 471886 17.130
1 (2, 3] 28627 360907 17.240
2 (3, 3.5] 46198 434063 18.370
3 (3.5, 4] 80307 564594 17.870
4 (4, 5] 28653 171226 16.860
5 (5, 6] 50301 415379 17.385
6 (6, 8] 45370 446013 17.730
7 (8, 10] 39859 360187 18.005
8 (10, 18] 52263 381596 17.630
9 (18, 585] 36897 387145 19.730
And this is my code :
这是我的代码:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
plt.title('Success Rate By Category : ' + str(group['category'].iloc[0]))
ax2 = ax.twinx()
x = last_merge['PriceBucket'].as_matrix()
ax2.bar(x, last_merge['sold'].as_matrix(), color='None')
ax2.bar(x, last_merge['not_sold'].as_matrix(), color='None', edgecolor='red', hatch="/")
ax.plot(x, last_merge['success_rate'].as_matrix(), color='blue')
ax2.set_ylabel("Product's number", color='red')
ax.set_ylabel(ylabel='Success Rate', color='blue')
ax.set_xlabel('Same X-values')
plt.show()
Now my object is to get 'PriceBucketTitle' on x, instead of 'PriceBucket'. The error message :
现在我的目标是在 x 上获得“PriceBucketTitle”,而不是“PriceBucket”。错误信息:
ValueError: could not convert string to float: [0, 2]
Help ? Thnx
帮助 ?谢谢
采纳答案by piRSquared
I did this:
我这样做了:
fig, ax = plt.subplots()
plt.title('Success Rate By Category')
ax2 = ax.twinx()
lmnew = last_merge.reset_index().set_index('PriceBucketTitle')
lmnew.sold.plot.bar(color='None', ax=ax2)
lmnew.not_sold.plot.bar(color='None', edgecolor='red', hatch='/', ax=ax2)
lmnew.success_rate.plot(color='blue', ax=ax)
ax2.set_ylabel("Product's number", color='red')
ax.set_ylabel(ylabel='Success Rate', color='blue')
ax.set_xlabel('Same X-values')