Monday, January 20, 2020

Erik Marsja: How to Save a Seaborn Plot as a File (e.g., PNG, PDF, EPS, TIFF)

The post How to Save a Seaborn Plot as a File (e.g., PNG, PDF, EPS, TIFF) appeared first on Erik Marsja.

In this short post, we will learn how to save Seaborn plots to a range of different file formats. More specifically, we will learn how to use the plt.savefig method save plots made with Seaborn to:

  1. Portable Network Graphics (PNG)
  2. Portable Document Format (PDF)
  3. Encapsulated Postscript (EPS)
  4. Tagged Image File Format (TIFF)
  5. Scalable Vector Graphics (SVG)

First, we will create some basic plots and work with Matplotlibs savefig method to export the files to the different file formats. There is more information on data visualization in Python using Seaborn and Matplotlib in the following posts:

Prerequisites: Python and Seaborn

Now, before learning how to save Seaborn plots (e.g., to .png files), we need to have both Python and Seaborn installed. There are two easy methods to install Seaborn. First, if we don’t have Python installed we can download and install a Python distribution packed with Seaborn (e.g., Anaconda). Second, if we already have Python installed we can install Seaborn using Pip. Of course, there are a number of mandatory dependencies (i.e., NumPy, SciPy, Matplotlib, & Pandas) but pip will installed them too. At times, we may need to update Seaborn and we will after we have installed Seaborn, learn how to update Seaborn to the latest version.

How to Install Seaborn with Pip

Now we’re ready to use pip to install Seaborn. It’s very simple. First, we open up a terminal window, or Windows command prompt, and type pip -m install seaborn.

How to Upgrade Seaborn using Pip and Conda

In this section, before creating and saving a Seaborn plot we will learn how to upgrade Seaborn using pip and conda. First, if we want to upgrade Seaborn with pip we just type the following code: pip install -upgrade seaborn.

If we, on the other hand, have the Anaconda Python distribution installed we will use conda to update Seaborn. Now, this is also very easy and we will open up a terminal window (or the Anaconda Prompt, if we use Windows) and type conda update seaborn.

Learn more about installing, using, and upgrading Python packages using pip, pipx, and conda in the following two posts:

When we are installing and upgrading packages, we may notice, that we also need to upgrade pip.

Using the plt.savefig Method

In this section, before we start saving a Seaborn plot as a file, we are going to learn a bit more about the plt.savefig method.

Saving Seaborn plots to files using the plt.savefig methodThe savefig method

As can be seen in the image above, we have a number of arguments to work with.

In this post, we are going to work with some of them when saving Seaborn plots as a file (e.g., PDF). Specifically, we will use the fname, dpi, format, orientation, and transparent. Now, orientation can only be used in the format is postscript (e.g., eps). Thus, we will only use it when we are saving a Seaborn plot as a .eps file.

Concerning the other arguments, they will work with other formats as well but we will only use them when we save a Seaborn plot as a png file.

How to Save a Seaborn Plot as a File (i.e., png, eps, svg, pdf)

In this section, we are finally going to learn how to save a Seaborn plot. Now, in all the examples of saving Seaborn plots here we will start by creating a plot. First, we need to import Seaborn, matplotlib.pyplot, and Pandas. Here, we are following convention and import seaborn as sns, matplotlib.pyplot as plt, and pandas as pd. Note, we need to do this in all our Python scripts in which we are visualizing data and saving the plots to files.

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

Example Data

Next, we are going to import data to visualize. Here, we are using Pandas and the pd.read_csv method to load data from a CSV file.

data = 'https://vincentarelbundock.github.io/Rdatasets/csv/datasets/mtcars.csv'
df = pd.read_csv(data, index_col=0)
df.head(6)

How to Save a Seaborn Plot as png

In the first example, we are going to export the Seaborn plot as Portable Network Graphics (png). First, we need to create our plot and we are going to create a simple histogram using sns.distplot.

In the second line of code, we are using plt.savefig with only the fname argument. Note, we are saving the file as a png only by using a string as fname. That is, we add the file ending .png to tell plt.savefig that we want the Seaborn plot saved as a png.

sns.distplot(df['mpg'])
plt.savefig('save_as_a_png.png')
Saving a Seaborn Histogram Plot as Png FileSaved PNG file (larger version)

Saving a High-Resolution PNG

Now, we will also use the dpi argument here. If we, for instance, wanted to save the histogram plot as a high-resolution image file. Most commonly is, however, that when we need a high-resolution image, we also want a different format. Anyway, if we want to save the histogram with 300 dpi, this is how we do it:

sns.distplot(df['mpg'])
plt.savefig('saving-a-high-resolution-seaborn-plot.png', dpi=300)

How to Save a Transparent PNG

In this example, we are going to save the Seaborn plot as a transparent png. That is, we will use the transparent argument and set it to true.

sns.distplot(df['mpg'])
plt.savefig('saving-a-seaborn-plot-as-png-file-transparent.png', 
           transparent=True)
How to Export a Seaborn Plot as PNGTransparent PNG

Save a Seaborn Figure as a PDF File

In this section, we are going to learn how to save a Seaborn plot as a .pdf file. As when we learned how to save a histogram figure as a png, we first need to make a plot. Here, we are going to create a scatter plot using the scatterplot method from Seaborn.

sns.scatterplot(x='wt', y='drat', data=df)
plt.savefig('saving-a-seaborn-plot-as-pdf-file.pdf')
How to Save a Seaborn Plot as a PDF filePDF file

Saving a Seaborn Plot as a High-Resolution PDF file

In this section, we are going to use the dpi argument again. Many scientific journals requires image files to be in high-resolution images. For example, the PLOS journals (e.g., Plos One) requires figures to be in 300 dpi. Here’s how to save a Seaborn plot as a PDF with 300 dpi:

<pre><code class="lang-py">sns.scatterplot(x='wt', y='drat', data=df)
plt.savefig('saving-a-seaborn-plot-as-pdf-file-300dpi.pdf', 
           dpi=300)</code></pre>

How to Save Python Data Visualizations (e.g., Seaborn plots) to an EPS file

In this section, we will carry on and learn how to save a Seaborn plot as an Encapsulated Postscript file. First, we will change the file ending (the fname argument) to .eps to export the plot as an EPS file. Second, we will learn how to save the Seaborn plot as a high-resolution .eps file.

In this example, we are going to create a violin plot using Seaborn’s catplot method and save it as a file:

sns.catplot(x='cyl', y='drat', hue='am',
data=df, kind='violin')
plt.savefig('saving-a-seaborn-plot-as-eps-file.eps')
Saving a seaborn plot as a eps fileViolin plot to save to eps

Saving a Seaborn Plot as a High-resolution EPS file

Now, we are already familiar with how to set the dpi but in the next example we are going to save the Seaborn violin plot as a high-resolution image file:

sns.catplot(x='cyl', y='drat', hue='am',
                data=df, kind='violin')
plt.savefig('saving-a-seaborn-plot-as-eps-file-300dpi.eps',
           dpi=300)

Saving plots as EPS, and also TIFF (as we will learn about later) is also formats that are required, or recommended (e.g., see the American Psychological Associations recommendations in ther submission guidelines).

Saving the Seaborn Plot in Landscape (EPS)

Now, in the final saving a plot to EPS example, we are going to use the orientation argument to save the Seaborn plot in landscape.

sns.catplot(x='cyl', y='drat', hue='am',
                data=df, kind='violin')
plt.savefig('saving-a-seaborn-plot-as-eps-file-300dpi.eps', orientation="landscape",
           dpi=300)
How to Save a Seaborn plot as a fileSeaborn plot saved in landscape orientation

How to Save Seaborn plots to TIFF files

In this section, we will use the final format to save the Seaborn plots. More specifically, we will save the plots as in the Tagged Image File Format (TIFF). Now, as with the other formats, we will change the file ending (the fname argument) to .tiff. This, will as we now know, save the plot as a TIFF file.

Learn how to save a swarm plot, created in seaborn, to a TIFF file.Swarm plot to save as TIFF

In the how to save to a TIFF file example, we are going to continue working with the catplot method but now we are going to create a swarm plot and save it as a TIFF file.

sns.catplot(x='vs', y='wt', hue='am',
                data=df, kind='swarm')
plt.savefig('saving-a-seaborn-plot-as-eps-file.tiff')

Saving a (Swarm) Plot as a High-Resolution TIFF File

In the final, how to save a TIFF file example, we are going to save the Seaborn plot as a high-resolution TIFF file. Why? Well, as previously mentioned a lot of scientific journals require, or recommend, that we use the PDF, EPS, or TIFF format when we submit our scientific papers.

Now, here’s how to explort the Seaborn plot as a TIFF file with high-resolution:

<pre><code class="lang-py">sns.catplot(x='vs', y='wt', hue='am',
                data=df, kind='swarm')
plt.savefig('saving-a-seaborn-plot-as-eps-file.tiff',
           dpi=300)</code></pre>

How to Save a Plot in Python Seaborn as a SVG File

In the final example, we are going to save a Seaborn plot as a SVG file. Now, we already know that we just change the file ending to accomplish this:

sns.catplot(x='vs', y='wt', hue='am',
                data=df, kind='swarm')
plt.savefig('saving-a-seaborn-plot-as-eps-file.svg')

Note, if we want to save the SVG file as a high-resolution SVG file, we’d just add the dpi=300 as we’ve learned in the previous examples.

There are, of course, a couple of other arguments that could’ve been used when working with plt.savefig. However, when working with Seaborn these are, most often, redundant. The formatting is already taken care of and we get quite nice-looking image files of our Seaborn plots.

Conclusion: Saving Plots in Python to Files

In this Python data visualization tutorial, we have learned how to save Python plots (made in Seaborn) as PNG, PDF, EPS, TIFF, and SVG files. It was quite simple, and we simply used the savefig method.

The post How to Save a Seaborn Plot as a File (e.g., PNG, PDF, EPS, TIFF) appeared first on Erik Marsja.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...