【问题标题】:Matplotlib : Could not convert string to floatMatplotlib:无法将字符串转换为浮点数
【发布时间】:2016-11-29 21:38:17
【问题描述】:

我正在尝试从此 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

这是我的代码:

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()

现在我的目标是在 x 上获取“PriceBucketTitle”,而不是“PriceBucket”。错误信息:

ValueError: could not convert string to float: [0, 2]

帮助?谢谢

【问题讨论】:

  • 嗯,我认为错误是不言自明的:它试图找到一个浮点数,或者浮点格式下的字符串,但只找到[0, 2],它不能转换为浮点数跨度>

标签: python python-2.7 pandas matplotlib data-visualization


【解决方案1】:

我这样做了:

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')

【讨论】:

  • 太棒了!谢谢!!你摇滚!
猜你喜欢
  • 2018-02-28
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多