[英]matplotlib two legends out of bar plot
My question is very similar to matplotlib two legends out of plot . 我的问题非常类似matplotlib两个传说中的情节 。 There's an answer which works fine for common line plots.
有一个答案适用于普通线图。
I faced a problem with copying the solution for bar plots... 我遇到了复制条形图解决方案的问题......
The problem is that in the given solution l1
, l2
, ... are <matplotlib.lines.Line2D
and if i do the same trick for bar-plot
, it cannot infer the colors... 问题是 ,在给定的解决方案中,
l1
, l2
,...是<matplotlib.lines.Line2D
,如果我对bar-plot
执行相同的技巧,则无法推断颜色...
Code: 码:
import matplotlib.pyplot as plt
import numpy as np
bar_data_cost = np.random.rand(4,11)
bar_data_yield = np.random.rand(4,11)
cmap_yield = plt.cm.Greens(np.linspace(0.2, 1, len(bar_data_cost)))
cmap_costs = plt.cm.Oranges(np.linspace(0.2, 1, len(bar_data_cost)))
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(20,8))
ax1 = axes
y_offset_yield = np.zeros(len(bar_data_yield[0]))
y_offset_cost = np.zeros(len(bar_data_cost[0]))
index1 = np.arange(len(bar_data_yield[1])) - 0.2
index2 = np.arange(len(bar_data_yield[1])) + 0.2
for row in range(len(bar_data_yield)):
b1 = ax1.bar(left=index1, width=0.4, height=bar_data_yield[row], bottom=y_offset_yield, color=cmap_yield[row])
y_offset_yield = bar_data_yield[row]
for row in range(len(bar_data_yield)):
b2 = ax1.bar(left=index2, width=0.4, height=bar_data_cost[row], bottom=y_offset_cost, color=cmap_costs[row])
y_offset_cost = bar_data_cost[row]
fig.legend(b1, grouped_dataset.index.levels[0], fontsize=16, loc="upper right")
fig.legend(b2, grouped_dataset.index.levels[0], fontsize=16, loc="center right")
Currently, your legend outputs only the last b1 and b2 from for
loops since they are re-assigned with each iteration. 目前,您的图例仅输出
for
循环中的最后一个b1和b2 ,因为它们会在每次迭代时重新分配。 In posted link, a tuple of lines are passed in first argument of legend
. 在发布的链接中,在
legend
第一个参数中传递一个行元组。 Hence, pass a list of b1 and list of b2 into legend
calls after appending bars iteratively. 因此,在迭代附加条形后,将b1列表和b2列表传递给
legend
调用。
Below demonstrates with seeded data for reproducibility and substitutes your grouped_dataset.index.levels[0]
as this is unknown from your post. 下面演示了种子数据的可重复性,并替换了您的
grouped_dataset.index.levels[0]
因为这在您的帖子中是未知的。
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(82018)
...
b1_list = []
for row in range(len(bar_data_yield)):
index1 = np.arange(len(bar_data_yield[row])) - 0.2
b1_list.append(ax1.bar(left=index1, width=0.4, height=bar_data_yield[row],
bottom=y_offset_yield, color=cmap_yield[row]))
y_offset_yield = bar_data_yield[row]
b2_list = []
for row in range(len(bar_data_yield)):
index2 = np.arange(len(bar_data_yield[row])) + 0.2
b2_list.append(ax1.bar(left=index2, width=0.4, height=bar_data_cost[row],
bottom=y_offset_cost, color=cmap_costs[row]))
y_offset_cost = bar_data_cost[row]
fig.legend(b1_list, list('ABCD'), fontsize=16, loc="upper right")
fig.legend(b2_list, list('WXYZ'), fontsize=16, loc="center right")
plt.show()
plt.clf()
plt.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.