在 matplotlib 图例中使用文本但不使用标记 [英] Use text but not marker in matplotlib legend

查看:28
本文介绍了在 matplotlib 图例中使用文本但不使用标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在图表中使用

所以我想要做的是将图例中的颜色条替换为与图中相同的图标.

我使用的句柄是一个补丁列表.但是我发现很难在 Patch 中添加文本.我发现这里有一个很好的解决方案可以将图片添加到图例中:

另见这个更通用的答案

I am using FontAwesome in my chart, and each data point is a symbol in FontAwesome font, displaying like an icon. Therefore, in the legend, I would like to use text (symbols in FontAwesome) to describe the items.

The code I am using is like:

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')])

And the plot is like:

So what I want to do is to replace the color bar in the legend to the same icons as it in the plot.

The handle I am using is a list of Patches. But I found that it's hard to add text into Patch. I found there's a great solution to add picture into legend here: Insert image in matplotlib legend

I have tried using TextArea replacing BboxImage in that answer, but it doesn't work, and TextArea doesn't support fontproperties like axis.text.

So is there a way I can use text instead of markers in the legend?

解决方案

For a solution using TextArea see this answer. You would then need to recreate the fontproperties for the text inside the TextArea.

Since here you want to show exactly the symbol you have as text also in the legend, a simpler way to create a legend handler for some text object would be the following, which maps the text to a TextHandler. The TextHandler subclasses matplotlib.legend_handler.HandlerBase and its create_artists produces a copy of the text to show in the legend. Some of the text properties then need to be adjusted for the legend.

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()

Also see this more generic answer

这篇关于在 matplotlib 图例中使用文本但不使用标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆