whitead/skunk

plot svgs into matplotlib subplots in a for loop

Closed this issue · 4 comments

Hi,

Many thanks for this easy and straightforward tool. I have a dict with lxml text in it, and I want to plot each key into a subplot. How would you do that using skunk?.. I would be very grateful if you can provide a demo code.

I played a little with your code, didn't manage to make what I am after, but I saw that I had to reset the dimensions of the svg image before adding it to a subplot.

Many thanks in advance,
HM

Hi @hmassalha,

It seems like your question is like This Example from the README. Are you having trouble with constructing the for loop or setting up the subplots? I can help with this, but will need to know a bit more like how many subplots and how you want them to be laid out.

Hi @whitead,

Thanks for your reply. I have a svg file that I made using Inkscape. My code loads the svg file and adds colors and change size to some key elements in the svg file based on some calculations, and all I want is to see the results. The best way I though about is plotting the edited svgs as subplots using matplotlib. The grid size is not that important, say 3x3. The main idea is to see all outputs at ones. As I mentioned in my previous msg, the edited svg variable is dict with lxml.

Many thanks for your help,
HM

Is this kind of like what you're looking for?

import skunk
import numpy as np
import os
import matplotlib.pyplot as plt

fig, axs = plt.subplots(ncols=2, nrows=2)

my_svg_dict = {
    'sk00': 'skunk.svg',
    'sk01': 'skunk.svg',
    'sk10': 'skunk.svg',
    'sk11': 'skunk.svg'
}

for i in range(2):
    for j in range(2):
        x = np.linspace(0, 2 * np.pi)
        axs[i, j].plot(x, np.sin(x))
        # important line where we set ID
        skunk.connect(axs[i, j], f'{sk}{i}{j}'))

plt.tight_layout()

# Overwrite using file path to my svg or a string that contains the SVG 
svg = skunk.insert(my_svg_dict)

# write to file
with open('replaced.svg', 'w') as f:
    f.write(svg)
# or in jupyter notebook
skunk.display(svg)

Many thanks, working perfect.
Please note you have few typos in the above code

Many thanks for the amazing tool 🚀