Marsilea-viz / marsilea

Declarative creation of composable visualization for Python (Complex heatmap, Upset plot, Oncoprint and more~)
https://marsilea.rtfd.io/
MIT License
137 stars 5 forks source link

Fontweight of color labels prevents saving as SVG #39

Open afrendeiro opened 1 month ago

afrendeiro commented 1 month ago

When passing label to mp.ColorMesh or mp.Colors and saving as SVG I get an error:

...

File ~/.local/lib/python3.11/site-packages/matplotlib/text.py:797, in Text.draw(self, renderer)
    793             textrenderer.draw_tex(gc, x, y, clean_line,
    794                                   self._fontproperties, angle,
    795                                   mtext=mtext)
    796         else:
--> 797             textrenderer.draw_text(gc, x, y, clean_line,
    798                                    self._fontproperties, angle,
    799                                    ismath=ismath, mtext=mtext)
    801 gc.restore()
    802 renderer.close_group('text')

File ~/.local/lib/python3.11/site-packages/matplotlib/backends/backend_svg.py:1269, in RendererSVG.draw_text(self, gc, x, y, s, prop, angle, ismath, mtext)
   1267     self._draw_text_as_path(gc, x, y, s, prop, angle, ismath, mtext)
   1268 else:
-> 1269     self._draw_text_as_text(gc, x, y, s, prop, angle, ismath, mtext)
   1271 if gc.get_url() is not None:
   1272     self.writer.end('a')

File ~/.local/lib/python3.11/site-packages/matplotlib/backends/backend_svg.py:1112, in RendererSVG._draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext)
   1110 if prop.get_variant() != 'normal':
   1111     font_parts.append(prop.get_variant())
-> 1112 weight = fm.weight_dict[prop.get_weight()]
   1113 if weight != 400:
   1114     font_parts.append(f'{weight}')

KeyError: 600

I think this is because maybe a default is making this label bold (600)? Also tried setting label_props=dict(fontweight=400) or label_props=dict(fontweight='regular') with same effect. Saving as PDF works, but can't be opened by Inkscape for example. Might be only Linux-specific.

Mr-Milk commented 1 month ago

Yes, by default the legendkit will try to use the bold setting for the title in the legend, the exact weight value is decided by matplotlib. May I ask which font are you using?

afrendeiro commented 1 month ago

Arial. These are the settings I use often:

import matplotlib.pyplot as plt
plt.rcParams["svg.fonttype"] = "none"
plt.rcParams["font.family"] = "Arial"
plt.rcParams["font.sans-serif"] = ["Arial"]
plt.rcParams["text.usetex"] = False