You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pandas绘制柱状图报错TypeError: no numeric data to plot的解决求助

问题描述

尝试用Pandas绘制柱状图时出现错误:TypeError: no numeric data to plot,代码如下:

import pandas as pd

# x轴刻度10对应两组并列柱状图
y10_a =[2, 2, 1, 3, 5, 4, 3, 2, 1, 1] # 真实值(bar1),长度10
y10_b =[1, 1, 1, 2, 6, 3, 1, 1, 1, 1] # 估计值(bar2),长度10

# x轴刻度15对应两组并列柱状图
y15_a =[2, 2, 6, 3, 5, 4, 3, 2, 1, 1, 4, 3, 2, 1, 1] # 真实值(bar1),长度15
y15_b =[1, 3, 6, 1, 4, 3, 1, 1, 0, 1, 4, 3, 2, 1, 1] # 估计值(bar2),长度15

true =[] # 真实值列表的集合(列表套列表)
est =[] # 估计值列表的集合(列表套列表)

true.append(y10_a)
true.append(y15_a)

est.append(y10_b) # 原代码写错变量名iest,此处修正
est.append(y15_b)

# 报错:TypeError: no numeric data to plot
df = pd.DataFrame({ 'dataSize':["10","15"], 'true':true, 'estimated':est})
df = df[["dataSize","true","estimated"]]
df.set_index(["dataSize"],inplace=True) # 设置x轴刻度
df.plot.bar(rot=0)

需求是:每个数据尺寸(10、15)对应两个并列柱状图,分别展示真实值和估计值的统计结果(如均值、总和等)。

另外,最初尝试用Matplotlib实现也失败了,代码如下:

import numpy as np 
import matplotlib.pyplot as plt 

X = ['size 4','size 7']
yTrue = [10,20,20,40]
zTrue = [20,30,25,30]
  
yEst = [10,20,20,40,60,42,65]
zEst = [20,30,25,30,59,22,60]

gb =[]
gb.append(yTrue)
gb.append(yEst)

bb = []
bb.append(zTrue)
bb.append(zEst)

X_axis = np.arange(len(X))
for i, j in zip(gb,bb):
    
    plt.bar( i, 0.4, label = 'True')
    plt.bar( j, 0.4, label = 'Estimated')
  
plt.xticks(X_axis, X)
plt.xlabel("Groups")
plt.title("True vs Estimated")
plt.legend()
plt.show()

错误原因与解决方案

核心问题

Pandas报错是因为true和estimated列存储的是列表(非数值型标量),plot.bar()无法直接对列表类型绘图;Matplotlib的错误则是plt.bar()参数使用错误,第一个参数应为x轴位置而非原始数据列表。

所有方案都需要先将每组列表转换为统计量(如均值、总和、中位数,以下以均值为例),再绘制并列柱状图。

方法一:Pandas实现

import pandas as pd

# 原始数据
y10_a =[2, 2, 1, 3, 5, 4, 3, 2, 1, 1]
y10_b =[1, 1, 1, 2, 6, 3, 1, 1, 1, 1]
y15_a =[2, 2, 6, 3, 5, 4, 3, 2, 1, 1, 4, 3, 2, 1, 1]
y15_b =[1, 3, 6, 1, 4, 3, 1, 1, 0, 1, 4, 3, 2, 1, 1]

# 构造包含统计量的DataFrame
df = pd.DataFrame({
    'true': [pd.Series(y10_a).mean(), pd.Series(y15_a).mean()],
    'estimated': [pd.Series(y10_b).mean(), pd.Series(y15_b).mean()]
}, index=['10', '15'])

# 绘制并列柱状图
df.plot.bar(rot=0, title='True vs Estimated Values')

方法二:Matplotlib实现

import numpy as np 
import matplotlib.pyplot as plt 

# 原始数据
X = ['size 10', 'size 15']
true_lists = [
    [2, 2, 1, 3, 5, 4, 3, 2, 1, 1],
    [2, 2, 6, 3, 5, 4, 3, 2, 1, 1, 4, 3, 2, 1, 1]
]
est_lists = [
    [1, 1, 1, 2, 6, 3, 1, 1, 1, 1],
    [1, 3, 6, 1, 4, 3, 1, 1, 0, 1, 4, 3, 2, 1, 1]
]

# 计算每组数据的均值
true_means = [np.mean(lst) for lst in true_lists]
est_means = [np.mean(lst) for lst in est_lists]

X_axis = np.arange(len(X))
width = 0.35 # 柱子宽度

# 绘制并列柱状图
plt.bar(X_axis - width/2, true_means, width, label='True')
plt.bar(X_axis + width/2, est_means, width, label='Estimated')

plt.xticks(X_axis, X)
plt.xlabel("Data Size")
plt.ylabel("Mean Value")
plt.title("True vs Estimated Values")
plt.legend()
plt.show()

内容的提问来源于stack exchange,提问作者Sea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 05:37:05