有没有办法实现这个目标?
下面是我的情节代码。
from pylab import *
import re
matplotlib.rc('xtick', labelsize=16)
matplotlib.rc('ytick', labelsize=16)
plt.rcParams['font.size'] = 16
plt.rcParams['axes.labelsize'] = 16
plt.rcParams['xtick.labelsize'] = 16
plt.rcParams['ytick.labelsize'] = 16
plt.rcParams['legend.fontsize'] = 16
rc('font',family='Arial')
fig, ax = plt.subplots(figsize=(5,4))
x = array([2.2,4.4])
a = array([1,2])
b = array([1,2])
c = array([1,2])
total_width, n = 1, 3
width = total_width / n
x = x - width*1.5
xmarks=[2.2,4.4]
plt.xticks(xmarks)
xx=plt.bar(x, a, width=width, color='r',alpha=0.5,label='0%')
xy=plt.bar(x + width, b, width=width, color='b',alpha=0.5,label='60%')
xz=plt.bar(x + 2 * width, c, width=width,color='g', alpha=0.5,label='100%')
extra = Rectangle((0, 0), 1, 1, fc="w", fill=False, edgecolor='none', linewidth=0)
leg=ax.legend([extra, xx, xy,xz], ("IOTs replacement\nwith FAC", "0%", "60%",'100%'),loc='upper left')
leg.get_frame().set_alpha(0.0)
plt.show()
答案 0 :(得分:1)
最好使用title
参数:
leg = ax.legend([xx, xy,xz],
("0%", "60%",'100%'),
title="IOTs replacement\nwith FAC",
loc='upper left')
看起来您需要对图例和标题的位置进行一些手动调整:
leg = ax.legend([xx, xy,xz],
("0%", "60%",'100%'),
title="IOTs replacement\nwith FAC",
loc=(-0.035, 0.48))
leg.get_title().set_position((18, 0))
答案 1 :(得分:1)
图例可以有一个由title
参数指定的标题。图例和轴边界之间的空间由参数borderaxespad
确定,可以设置为零。
要将图例设为left aligned in the legend box,您可以将图例的_legend_box.align
设置为“左”。
leg=ax.legend([xx, xy,xz], ("0%", "60%",'100%'),
loc='upper left',
title = "IOTs replacement\nwith FAC",
borderaxespad=0)
leg.get_frame().set_alpha(0.0)
leg._legend_box.align = "left"