Python has a module named Seaborn that we may use to create statistical graphics or graphical representations of statistical data. This library is based on the matplotlib library’s principles and is closely linked to Pandas’ data models. When working with large amounts of data, we may want a nice graphical interface to see and analyze the data successfully. In this situation, we will require certain Python libraries, which is where Seaborn plays its role. The Seaborn is a graphical representation of our data, which is recorded as an array, collections, or even a whole DataFrame.
Seaborn allows you to save the outcome in various file formats, including.png,.pdf,.svg,.eps, and more. Let’s look at how to store the resulting graph in a specific file type.
In today’s data visualization article, we will show you how to save a Seaborn plot as a graphic file, which you can then include in a website, presentation, Excel spreadsheet, or another document. We will save it to an external file with the savefig() function. Within the function, we must define the filename, its format, and location.
Example 1: Loading the Sample Data for the Graph in Seaborn in Ubuntu 20.04
Initially, we loaded the Seaborn sample data titanic, which is built-in in Python Seaborn. For this, we have defined variable data and called a Seaborn load_dataset function. Within the load_dataset function, the data sample titanic is passed. We can easily read the titanic CSV file from the Python Seaborn. The print function has the variable data as an input, and the head function is applied to the provided data variable. The head function will return the first five entries inside the titanic dataset.
1 2 3 4 5 | import seaborn as sns data = sns.load_dataset('titanic') print(data.head()) |
The script is saved in the file save.py. When we run this file in the command prompt of Ubuntu. The following dataset is returned, which displays the head entries of the titanic dataset:
Example 2: Saving the Graph in the PNG Format in Seaborn in Ubuntu 20.04
Portable Network Graphic (PNG) is a raster picture file format. It can accommodate visuals with transparency or semi-transparent backgrounds. It’s a widely attractive file type among web designers. Because the PNG file format isn’t patented, anyone can open it with any editing software. We can save the plot in the .png extension in Seaborn Python.
In the example, we have a Seaborn module for plot rendering. Then, loaded the sample data titanic inside the Seaborn load_dataset function. We have created a variable scatterplot that has the Seaborn scatterplot function. The scatterplot function takes the pclass and fare column parameters from the data sample titanic and chooses the hue option sex for this plot. Inside the savefig() function, we have specified the filename and .png extension.
1 2 3 4 5 6 7 8 9 10 11 | import seaborn as sns data = sns.load_dataset('titanic') scatterPlot = sns.scatterplot( x=data['pclass'], y=data['fare'], hue=data['sex']) fig = scatterPlot.get_figure() fig.savefig('plot1.png') |
The plot figure is saved in the plot1.png file as displayed on the following screen:
Example 3: Saving the Graph in the SVG Format in Seaborn in Ubuntu 20.04
As in the previous example, the PNG extension is used to save the Seaborn plot figure. Now, we are using the SVG extension to save the figure of the plot. Vector files hold images using mathematical algorithms based on points and lines on a grid, unlike pixel-based raster formats like JPEGs, which are based on pixels. This implies that vector files, such as SVG, may be scaled up or down without losing quality, making them excellent for logos and intricate online images.
First, we have a dataset titanic, which we have loaded in the Seaborn load_dataset function. This sample dataset is stored inside the variable data. Then, we have another variable, myPlot, inside which we have a scatterPlot method, and, in that method, we have three inputs x, y, and hue. These inputs are set with the different columns of the data sample titanic. After this, we have the get_figure function from the myPlot variable, and through the savefig method, we have given a filename plot2 and .svg extension. The plot is now saved as plot2.svg file.
1 2 3 4 5 6 7 8 9 10 11 | import seaborn as sns data = sns.load_dataset('titanic') MyPlot = sns.scatterplot( x=data['class'], y=data['age'], hue=data['sex']) Plot_fig = MyPlot.get_figure() Plot_fig.savefig('plot2.svg') |
The plot is saved in the directory where your script file is saved. When you open that file plot2.svg, it visualizes the plot that we have rendered in the following figure:
Example 4: Saving the Graph in the PDF Format in Seaborn in Ubuntu 20.04
The PDF (Portable Document Format) enables the display of a variety of documents, including bank statements, presentations, and photographs, among other things. The fact that it’s a universal format means that the content of a PDF will always look the same on any device. Printers love it because it preserves the integrity of all page components and pixel density when widened. This example is quite different from the previous examples. We use the PDF format and then save the file in the directory where we want to save it.
As in the initial step, we have the dataset titanic which we have loaded by calling the load_dataset function of Seaborn and placing it inside the variable data. Then, we have a variable set_plot where the scatterPlot function takes the x, y, and hue arguments. Another variable, Figure, has gotten the figure from the scatterplot and saved it in the new specified directory as plot2.pdf.
1 2 3 4 5 6 7 8 9 10 11 | import seaborn as sns data = sns.load_dataset('titanic') set_plot = sns.scatterplot( x=data['pclass'], y=data['age'], hue=data['survived']) Figure = set_plot.get_figure() Figure.savefig(r'/home/kalsoom/Documents/plot2.pdf') |
When we open the specified directory, the file is saved there as plot2.pdf. Opening the file shows the following plot:
Conclusion
Here, we have concluded the Seaborn save plot article. We can save the plot in Seaborn with different file extensions in the savefig method. We could save the plot as a.jpg,.pdf, or another file type. We have used several file extensions to save the plot figure with the savefig method. Finally, we utilized the savefig approach, which was simple method.