The figsize
parameter of the plot()
function is used to adjust or change the figure/picture size of a plot in pandas. This param contains two values one to specify width
of the plot and the second to specify height
of the plot. We can modify the plot by passing the required dimensions as a tuple to the figsize
parameter, the dimensions must be in inches.
In this article, I will explain how to change/adjust the size of the plot using the figsize
parameter of the plot()
, plot.bar(), plot.scatter() and other plotting functions.
Key Points –
- Use the
figsize
parameter in theplot
function of Pandas to specify the width and height of the plot in inches. - Adjust the size of the plot by setting the
figsize
parameter when creating a Pandas DataFrame plot. - Alternatively, use the
plot
method and pass thefigsize
argument to control the size of the resulting plot. - For more fine-grained control, employ the
matplotlib
library alongside Pandas, using functions likeplt.subplots
to create a figure and axes, then setting the size withfigsize
. - Utilize the
matplotlib
library in conjunction with Pandas to customize plot size by creating a figure and setting its dimensions with thefigsize
attribute.
1. Quick Examples of Changing Size of Pandas Plot
Following are quick examples of how to change the size of plot chart in pandas.
Below are the quick examples
# Example 1: Create line plot with figsize
df.plot(x='x', y='y', figsize(4, 2))
# Example 2: Adjust the size of a plot bar
df.plot.bar(figsize = (2, 4))
# Example 3: Adjust the size of a single column plot bar
df['x'].plot.bar(figsize = (6, 3))
# Example 4: Create scatter plot with figsize
df.plot.scatter(x='x', y='y', figsize=(2, 4,))
2. Syntax to Change Size of Plot Chart
Following is the syntax of plot()
and figsize
parameter.
# Syntax of plot & figsize
df.plot(figsize = (width, height))
# Using plot.bar()
df.plot.bar(figsize = (width, height))
3. Change or Adjust Plot size in Pandas
Python Pandas library is mainly focused on data analysis and it is not only a data processing library but also using this we can create a basic plot for visualization. When we want to create exploratory data analysis plots, we can use Pandas. It provides several different functions for visualizing our data with the help of the plot()
function.
Using the Pandas plot()
function we can visualize the given Data in a default size and this method provides a parameter to change the size of the chart
Now let’s create pandas DataFrame.
# Create DataFrame
import pandas as pd
df = pd.DataFrame({'x': [5, 10, 15, 20, 25, 30, 35],
'y': [5, 10, 15, 20, 25, 30, 35]})
print(df)
Yields below output.
# Output:
x y
0 5 5
1 10 10
2 15 15
3 20 20
4 25 25
5 30 30
6 35 35
4. Change or Adjust Bar Plot size in Pandas
We can create a bar graph using a plot.bar(). A bar plot is a plot in which, the categorical data with rectangular bars with lengths proportional to the values that they represent. For example,
# Draw a plot bar chart
df.plot.bar()
Yields below output.
Now we can customize the figure size of pandas plot bar using figsize param.
# Adjust the size of a plot bar
df.plot.bar(figsize = (2, 4))
Yields below output.
We can also create a single column plot bar using a plot.bar() function and modify the figure size of the plot bar. Let’s modify.
# Adjust the size of a single column plot bar
df['x'].plot.bar(figsize = (6, 3))
5. Change or Adjust Scatter Plot size in Pandas
To create a scatter plot in pandas use plot.scatter() function, it will return the default figure size of the scatter plot. Let’s create a scatter plot using data from the DataFrame.
# Create scatter plot
df.plot.scatter(x='x', y='y')
Yields below output.
5.1 Use the figsize Param and Change Scatter Plot Size
Use figsize
param we can adjust the size of the plot. For, that we need to pass the figsize param along with x, y coordinates into plot.scatter() function, it will make our visualization more convenience. For example, I have passed width
as a 2
and heigh
t as a 4
into figsize
param.
# Create scatter plot with figsize
df.plot.scatter(x='x', y='y', figsize=(2, 4,))
6. Adjust Size of Line Plot
line plot bar is the default plot of the plot() function. Using this function we will plot the line plot of the given DataFrame. Let’s create a line plot of the given DataFrame.
# Create line plot with figsize
df.plot(x='x', y='y')
Yields below output.
6.1 Use the FigSize Param and Adjust line Plot Size
Pass the figsize param with width and height into the plot() function, it will return the customized size of the line plot.
# Create line plot with figsize
df.plot(x='x', y='y', figsize(4, 2))
Frequently Asked Questions on Change Pandas Plot Size
You can change the size of a Pandas plot by using the figsize
parameter within the plot
function. This parameter allows you to specify the width and height of the plot in inches.
You can set the plot size while creating a Pandas DataFrame plot by including the figsize
parameter in the plot
method. This provides a convenient way to adjust the size of the plot at the time of creation.
Another approach is to use the matplotlib
library alongside Pandas. By creating a figure and setting its dimensions with the figsize
attribute, you have more fine-grained control over the plot size.
For greater control, you can use the ax
parameter in the plot
function and set the figsize
attribute of the resulting Axes object. This allows you to tailor the size of the plot according to your specific requirements.
For more flexibility, consider combining Pandas with matplotlib
functions like plt.subplots
. This enables you to create a figure and axes separately, allowing precise control over the size of the plot by setting the figsize
parameter.
Conclusion
In this article, I have explained how to adjust the size of the pandas plot using the figsize
param of plot()
and plot.bar()
function and also explained how we can change the size of different plots using the figsize param with examples.
Happy Learning !!
Related Articles
- How to Plot the Boxplot from DataFrame?
- How to Plot a Scatter Plot Using Pandas?
- Create Pandas Plot Bar Explained with Examples
- How to Generate Time Series plots in Pandas?
- How to Plot the Boxplot from DataFrame?
- How to Generate Time Series Plot in Pandas?
- Create Pandas Plot Bar Explained with Example
- How to add title to Pandas plots?
- How to generate line plot in Pandas?
- How to add legends to plots in Pandas
- How to Plot Columns of Pandas DataFrame
- How to generate histograms in Pandas?