Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving high-resolution images with plotnine

I'm trying to use plotnine to save a high-resolution png image.

With a test dataset, this looks like:

from plotnine import *
import pandas as pd
import numpy as np

df = pd.DataFrame()
df['x'] = np.arange(0,10,0.01)
df['y'] = np.sin(df['x'])

p = ggplot(df, aes(x='x',y='y')) + labs(x='x', y='y') + geom_point(size=0.1)
p.save(filename = 'test3.png', height=5, width=5, units = 'in', dpi=1000)

This produces a low-resolution .png file containing my plot, which is not improved when I increase the specified dpi.

I've also tried saving with:

ggsave(plot=p, filename='test.png', dpi=1000)

and replacing dpi=1000 with res=1000. This produces identical low-resolution png files.

How can I save my plot at the resolution I want?

Edit: This bug is resolved in plotnine version 0.3.0. and the above code works correctly.

like image 644
Blizzard Avatar asked Jan 18 '18 23:01

Blizzard


People also ask

How do I save an image in high resolution in Python?

MatPlotLib with Python To get a high-quality image, we can use . eps image format. You can increase the dot per inch value, i.e., dpi. Using savefig() method, we can save the image locally.

How do I save a high resolution plot in Python?

To save the file in pdf format, use savefig() method where the image name is myImagePDF. pdf, format="pdf". We can set the dpi value to get a high-quality image. Using the saving() method, we can save the image with format=”png” and dpi=1200.

What is GG plot in Python?

Using ggplot in Python allows you to build visualizations incrementally, first focusing on your data and then adding and tuning components to improve its graphical representation. In the next section, you'll learn how to use colors and how to export your visualizations.

How do I add a title to Plotnine?

To add a title, we include the option ggtitle and include the name of the graph as a string argument.


1 Answers

Since this still isn't answered and I just got directed here, too...

According to @has2k1 (the author of plotnine), this was a bug and is now resolved. This commit looks like it might be the referenced fix.

To solve the issue, ensure you are using the git version or at least version 0.3.0.

like image 124
Hendy Avatar answered Oct 20 '22 12:10

Hendy