Built with Tutify.io

    AIStudyLabs

    How to Change Pandas Plot Size?

    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.

    Key Points –

    <a id="h-1-quick-examples-of-changing-size-of-pandas-plot"></a>

    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,))
    

    <a id="h-2-syntax-to-change-size-of-plot-chart"></a>

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

    <a id="h-3-change-or-adjust-plot-size-in-pandas"></a>

    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
    

    <a id="h-4-change-or-adjust-bar-plot-size-in-pandas"></a>

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

    <a id="h-5-change-or-adjust-scatter-plot-size-in-pandas"></a>

    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.

    <a id="h-5-1-use-the-figsize-param-and-change-scatter-plot-size"></a>

    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,))
    

    <a id="h-6-adjust-size-of-line-plot"></a>

    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.

    <a id="h-6-1-use-the-figsize-param-and-adjust-line-plot-size"></a>

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

    <a id="h-frequently-asked-questions-on-change-pandas-plot-size"></a>

    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.

    <a id="h-conclusion"></a>

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

    <a id="h-related-articles"></a>

    Related Articles