使用 matplotlib 绘制数据并使用相同颜色为组直方图着色
Plotting the data using the matplotlib and coloring the group histograms with the same color
我正在尝试使用 matplotlib 库绘制图形。这是我的代码。
import numpy as np
import matplotlib.pyplot as plt
data = [[206.6, 735.4, 427.9, 175.2,384.4],
[487.5, 273.7, 742.6, 159.5,144],
[613.4, 0, 294.9, 0,0]]
X = np.arange(5)
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(X + 0.00, data[0], color = 'b', width = 0.25)
ax.bar(X + 0.25, data[1], color = 'g', width = 0.25)
ax.bar(X + 0.50, data[2], color = 'r', width = 0.25)
ax.legend(labels=['Group-1', 'Group-2','Group-3','Group-4','Group-5'])
我希望上面的图表组颜色相同。例如,第 1 组直方图应为 Red
,第 2 组直方图应为 Blue
,第 3 组直方图应为 Orange
,等等。如何使用上面的代码得到它
以下将从您的代码开始执行我认为您要求的操作。这个要求对我来说似乎有点奇怪,因为你可以在 x-axis 上标记图组,所以颜色提供了一种标记每个系列的明显方法。
import numpy as np
import matplotlib.pyplot as plt
data = [[206.6, 735.4, 427.9, 175.2,384.4],
[487.5, 273.7, 742.6, 159.5,144],
[613.4, 0, 294.9, 0,0]]
X = np.arange(5)
col_list = ['red','blue','orange','green','cyan']
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
handles = ax.bar(X + 0.00, data[0], color = col_list, width = 0.25)
ax.bar(X + 0.25, data[1], color = col_list, width = 0.25)
ax.bar(X + 0.50, data[2], color = col_list, width = 0.25)
ax.legend(labels=['Group-1','Group-2','Group-3','Group-4', 'Group-5'],
handles=handles)
plt.show()
给出:
我正在尝试使用 matplotlib 库绘制图形。这是我的代码。
import numpy as np
import matplotlib.pyplot as plt
data = [[206.6, 735.4, 427.9, 175.2,384.4],
[487.5, 273.7, 742.6, 159.5,144],
[613.4, 0, 294.9, 0,0]]
X = np.arange(5)
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(X + 0.00, data[0], color = 'b', width = 0.25)
ax.bar(X + 0.25, data[1], color = 'g', width = 0.25)
ax.bar(X + 0.50, data[2], color = 'r', width = 0.25)
ax.legend(labels=['Group-1', 'Group-2','Group-3','Group-4','Group-5'])
我希望上面的图表组颜色相同。例如,第 1 组直方图应为 Red
,第 2 组直方图应为 Blue
,第 3 组直方图应为 Orange
,等等。如何使用上面的代码得到它
以下将从您的代码开始执行我认为您要求的操作。这个要求对我来说似乎有点奇怪,因为你可以在 x-axis 上标记图组,所以颜色提供了一种标记每个系列的明显方法。
import numpy as np
import matplotlib.pyplot as plt
data = [[206.6, 735.4, 427.9, 175.2,384.4],
[487.5, 273.7, 742.6, 159.5,144],
[613.4, 0, 294.9, 0,0]]
X = np.arange(5)
col_list = ['red','blue','orange','green','cyan']
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
handles = ax.bar(X + 0.00, data[0], color = col_list, width = 0.25)
ax.bar(X + 0.25, data[1], color = col_list, width = 0.25)
ax.bar(X + 0.50, data[2], color = col_list, width = 0.25)
ax.legend(labels=['Group-1','Group-2','Group-3','Group-4', 'Group-5'],
handles=handles)
plt.show()
给出: