Commit dec9f9e9 authored by Kris Vanneste's avatar Kris Vanneste
Browse files

Added text_legend_handler.py.

git-svn-id: https://svn.seismo.oma.be/svn/seismo/mapping/layeredbasemap/trunk@7739 40b490c5-b4d9-47cb-8714-9bef99b524d5
parent 5cba2e60
# -*- coding: utf-8 -*-
"""
Matplotlib legend handler for text artists
Based on https://stackoverflow.com/questions/27174425/how-to-add-a-string-as-the-artist-in-matplotlib-legend
Created on Fri May 7 10:29:50 2021
@author: kris
"""
from matplotlib.legend_handler import HandlerBase
from matplotlib.text import Text
class TextHandler(HandlerBase):
"""
Handler for text artists in matplotlib legends
:param font_size:
float, font size
(default: 10)
:param font_weight:
str, font weight, e.g. 'normal', 'bold'
(default: 'normal')
:param font_color:
matplotlib color spec for font
(default: 'k')
In order to use this, add the following lines in your script after importing:
from matplotlib.legend import Legend
Legend.update_default_handler_map({str : TextHandler()})
"""
def __init__(self, font_size=10, font_weight='normal', font_color='k'):
HandlerBase.__init__(self)
self.font_size = font_size
self.font_weight = font_weight
self.font_color = font_color
def create_artists(self, legend, text ,xdescent, ydescent,
width, height, fontsize, trans):
tx = Text(width/2.,height/2, text, fontsize=self.font_size,
ha="center", va="center", fontweight=self.font_weight,
color=self.font_color)
return [tx]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment