python - Matplotlib plot multiple bars in one graph
问题描述
I have a plot with multiple bars with different scenarios, but when I plot it all the bars are repeated. Please find below my code.
I know that I'm using only one value at a time from the list, but when I try to pass the whole sub-array using data[0]
instead, I get a Value mismatch error:
ValueError: shape mismatch: objects cannot be broadcast to a single shape
What am I doing wrong? I looked at the PyPlot example and this other post and both pass an array to ax.bar
.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = [[20, 35, 30, 40], [25, 40, 45, 30],
[15, 20, 35, 45], [10, 25, 40, 15],
[50, 20, 45, 55], [10, 55, 60, 20]]
data_std = [[1, 2, 1, 2], [1, 2, 1, 2], [1, 2, 1, 2],
[1, 2, 1, 2], [1, 2, 1, 2], [1, 2, 1, 2]]
length = len(data)
x_labels = ['A', 'B', 'C', 'D', 'E', 'F']
# Set plot parameters
fig, ax = plt.subplots()
width = 0.2 # width of bar
x = np.arange(length)
ax.bar(x, data[0][0], width, color='#000080', label='Case-1', yerr=data_std[0][0])
ax.bar(x + width, data[0][1], width, color='#0F52BA', label='Case-2', yerr=data_std[0][1])
ax.bar(x + (2 * width), data[0][2], width, color='#6593F5', label='Case-3', yerr=data_std[0][2])
ax.bar(x + (3 * width), data[0][3], width, color='#73C2FB', label='Case-4', yerr=data_std[0][3])
ax.set_ylabel('Metric')
ax.set_ylim(0,75)
ax.set_xticks(x + width + width/2)
ax.set_xticklabels(x_labels)
ax.set_xlabel('Scenario')
ax.set_title('Title')
ax.legend()
plt.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3)
fig.tight_layout()
plt.show()
Result is:
解决方案
您想按列绘制数据。因此,将列表转换为数组并选择要绘制的相应列是有意义的。
import numpy as np
import matplotlib.pyplot as plt
data = np.array([[20, 35, 30, 40], [25, 40, 45, 30],
[15, 20, 35, 45], [10, 25, 40, 15],
[50, 20, 45, 55], [10, 55, 60, 20]])
data_std = np.array([[1, 2, 1, 2], [1, 2, 1, 2], [1, 2, 1, 2],
[1, 2, 1, 2], [1, 2, 1, 2], [1, 2, 1, 2]])
length = len(data)
x_labels = ['A', 'B', 'C', 'D', 'E', 'F']
# Set plot parameters
fig, ax = plt.subplots()
width = 0.2 # width of bar
x = np.arange(length)
ax.bar(x, data[:,0], width, color='#000080', label='Case-1', yerr=data_std[:,0])
ax.bar(x + width, data[:,1], width, color='#0F52BA', label='Case-2', yerr=data_std[:,1])
ax.bar(x + (2 * width), data[:,2], width, color='#6593F5', label='Case-3', yerr=data_std[:,2])
ax.bar(x + (3 * width), data[:,3], width, color='#73C2FB', label='Case-4', yerr=data_std[:,3])
ax.set_ylabel('Metric')
ax.set_ylim(0,75)
ax.set_xticks(x + width + width/2)
ax.set_xticklabels(x_labels)
ax.set_xlabel('Scenario')
ax.set_title('Title')
ax.legend()
plt.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3)
fig.tight_layout()
plt.show()
推荐阅读
- javascript - 使用 Reduce、Map 和 Concat 与数组组合的 Javascript 嵌套函数
- python-3.x - 列格式大树的Tkinter Treeview双击问题
- vue.js - 如何在大型 VUE 项目中组织导入 mixins、过滤器和指令
- c# - 如何将两个连接表中的 IQueryable LINQ 结果返回到列表中
? - flutter - 列表
' 不是类型 'Map 的子类型 - docker - 无法从文件安装包,上次元数据过期检查,错误:找不到匹配项,Centos 8
- react-native - React Native:使用钩子和 TextInput 更新状态对象
- android - 公版通知无法展开
- mysql - 在第 1 行的配置文件 /etc/my.cnf 中找到没有前面组的选项
- python - mega.py 我可以打开浏览器吗?