plt.bar(x_14,b_14,width=bar_width,label="9月14日") plt.bar(x_15,b_15,width=bar_width,label="9月15日") plt.bar(x_16,b_16,width=bar_width,label="9月16日")
时间: 2024-03-31 12:33:27 浏览: 18
这段代码使用了 matplotlib 库中的 `bar()` 函数,用于绘制柱形图。其中的参数含义如下:
- `x_14`、`x_15`、`x_16`:分别表示三个日期的 X 坐标,是一个列表。
- `b_14`、`b_15`、`b_16`:分别表示三个日期的柱形高度,是一个列表。
- `width`:柱形的宽度。
- `label`:柱形图的标签,用于在图例中显示。
这段代码会生成三组柱形图,分别对应 9 月 14 日、9 月 15 日和 9 月 16 日的数据。每组柱形图的 X 坐标和柱形高度分别由 `x_14`、`b_14`、`x_15`、`b_15`、`x_16` 和 `b_16` 决定。同时,每组柱形图都有一个标签,分别为 9 月 14 日、9 月 15 日和 9 月 16 日。在绘制完成后,这三组柱形图会在同一个坐标系中显示,并且会自动生成一个图例,其中包含了每组柱形图的标签。
相关问题
y = ("Total","Female","Male","Rural","Urban") x = df["educational level"] total_width,n = 0.8,5 width = total_width/n x=x-(total_width-width)/2 print(x) plt.bar(x,df["Total"],width = width,label="完成率") plt.bar(x+width,df["Female"],width = width,label="完成率") plt.bar(x+2*width,df["Male"],width = width,label="完成率") plt.bar(x+3*width,df["Rural"],width = width,label="完成率") plt.bar(x+4*width,df["Urban"],width = width,label="完成率") plt. legend() plt. show() 错误为unsupported operand type(s) for -: 'str' and 'float'怎么改
要解决这个问题,您需要将变量 `x` 中的字符串转换为浮点数。您可以使用 `.astype(float)` 方法将其转换为浮点数类型。请修改以下代码:
```python
y = ("Total", "Female", "Male", "Rural", "Urban")
x = df["educational level"].astype(float) # 将字符串转换为浮点数
total_width, n = 0.8, 5
width = total_width / n
x = x - (total_width - width) / 2
print(x)
plt.bar(x, df["Total"], width=width, label="完成率")
plt.bar(x + width, df["Female"], width=width, label="完成率")
plt.bar(x + 2 * width, df["Male"], width=width, label="完成率")
plt.bar(x + 3 * width, df["Rural"], width=width, label="完成率")
plt.bar(x + 4 * width, df["Urban"], width=width, label="完成率")
plt.legend()
plt.show()
```
在这里,我们使用 `.astype(float)` 方法将 `df["educational level"]` 中的字符串转换为浮点数。然后,我们进行了相应的计算,并使用转换后的浮点数 `x` 进行绘图。
请确保您的数据列中的所有值都可以正确转换为浮点数,以避免出现异常。
import numpy as np import matplotlib.pyplot as plt plt.rcParams["font.sans-serif"] = ["SimHei"] plt.rcParams["axes.unicode_minus"] = False x = np.arange(3) y1 = np.array([61.8, 42.8, 48.0]) y2 = np.array([32.9, 12.5, 13.3]) bar_width = 0.3 plt.bar(x, y1, width=bar_width, alpha=0.8, color='skyblue', label='获得offer比例') plt.bar(x + bar_width, y2, width=bar_width, alpha=0.8, color='pink', label='签约比例') plt.title('不同院校求职毕业生获得offer与签约比例') plt.xlabel('学历') plt.ylabel('签约率') plt.xticks(x + bar_width/2, ['双一流院校', '普通本科院校', '专科院校']) plt.legend(loc='upper right') plt.show()给这个图片添加标签,并美化
import numpy as np
import matplotlib.pyplot as plt
# 设置中文字体和负号正常显示
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
# 数据
x = np.arange(3)
y1 = np.array([61.8, 42.8, 48.0])
y2 = np.array([32.9, 12.5, 13.3])
# 柱状图参数
bar_width = 0.3
# 绘制柱状图
plt.bar(x, y1, width=bar_width, alpha=0.8, color='skyblue', label='获得offer比例')
plt.bar(x + bar_width, y2, width=bar_width, alpha=0.8, color='pink', label='签约比例')
# 添加标题、坐标轴标签和刻度
plt.title('不同院校求职毕业生获得offer与签约比例')
plt.xlabel('学历')
plt.ylabel('签约率')
plt.xticks(x + bar_width/2, ['双一流院校', '普通本科院校', '专科院校'])
# 添加数据标签
for a, b in zip(x, y1):
plt.text(a, b+0.5, '%.1f%%' % b, ha='center', va='bottom', fontsize=10)
for a, b in zip(x, y2):
plt.text(a+bar_width, b+0.5, '%.1f%%' % b, ha='center', va='bottom', fontsize=10)
# 美化图形
plt.ylim(0, 80) # 设定y轴范围
plt.legend(loc='upper right') # 添加图例
plt.show() # 显示图形
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)