Python Forum
Unique Legend Image and Line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unique Legend Image and Line
#1
I would like to plot 2 curves and, in the legend, have a unique image for each curve and the corresponding curve color to show which it belongs to.  I cannot figure out how to replace the text "Curve 1" or "Curve 2" with a sample of the actual curve, as you would usually see in the place of the monkey/giraffe in my case.

I don't have  a great grasp on the handler, but I have enough to create the following code. See the attached image for clarification. To reproduce, you can download a monkey and giraffe png (monkey.png and giraffe.png) into the same directory you run the code in.

So instead of the legend showing:
image1 "curve 1" (where "Curve 1 is the actual text"
image2 "curve 2"

I would like:
image1 curve1 (where curve1 is a segment representing curve 1 (green line)
image2 curve2

I appreciate any help you can offer!

import os

from matplotlib.transforms import TransformedBbox
from matplotlib.image import BboxImage
from matplotlib.legend_handler import HandlerBase
from matplotlib._png import read_png
from matplotlib.cbook import get_sample_data
from matplotlib.transforms import Bbox
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc('text', usetex = True)


class ImageHandler(HandlerBase):
    def create_artists(self, legend, orig_handle,
                       xdescent, ydescent, width, height, fontsize,
                       trans):

        # enlarge the image by these margins
        sx, sy = self.image_stretch

        # create a bounding box to house the image
        bb = Bbox.from_bounds(xdescent - sx,
                              ydescent - sy,
                              width + sx,
                              height + sy)

        tbb = TransformedBbox(bb, trans)
        image = BboxImage(tbb)
        image.set_data(self.image_data)

        self.update_prop(image, orig_handle, legend)

        return [image]

    def set_image(self, image_path, image_stretch=(0, 0)):
        if not os.path.exists(image_path):
            #sample = get_sample_data("grace_hopper.png", asfileobj=False)
            sample = "monkey.png"
            self.image_data = read_png(sample)
        else:
            self.image_data = read_png(image_path)

        self.image_stretch = image_stretch
class ImageHandler2(HandlerBase):
    def create_artists(self, legend, orig_handle,
                       xdescent, ydescent, width, height, fontsize,
                       trans):

        # enlarge the image by these margins
        sx, sy = self.image_stretch

        # create a bounding box to house the image
        bb = Bbox.from_bounds(xdescent - sx,
                              ydescent - sy,
                              width + sx,
                              height + sy)

        tbb = TransformedBbox(bb, trans)
        image = BboxImage(tbb)
        image.set_data(self.image_data)

        self.update_prop(image, orig_handle, legend)

        return [image]

    def set_image(self, image_path, image_stretch=(0, 0)):
        if not os.path.exists(image_path):
            #sample = get_sample_data("grace_hopper.png", asfileobj=False)
            sample = "giraffe.png"
            self.image_data = read_png(sample)
        else:
            self.image_data = read_png(image_path)

        self.image_stretch = image_stretch

lw = 1.5;      # LineWidth

# Random curve
d1 = np.linspace(25,3500);
h1 = 2.020 * d1 **(0.449);
h2 = 2.8 * d1 **(0.467);
plt.figure()
s, = plt.plot(d1,h1,'g',linewidth=lw)
s2, = plt.plot(d1,h2,'b',linewidth=lw)

# setup the handler instance for the scattered data
custom_handler = ImageHandler()
custom_handler.set_image("[PATH TO IMAGE]",
                         image_stretch=(0, 20)) # this is for grace hopper
custom_handler2 = ImageHandler2()
custom_handler2.set_image("[PATH TO IMAGE]",
                         image_stretch=(0, 20)) # this is for grace hopper

# add the legend for the scattered data, mapping the
# scattered points to the custom handler

plt.legend([s, s2],
           ['Curve 1', 'Curve 2'],
           handler_map={s: custom_handler, s2: custom_handler2},
           labelspacing=2,
           frameon=False,
           loc=2)
plt.show()

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
Unique Legend Image and Line - by nettesheim2 - Sep-28-2016, 02:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I need a code line to spam a keyboard key | Image detection bot Aizou 2 3,175 Dec-06-2020, 10:10 PM
Last Post: Aizou
  saving only one line of a figure as an image (python matplotlib) nitrochloric 0 2,063 Nov-23-2020, 01:41 PM
Last Post: nitrochloric
  Display the bottom image of the line and cut the upper image using Opencv jenkins43 1 3,299 May-27-2019, 06:56 AM
Last Post: heiner55
  legend/color mcgrim 5 3,222 Apr-17-2019, 08:27 AM
Last Post: mcgrim
  How to find unique and common words per line from a txt file? sweet_swiss 6 4,367 Aug-11-2018, 01:28 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020