# 划分区间 bins = [0,1,2,3,4,5,10,15,20,25,30,35,40,45,50,60,70,80,90,100,1220] data = data_forecast_is_stk_bs_rpt_zq_befor_big['本年比上一年归母倍数'] data1 = data_forecast_is_stk_bs_rpt_cw_befor_big['本年比上一年归母倍数'] # 统计每个柱子的数据 hist, edges = pd.cut(data, bins=bins, right=False, include_lowest=True, retbins=True) hist1, edges1 = pd.cut(data1, bins=bins, right=False, include_lowest=True, retbins=True) counts = hist.value_counts() counts1 = hist1.value_counts() # 按照 hist 的顺序重新排序 counts counts = counts.reindex(hist.cat.categories) counts1 = counts1.reindex(hist1.cat.categories) # 按照 bins 的顺序给每个柱子指定标签 labels = counts.index.astype(str) labels1 = counts1.index.astype(str) fig, ax = plt.subplots(figsize=(18, 6)) # 绘制柱形图 #plt.bar(labels, counts) plt.bar(labels, counts, width=0.4, align='center', label='Data 1') plt.bar(labels1+0.4, counts1, width=0.4, align='center', label='Data 2') can only concatenate str (not "float") to str
时间: 2023-12-14 10:39:57 浏览: 64
关于pandas的离散化,面元划分详解
这个错误提示是因为在下面的代码中,你使用了字符串类型的标签和浮点类型的数值进行字符串拼接,导致了这个错误。具体来说,在第 19 行中,你使用了 `labels1+0.4` 来进行标签的位置调整,但是 `labels1` 是一个字符串类型的数组,无法直接和浮点数相加。
你可以将 `labels1` 数组中的每个元素都加上 0.4,然后再进行字符串拼接,例如:
```python
labels1 = (edges[:-1] + 0.4).astype(str) + '-' + (edges[1:]).astype(str)
```
这样就可以得到一个类似于 `'0.4-1.0'` 的字符串,表示某个区间的标签。然后在绘制柱形图时,就可以使用这个新的标签数组 `labels1`。
完整代码如下:
```python
# 划分区间
bins = [0,1,2,3,4,5,10,15,20,25,30,35,40,45,50,60,70,80,90,100,1220]
data = data_forecast_is_stk_bs_rpt_zq_befor_big['本年比上一年归母倍数']
data1 = data_forecast_is_stk_bs_rpt_cw_befor_big['本年比上一年归母倍数']
# 统计每个柱子的数据
hist, edges = pd.cut(data, bins=bins, right=False, include_lowest=True, retbins=True)
hist1, edges1 = pd.cut(data1, bins=bins, right=False, include_lowest=True, retbins=True)
counts = hist.value_counts()
counts1 = hist1.value_counts()
# 按照 hist 的顺序重新排序 counts
counts = counts.reindex(hist.cat.categories)
counts1 = counts1.reindex(hist1.cat.categories)
# 按照 bins 的顺序给每个柱子指定标签
labels = counts.index.astype(str)
labels1 = (edges[:-1] + 0.4).astype(str) + '-' + (edges[1:]).astype(str)
fig, ax = plt.subplots(figsize=(18, 6))
# 绘制柱形图
plt.bar(labels, counts, width=0.4, align='center', label='Data 1')
plt.bar(labels1, counts1, width=0.4, align='center', label='Data 2')
# 添加图例和标题
plt.legend()
plt.title('Histogram of Data')
```
阅读全文