has2k1 / plotnine

A Grammar of Graphics for Python
https://plotnine.org
MIT License
3.92k stars 210 forks source link

Can't edit text when saving a plotnine figure to an svg file #756

Closed nettanetta closed 3 months ago

nettanetta commented 4 months ago

Hi, First I want to thank you for helping me avoid writing code in R, Plotnine is a lifesaver package! Lately, I noticed that when I save a plot made with R's ggplot2 as an SVG or PDF file, I can upload it to illustrator and edit the figure text. However, when I do the same with a plot generated using plotnine, illustrator does not recognize the text as text and therefore makes editing my figures a bit tricky.

As an example I created the same figure using ggplot and plotnine (based on this tutorial).

Is it possible to fix it? thanks! Netta

python code:

from plotnine import *
from palmerpenguins import load_penguins

penguins = load_penguins().dropna()
p = ggplot(penguins, aes(x = "island")) + geom_bar()
ggsave(p, filename = "penguins_python.svg")

R code:

library(ggplot2)
library(palmerpenguins)

penguins <- na.omit(palmerpenguins::penguins)
penguins_figure <- ggplot(penguins, aes(x = island)) + geom_bar()
ggsave(penguins_figure, filename = "penguins-figure.svg")

penguins_python penguins_r

TyberiusPrime commented 4 months ago

That's a matplot lib trap / optimization to have the SVG look identical even if the fonts are not on the target machine.:

Use

import matplotlib.pyplot as plt
plt.rcParams['svg.fonttype'] = 'none'

to turn this off.

See https://matplotlib.org/stable/users/explain/customizing.html

has2k1 commented 4 months ago

You can try something like this


from plotnine import *
from palmerpenguins import load_penguins

class use_svgfonts(theme):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._rcParams.update({"svg.fonttype": "none"})

penguins = load_penguins().dropna()
p = ggplot(penguins, aes(x = "island")) + geom_bar() + use_svgfonts()
p.save(filename = "penguins_python.svg")
has2k1 commented 3 months ago

With the next release you will be able to use theme(svg_usefonts=True).