0

I am trying to read an image from an SVG file and insert in matplotlib figure.

import matplotlib.pyplot as plt
import pylustrator as pyl
import numpy as np
from matplotlib.figure import Figure
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from svglib.svglib import svg2rlg

ax = plt.subplot(111)
ax.plot(
    [1, 2, 3], [1, 2, 3],
    'go-',
    label='line 1',
    linewidth=2
 )
# arr_img = plt.imread("stinkbug.svg")
# arr_img = svg2rlg("stinkbug.svg")
arr_img = pyl.load("stinkbug.svg")
im = OffsetImage(arr_img)
ab = AnnotationBbox(im, (1, 0), xycoords='axes fraction')
ax.add_artist(ab)
plt.show()

I tried to use pylustratorpost for reading the svg image. The code works when the input image is in png format. But I am not able to add the same image saved in svg extension(image).

I couldn't successfully insert due to the following error,

    "float".format(self._A.dtype))
TypeError: Image data of dtype object cannot be converted to float

Suggestions on how to fix this will be really helpful.

Natasha
  • 1,111
  • 5
  • 28
  • 66
  • @Randrian Could you please have a look? – Natasha Mar 09 '21 at 05:29
  • How does this differ from the second half of [your previous question](https://stackoverflow.com/questions/66540026/insert-an-svg-image-in-matplotlib-figure) i.e. why aren't they duplicates? – Robert Longson Mar 09 '21 at 08:36

0 Answers0