OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Replace Matplotlib legend's labels with image

  • Thread starter Thread starter Drico
  • Start date Start date
D

Drico

Guest
I would like to use image instead of labels in the legend.

For example, I draw 2 lines and show a legend :

Code:
import matplotlib.pyplot as plt
plt.plot([1,2],label="first_image")
plt.plot([2,1],label="second_image")
plt.legend()
plot.show()

What I have now

But I would like to have something like this :

The result I need

Note that this is not a duplicate of Insert image in matplotlib legend , My issue is "change the label into an image", while the other is "change the legend's symbol into an image"
<p>I would like to use image instead of labels in the legend.</p>

<p>For example, I draw 2 lines and show a legend :</p>

<pre><code>import matplotlib.pyplot as plt
plt.plot([1,2],label="first_image")
plt.plot([2,1],label="second_image")
plt.legend()
plot.show()
</code></pre>

<p><a href="https://i.sstatic.net/WEnms.png" rel="noreferrer"><img src="https://i.sstatic.net/WEnms.png" alt="What I have now"></a></p>

<p>But I would like to have something like this :</p>

<p><a href="https://i.sstatic.net/DsLP9.png" rel="noreferrer"><img src="https://i.sstatic.net/DsLP9.png" alt="The result I need"></a></p>

<p>Note that this is not a duplicate of <a href="https://stackoverflow.com/questions/26029592/insert-image-in-matplotlib-legend">Insert image in matplotlib legend</a> ,
My issue is "change the label into an image", while the other is "change the legend's symbol into an image"</p>
 
Top