用python代码,用条形图显示1月份到五月份苹果和葡萄的月销售额对比。苹果1月到5月的销售额是20,30,15,17,30吨,而葡萄1月到5月的销售额19,28,25,31,40吨。苹果用红色条形图表示,葡萄用绿色条形图表示。如下图:
时间: 2024-03-09 20:46:13 浏览: 6
可以使用Python中的Matplotlib库来画条形图。
以下是代码实现:
```python
import matplotlib.pyplot as plt
# 设置数据
apple_sales = [20, 30, 15, 17, 30]
grape_sales = [19, 28, 25, 31, 40]
# 设置颜色
apple_color = 'red'
grape_color = 'green'
# 设置x轴标签和位置
x_label = ['January', 'February', 'March', 'April', 'May']
x_pos = [i for i, _ in enumerate(x_label)]
# 绘制条形图
plt.bar(x_pos, apple_sales, color=apple_color, width=0.4)
plt.bar([i + 0.4 for i in x_pos], grape_sales, color=grape_color, width=0.4)
# 设置图例和标题
plt.legend(['Apple', 'Grape'])
plt.title('Monthly Sales of Apple and Grape')
# 设置x轴标签
plt.xticks([i + 0.2 for i in x_pos], x_label)
# 显示图像
plt.show()
```
运行以上代码,将得到以下条形图:
![image.png](attachment:image.png)
希望能够帮到你!