如何更改 Matplotlib 图例中标记(补丁)的边缘颜色?
How to change the edge color of markers (patches) in Matplotlib legend?
我知道绘制条形图时填充颜色遵循边缘颜色,所以我绘制了两次条形图以分离填充颜色和边缘颜色。虽然这种方法在条形图上有效,但我不知道如何改变图例中标记的边缘颜色,使图例中的标记与条形图保持一致。
这是我的代码和绘制的图片。我想让图例中的标记也有像条一样的黑边。
import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
x_lable = ['3', '4', '5', '6', '7']
width = 0.3
data_1 = [2228, 2621, 3165, 3761, 3763]
data_2 = [3895, 5670, 7354, 8999, 10731]
data_3 = [4355, 6373, 8279, 10105, 12018]
xcoordinate = [1, 3, 5, 7, 9]
xcoordinate_1 = [xcoordinate[0] + 0 * width, xcoordinate[1] + 0 * width, xcoordinate[2] + 0 * width, xcoordinate[3] + 0 * width, xcoordinate[4] + 0 * width]
xcoordinate_2 = [xcoordinate[0] + 1 * width, xcoordinate[1] + 1 * width, xcoordinate[2] + 1 * width, xcoordinate[3] + 1 * width, xcoordinate[4] + 1 * width]
xcoordinate_3 = [xcoordinate[0] + 2 * width, xcoordinate[1] + 2 * width, xcoordinate[2] + 2 * width, xcoordinate[3] + 2 * width, xcoordinate[4] + 2 * width]
xcoordinate_4 = [xcoordinate[0] + 3 * width, xcoordinate[1] + 3 * width, xcoordinate[2] + 3 * width, xcoordinate[3] + 3 * width, xcoordinate[4] + 3 * width]
xcoordinate_5 = [xcoordinate[0] + 4 * width, xcoordinate[1] + 4 * width, xcoordinate[2] + 4 * width, xcoordinate[3] + 4 * width, xcoordinate[4] + 4 * width]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.bar(xcoordinate_3, data_1, width=width, label='GGG', color='none', edgecolor='orange', hatch='\\\')
ax1.bar(xcoordinate_4, data_2, width=width, label='SSS', color='none', edgecolor='blue', hatch='---')
ax1.bar(xcoordinate_5, data_3, width=width, label='KKK', color='none', edgecolor='orangered', hatch='///')
fmt='%.2f%%'
yticks = mtick.FormatStrFormatter(fmt)
ax1.set_ylabel('AAA')
ax1.set_xlabel('BBB')
plt.xticks([i + 3 * width for i in xcoordinate], x_lable)
ax1.bar(xcoordinate_3, data_1, width=width, label='GGG', color='none', edgecolor='black')
ax1.bar(xcoordinate_4, data_2, width=width, label='SSS', color='none', edgecolor='black')
ax1.bar(xcoordinate_5, data_3, width=width, label='KKK', color='none', edgecolor='black')
handles1, labels1 = ax1.get_legend_handles_labels()
order = [0, 1, 2]
plt.legend([handles1[idx] for idx in order], [labels1[idx] for idx in order], loc=(2.5/10, 1.01), frameon=True, ncol=3, shadow=False, framealpha=1, labelspacing=1,handlelength=2).get_frame().set_edgecolor('black')
plt.show()
只需添加第二个句柄:
order = [0, 1, 2]
plt.legend([handles1[idx]+handles1[idx+3] for idx in order], [labels1[idx] for idx in order], loc=(2.5/10, 1.01), frameon=True, ncol=3, shadow=False, framealpha=1, labelspacing=1,handlelength=2).get_frame().set_edgecolor('black')
示例输出:
我知道绘制条形图时填充颜色遵循边缘颜色,所以我绘制了两次条形图以分离填充颜色和边缘颜色。虽然这种方法在条形图上有效,但我不知道如何改变图例中标记的边缘颜色,使图例中的标记与条形图保持一致。 这是我的代码和绘制的图片。我想让图例中的标记也有像条一样的黑边。
import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
x_lable = ['3', '4', '5', '6', '7']
width = 0.3
data_1 = [2228, 2621, 3165, 3761, 3763]
data_2 = [3895, 5670, 7354, 8999, 10731]
data_3 = [4355, 6373, 8279, 10105, 12018]
xcoordinate = [1, 3, 5, 7, 9]
xcoordinate_1 = [xcoordinate[0] + 0 * width, xcoordinate[1] + 0 * width, xcoordinate[2] + 0 * width, xcoordinate[3] + 0 * width, xcoordinate[4] + 0 * width]
xcoordinate_2 = [xcoordinate[0] + 1 * width, xcoordinate[1] + 1 * width, xcoordinate[2] + 1 * width, xcoordinate[3] + 1 * width, xcoordinate[4] + 1 * width]
xcoordinate_3 = [xcoordinate[0] + 2 * width, xcoordinate[1] + 2 * width, xcoordinate[2] + 2 * width, xcoordinate[3] + 2 * width, xcoordinate[4] + 2 * width]
xcoordinate_4 = [xcoordinate[0] + 3 * width, xcoordinate[1] + 3 * width, xcoordinate[2] + 3 * width, xcoordinate[3] + 3 * width, xcoordinate[4] + 3 * width]
xcoordinate_5 = [xcoordinate[0] + 4 * width, xcoordinate[1] + 4 * width, xcoordinate[2] + 4 * width, xcoordinate[3] + 4 * width, xcoordinate[4] + 4 * width]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.bar(xcoordinate_3, data_1, width=width, label='GGG', color='none', edgecolor='orange', hatch='\\\')
ax1.bar(xcoordinate_4, data_2, width=width, label='SSS', color='none', edgecolor='blue', hatch='---')
ax1.bar(xcoordinate_5, data_3, width=width, label='KKK', color='none', edgecolor='orangered', hatch='///')
fmt='%.2f%%'
yticks = mtick.FormatStrFormatter(fmt)
ax1.set_ylabel('AAA')
ax1.set_xlabel('BBB')
plt.xticks([i + 3 * width for i in xcoordinate], x_lable)
ax1.bar(xcoordinate_3, data_1, width=width, label='GGG', color='none', edgecolor='black')
ax1.bar(xcoordinate_4, data_2, width=width, label='SSS', color='none', edgecolor='black')
ax1.bar(xcoordinate_5, data_3, width=width, label='KKK', color='none', edgecolor='black')
handles1, labels1 = ax1.get_legend_handles_labels()
order = [0, 1, 2]
plt.legend([handles1[idx] for idx in order], [labels1[idx] for idx in order], loc=(2.5/10, 1.01), frameon=True, ncol=3, shadow=False, framealpha=1, labelspacing=1,handlelength=2).get_frame().set_edgecolor('black')
plt.show()
只需添加第二个句柄:
order = [0, 1, 2]
plt.legend([handles1[idx]+handles1[idx+3] for idx in order], [labels1[idx] for idx in order], loc=(2.5/10, 1.01), frameon=True, ncol=3, shadow=False, framealpha=1, labelspacing=1,handlelength=2).get_frame().set_edgecolor('black')
示例输出: