Home »
Python »
Python Programs
How to save image created with 'pandas.DataFrame.plot'?
Learn, how to save image created with 'pandas.DataFrame.plot' in Python?
By Pranit Sharma Last updated : October 06, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
One of the important processes of data analysis is data visualization. Data visualization is a process of representing statistical or categorical data in the form of charts, graphs, or any pictorial format.
Data visualization is an important process as far as data analysis is concerned because it allows us to understand the various kinds of patterns from the data so that we can draw some useful insights from it.
Problem statement
Suppose we are given a Pandas dataframe with multiple columns containing some numerical values and we need to create a plot using dataframe.plot() method and we need to save this plot as an image in our disk storage.
Saving image created with 'pandas.DataFrame.plot'
What is the purpose we will first use the dataframe.plot() method to create a plot and then we will you use savefig() method to save the plot.
Let's understand with the help of an example,
Python program to save image created with 'pandas.DataFrame.plot'
# Importing pandas package
import pandas as pd
# Importing numpy package
import numpy as np
# Creating a dictionary
d = {'A':[10,20,30,40,50],'B':[60,70,80,90,100]}
# Creating a DataFrame
df = pd.DataFrame(d)
# Display Original df
print("Original DataFrame:\n",df,"\n")
# Creating a plot
res = df.plot(kind='bar',figsize=(20, 16), fontsize=26).get_figure()
# Save figure
res.savefig('plot.pdf')
Output
The output of the above program is:
In this example, we have used the following Python topics that you should learn:
Python Pandas Programs »