How to Change Pandas Plot Size?

  • Post author:
  • Post category:Pandas
  • Post last modified:February 2, 2024
  • Reading time:17 mins read

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 the plot 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 the figsize argument to control the size of the resulting plot.
  • For more fine-grained control, employ the matplotlib library alongside Pandas, using functions like plt.subplots to create a figure and axes, then setting the size with figsize.
  • Utilize the matplotlib library in conjunction with Pandas to customize plot size by creating a figure and setting its dimensions with the figsize 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.

pandas plot size

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.

pandas plot size

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))
pandas Adjust plot size

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.

Pandas scatter Adjust plot
Scatter plot using Pandas

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 height as a 4 into figsize param.


# Create scatter plot with figsize
df.plot.scatter(x='x', y='y', figsize=(2, 4,))
Pandas Adjust plot size
Scatter plot with customized figure size

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.

Pandas plot size
Line plot of using Pandas

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))
Pandas plot size
Customized figure size of line bar

Frequently Asked Questions on Change Pandas Plot Size

How can I change the size of a Pandas plot?

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.

Can I set the plot size while creating a Pandas DataFrame plot?

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.

Is there an alternative method to change the plot size in Pandas?

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.

Can I customize the plot size using the Axes object in Pandas?

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.

Are there advanced options for adjusting Pandas plot size?

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 !!

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.

Leave a Reply