rgerum / pylustrator

Visualisations of data are at the core of every publication of scientific research results. They have to be as clear as possible to facilitate the communication of research. As data can have different formats and shapes, the visualisations often have to be adapted to reflect the data as well as possible. We developed Pylustrator, an interface to directly edit python generated matplotlib graphs to finalize them for publication. Therefore, subplots can be resized and dragged around by the mouse, text and annotations can be added. The changes can be saved to the initial plot file as python code.
GNU General Public License v3.0
706 stars 38 forks source link

read an svg file to insert in matplotlib #29

Closed DeepaMahm closed 3 years ago

DeepaMahm commented 3 years ago

Hi @rgerum, 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.

rgerum commented 3 years ago

You can directly add it with pylustrator.load

import matplotlib.pyplot as plt
import pylustrator as pyl

ax = plt.subplot(111)
ax.plot(
    [1, 2, 3], [1, 2, 3],
    'go-',
    label='line 1',
    linewidth=2
 )

arr_img = pyl.load("stinkbug.svg")

plt.show()

this works for me fine with the current version.