Gua*_* Li 4 python visualization matplotlib
我在我的图表中使用FontAwesome,每个数据点都是FontAwesome字体中的符号,显示为图标.因此,在图例中,我想使用文本(FontAwesome中的符号)来描述项目.
我使用的代码如下:
from matplotlib.patches import Patch
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
ax = plt.gca()
ax.axis([0, 3, 0, 3])
prop = fm.FontProperties(fname='FontAwesome.otf', size=18)
ax.text(x=0, y=0, s='\uf1c7', color='r', fontproperties=prop)
ax.text(x=2, y=0, s='\uf050', color='g', fontproperties=prop)
plt.legend(handles=[Patch(color='r', label='label1'), Patch(color='g', label='label2')])
Run Code Online (Sandbox Code Playgroud)
所以我想要做的是将图例中的颜色条替换为与图中相同的图标.
我使用的句柄是补丁列表.但我发现在Patch中添加文本很难.我发现这里有一个很好的解决方案可以将图片添加到图例中:在matplotlib图例中插入图片
我已尝试在该答案中使用TextArea替换BboxImage,但它不起作用,TextArea不支持像axis.text这样的fontproperties.
那么有没有一种方法可以在图例中使用文字而不是标记?
有关解决方案,TextArea
请参阅此答案.然后,您需要为其中的文本重新创建fontproperties TextArea
.
因为在这里你想要在图例中准确显示你作为文本的符号,为一些文本对象创建图例处理程序的简单方法如下,将文本映射到a TextHandler
.该TextHandler
子类matplotlib.legend_handler.HandlerBase
及其create_artists
产生的文本的副本,在图例显示.然后需要为图例调整一些文本属性.
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerBase
import copy
ax = plt.gca()
ax.axis([-1, 3,-1, 2])
tx1 = ax.text(x=0, y=0, s=ur'$\u2660$', color='r',size=30, ha="right")
tx2 = ax.text(x=2, y=0, s=ur'$\u2665$', color='g',size=30)
class TextHandler(HandlerBase):
def create_artists(self, legend, orig_handle,xdescent, ydescent,
width, height, fontsize,trans):
h = copy.copy(orig_handle)
h.set_position((width/2.,height/2.))
h.set_transform(trans)
h.set_ha("center");h.set_va("center")
fp = orig_handle.get_font_properties().copy()
fp.set_size(fontsize)
# uncomment the following line,
# if legend symbol should have the same size as in the plot
h.set_font_properties(fp)
return [h]
labels = ["label 1", "label 2"]
handles = [tx1,tx2]
handlermap = {type(tx1) : TextHandler()}
ax.legend(handles, labels, handler_map=handlermap,)
plt.show()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
576 次 |
最近记录: |