为什么我生成的条形图是一片空白呢
是在aistudio里做的就可以忽略读取文件错误的而影响
y轴刻度也明显不对
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
student_scores=pd.read_excel('PyDm_exer_data.xlsx','1.5 _学生成绩')
plt.rcParams['font.sans-serif'] = ['FZsongYi-Z13S']
plt.rcParams['axes.unicode_minus']=False
plt.figure(figsize=(12,9),dpi=80)
bar_width=0.2
y1=student_scores['数学']
y2=student_scores['统计学']
index_math=np.arange(len(student_scores.编号))
index_statistics=index_math+bar_width
bar1=plt.bar=(index_math, y1)
bar2=plt.bar=(index_statistics,y2)
plt.xticks(index_math+bar_width/2,student_scores.编号)
plt.xlabel('编号')
plt.ylabel('成绩')
plt.title('学生成绩情况统计')
plt.legend()
plt.show()
这是我的数据文件截图
看看你的y1 y2读取到了么?列名不要中文看看
在你的代码中,有两个问题可能导致生成的条形图为空白:
1、第15行 bar1=plt.bar=(index_math, y1) 和第16行 bar2=plt.bar=(index_statistics,y2) 中的 = 号多余,导致 plt.bar 函数没有被正确调用,应该修改为 bar1=plt.bar(index_math, y1) 和 bar2=plt.bar(index_statistics,y2)。
2、第19行中,使用了未定义的 plt.legend(),你需要添加对应的参数才能显示图例,例如 plt.legend(['数学', '统计学'])。
修改后的代码如下:
```python
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
student_scores=pd.read_excel('PyDm_exer_data.xlsx','1.5 _学生成绩')
plt.rcParams['font.sans-serif'] = ['FZsongYi-Z13S']
plt.rcParams['axes.unicode_minus']=False
plt.figure(figsize=(12,9),dpi=80)
bar_width=0.2
y1=student_scores['数学']
y2=student_scores['统计学']
index_math=np.arange(len(student_scores.编号))
index_statistics=index_math+bar_width
bar1=plt.bar(index_math, y1, width=bar_width)
bar2=plt.bar(index_statistics, y2, width=bar_width)
plt.xticks(index_math+bar_width/2,student_scores.编号)
plt.xlabel('编号')
plt.ylabel('成绩')
plt.title('学生成绩情况统计')
plt.legend(['数学', '统计学'])
plt.show()
```
不知道你这个问题是否已经解决, 如果还没有解决的话: