答案 0 :(得分:1)
您可以创建一个自定义图例对象并将其定位在颜色栏旁边。显示为随机图:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
import matplotlib.patches as patches
class SquareObject(object):
pass
# Custom legend object
class SquareObjectHandler(object):
def legend_artist(self, legend, orig_handle, fontsize, handlebox):
x0, y0 = handlebox.xdescent, handlebox.ydescent
width, height = handlebox.width, handlebox.height
l1 = patches.Rectangle(
(x0, y0), # (x,y)
width / 2, # width
height, # height
fill=True,
facecolor="green",
)
handlebox.add_artist(l1)
return [l1]
fig, ax1 = plt.subplots(1, 1, figsize=(14, 6))
im = ax1.imshow(np.arange(100).reshape((10, 10)))
# To locate the colorbar
divider = make_axes_locatable(ax1)
cax = divider.append_axes('right', size='5%', pad=0.05)
plt.colorbar(im, cax=cax, label="colorbar")
# Add the legend
ax1.legend([SquareObject()],
['Reference'],
handler_map={SquareObject: SquareObjectHandler()},
loc='right center',
bbox_to_anchor=(1.4, 0.8), #(x, y)
frameon=False,
handletextpad=-0.5)
plt.show()
您可以使用bbox_to_anchor
参数移动图例。
答案 1 :(得分:1)
如果有人可能遇到与我相同的问题,只需在此处发布即可。为了从颜色栏中获取颜色,我计算了颜色栏中给定参考的相应比例。
"integration" : {
"persist_globals" : true,
"launch_url" : "https://test.url",
"globals" : {
"foo" : "var"
},
"desiredCapabilities" : {
"browserName" : "firefox",
"acceptInsecureCerts" : true,
"alwaysMatch" : {
"acceptInsecureCerts" : true,
"moz:firefoxOptions" : {
"args" : [ "-headless" ]
}
}
}
},
最后,将其设置为“ SquareObjectHandler”类中的“ facecolor”。 位置将是相同的方式。找出颜色条的坐标,然后在“图例”中设置“ bbox_to_anchor”。