我想为堆叠条形图的每个组件赋予自定义颜色。例如,在下面的条形图中:
import numpy as np
import matplotlib.pyplot as plt
labels = ['Cars', 'Electric/\nHybrid/\nFuel', 'Diesel/\nOctane/\nPremium']
row1 = [2000,1800,1200]
row2 = [0,110,280]
row3 = [0,90,320]
width = 0.35
fig, ax = plt.subplots()
ax.bar(labels, row1, width, color='seagreen')
ax.bar(labels, row2, width, bottom=row1, color='gray')
ax.bar(labels, row3, width, bottom=np.array(row2)+np.array(row1), color='orange')
ax.set_ylim(0,2200)
plt.show()
我想为第2列和第3列中堆积条形图的每个组件赋予自定义颜色。第2列显示第1列的细分,第3列显示第2列的绿色组件的细分。 / p>
答案 0 :(得分:1)
epoch : 0 loss: 5.2415571212768555 training_acc tensor(0.3103, device='cuda:0') 0m 2s
epoch : 0 loss: 5.228370666503906 training_acc tensor(0.3235, device='cuda:0') 0m 2s
epoch : 0 loss: 5.224219799041748 training_acc tensor(0.3276, device='cuda:0') 0m 2s
epoch : 0 loss: 5.222436428070068 training_acc tensor(0.3294, device='cuda:0') 0m 2s
epoch : 0 loss: 5.2202887535095215 training_acc tensor(0.3316, device='cuda:0') 0m 2s
epoch : 1 loss: 5.2415571212768555 training_acc tensor(0.3103, device='cuda:0') 0m 0s
epoch : 1 loss: 5.22836971282959 training_acc tensor(0.3235, device='cuda:0') 0m 0s
epoch : 1 loss: 5.224219799041748 training_acc tensor(0.3276, device='cuda:0') 0m 0s
epoch : 1 loss: 5.222436428070068 training_acc tensor(0.3294, device='cuda:0') 0m 1s
epoch : 1 loss: 5.2202887535095215 training_acc tensor(0.3316, device='cuda:0') 0m 1s
epoch : 2 loss: 5.2415571212768555 training_acc tensor(0.3103, device='cuda:0') 0m 0s
epoch : 2 loss: 5.22836971282959 training_acc tensor(0.3235, device='cuda:0') 0m 0s
epoch : 2 loss: 5.224219799041748 training_acc tensor(0.3276, device='cuda:0') 0m 0s
epoch : 2 loss: 5.222436428070068 training_acc tensor(0.3294, device='cuda:0') 0m 1s
epoch : 2 loss: 5.2202887535095215 training_acc tensor(0.3316, device='cuda:0') 0m 1s
epoch : 3 loss: 5.2415571212768555 training_acc tensor(0.3103, device='cuda:0') 0m 0s
epoch : 3 loss: 5.22836971282959 training_acc tensor(0.3235, device='cuda:0') 0m 0s
epoch : 3 loss: 5.224219799041748 training_acc tensor(0.3276, device='cuda:0') 0m 0s
epoch : 3 loss: 5.222436428070068 training_acc tensor(0.3294, device='cuda:0') 0m 1s
epoch : 3 loss: 5.2202887535095215 training_acc tensor(0.3316, device='cuda:0') 0m 1s
包含用于创建条形图的矩形列表(按创建顺序)。因此,有3个“海绿色”矩形,3个“灰色”矩形和3个“橙色”矩形。
如果定义所需颜色的列表(必须匹配矩形的数量,包括零高度矩形),则可以遍历色块列表并设置其颜色
ax.patches