划分区间 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['本年比上一年归母倍数'] # 统计每个柱子的数据 hist, edges = pd.cut(data, bins=bins, right=False, include_lowest=True, retbins=True) counts = hist.value_counts() # 按照 bins 的顺序给每个柱子指定标签 labels = [f'{bins[i]}-{bins[i+1]}' for i in range(len(bins)-1)] fig, ax = plt.subplots(figsize=(12, 6)) # 绘制柱形图 plt.bar(counts.index.astype(str), counts.values) # 添加数据标签 for i, v in enumerate(counts.values): plt.text(i, v, str(v), ha='center', va='bottom') # 设置图表标题和坐标轴标签 plt.title('Score Distribution') plt.xlabel('Range') plt.ylabel('Count') # 显示图表 plt.show() 柱形图从左到右是按数量从大到小显示的顺序,能不能按bins中的顺序从左往右显示
时间: 2023-12-14 11:36:36 浏览: 58
可以按照 bins 中的顺序从左往右显示,只需要在绘制柱形图时指定 x 轴的标签为 labels 即可。修改代码如下:
```
fig, ax = plt.subplots(figsize=(12, 6))
# 绘制柱形图,并按照 labels 的顺序指定 x 轴的标签
plt.bar(labels, counts[labels])
# 添加数据标签
for i, v in enumerate(counts[labels]):
plt.text(i, v, str(v), ha='center', va='bottom')
# 设置图表标题和坐标轴标签
plt.title('Score Distribution')
plt.xlabel('Range')
plt.ylabel('Count')
# 显示图表
plt.show()
```
相关问题
# 划分区间 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, counts1, width=0.4, align='center', label='Data 2') 2个柱子合并了能不能分开
可以分开绘制,只需要将绘制柱状图的代码分别写在两个子图中即可。可以参考如下代码:
```
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(18, 6))
# 绘制第一个子图
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()
counts = counts.reindex(hist.cat.categories)
counts1 = counts1.reindex(hist1.cat.categories)
labels = counts.index.astype(str)
labels1 = counts1.index.astype(str)
ax1.bar(labels, counts, width=0.4, align='center', label='Data 1')
ax1.set_title('Data 1')
# 绘制第二个子图
ax2.bar(labels1, counts1, width=0.4, align='center', label='Data 2')
ax2.set_title('Data 2')
# 添加图例
fig.legend(loc='upper right')
plt.show()
```
这里使用了 `subplots` 函数创建了两个子图,并将绘制柱状图的代码分别写在了两个子图中。同时,为了方便区分两个子图,我在每个子图的标题中添加了不同的数据标识。最后,通过 `fig.legend` 函数添加了图例。
# 划分区间 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['本年比上一年归母倍数'] # 统计每个柱子的数据 hist, edges = pd.cut(data, bins=bins, right=False, include_lowest=True, retbins=True) counts = hist.value_counts() # 按照 bins 的顺序给每个柱子指定标签 labels = [f'{bins[i]}-{bins[i+1]}' for i in range(len(bins)-1)] fig, ax = plt.subplots(figsize=(18, 6)) # 绘制柱形图 plt.bar(counts.index.astype(str), counts[labels]) # 添加数据标签 for i, v in enumerate(counts.values): plt.text(i, v, str(v), ha='center', va='bottom') # 设置图表标题和坐标轴标签 plt.title('Score Distribution') plt.xlabel('Range') plt.ylabel('Count') # 显示图表 plt.show() KeyError: "None of [['0-1', '1-2', '2-3', '3-4', '4-5', '5-10', '10-15', '15-20', '20-25', '25-30', '30-35', '35-40', '40-45', '45-50', '50-60', '60-70', '70-80', '80-90', '90-100', '100-1220']] are in the [index]"
这个 KeyError 的原因是因为 hist.value_counts() 方法返回的是一个以区间标签作为索引的 Series,而在 plt.bar(counts.index.astype(str), counts[labels]) 中使用了 counts[labels],这里的 labels 是一个列表,不是一个合法的索引。可以将 labels 改为 counts.index.astype(str),即可解决该问题。修改后的代码如下:
```
# 划分区间
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['本年比上一年归母倍数']
# 统计每个柱子的数据
hist, edges = pd.cut(data, bins=bins, right=False, include_lowest=True, retbins=True)
counts = hist.value_counts()
# 按照 bins 的顺序给每个柱子指定标签
labels = counts.index.astype(str)
fig, ax = plt.subplots(figsize=(18, 6))
# 绘制柱形图
plt.bar(labels, counts)
# 添加数据标签
for i, v in enumerate(counts.values):
plt.text(i, v, str(v), ha='center', va='bottom')
# 设置图表标题和坐标轴标签
plt.title('Score Distribution')
plt.xlabel('Range')
plt.ylabel('Count')
# 显示图表
plt.show()
```
阅读全文