Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nettanetta opened this issue Mar 12, 2024 · 3 comments
Closed

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

nettanetta opened this issue Mar 12, 2024 · 3 comments

Comments

@nettanetta
Copy link

nettanetta commented Mar 12, 2024

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
Copy link
Contributor

TyberiusPrime commented Mar 12, 2024

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
Copy link
Owner

has2k1 commented Mar 14, 2024

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
Copy link
Owner

has2k1 commented Apr 25, 2024

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

@has2k1 has2k1 closed this as completed in 18a82c3 Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants