How to Change Pandas Plot Size?

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.

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

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

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply

You are currently viewing How to Change Pandas Plot Size?