Can't edit text when saving a plotnine figure to an svg file
nettanetta opened this issue · comments
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")
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
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")
With the next release you will be able to use theme(svg_usefonts=True)
.