Built with Tutify.io

    AIStudyLabs

    How to Plot a Scatter Plot Using Pandas?

    How to Plot a Scatter Plot Using Pandas?

    Pandas DataFrame plot.scatter() is used to create a scatter plot by using dots to represent values of two different numeric variables. A Scatter plot is a type of data visualization technique that shows the relationship between two numerical variables. Each dot on a scatter plot represents an individual data point. In this article, I will explain the concept of scatter plots by using the plot() function and creating scatter plot from the DataFrame.

    Key Points –

    <a id="h-quick-examples-of-scatter-plot"></a>

    Quick Examples of Scatter Plot

    If you are in a hurry below are some quick examples of how to create scatter plot chart.

    
    # Quick examples of scatter plot
    
    # Example 1: Create scatter plot
    df.plot.scatter(x='x', y='y')
    
    # Example 2: organize the scatter plot
    df.plot.scatter(x='x', y='y', s = 100, c='purple')
    
    # Example 3: create scatterplot
    plot.scatter(df.x, df.y)
    
    # Example 4: Customize the scatter plot
    plot.scatter(df.x, df.y, s=60, c='purple')
    

    <a id="h-syntax-of-pandas-plot-scatter"></a>

    Syntax of Pandas plot.scatter()

    
    # Syntax of plot.scatter()
    DataFrame.plot.scatter(x, y, s = none, c = none)
    

    <a id="h-parameters-of-the-plot-scatter"></a>

    Parameters of the plot.scatter()

    Below are the parameters of the scatter() function.

    <a id="h-return-value"></a>

    Return Value

    It returns a scatter plot.

    <a id="h-create-scatter-plot-from-pandas-dataframe"></a>

    Create Scatter Plot from Pandas DataFrame

    In Pandas Scatter plot is one of the visualization techniques to represent the data from a DataFrame. We can use the plot.scatter() function to create a simple scatterplot. We can also create scatter plot from plot() function and this can also be used to create bar graph, plot box, histogram and plot bar in Pandas.

    Let's create Pandas DataFrame from Python Dicttionary.

    
    import pandas as pd
    # Create DataFrame
    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
    

    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-customize-the-scatter-plot"></a>

    Customize the Scatter Plot

    We can customize the scatter plot using the 's' and 'c' arguments to modify the size and color of the points, respectively. Use param c to specify the color of the dot.

    
    # Organize the scatter plot
    df.plot.scatter(x='x', y='y', s = 100, c='purple')
    

    Yields below output.

    <a id="h-use-matplotlib-to-create-scatter-plot"></a>

    Use Matplotlib to Create Scatter Plot

    Matplotlib is another most used library in Python that is used to visualize the data in a charts. It provides the scatter() function to create the scatter plots. Use the pyplot.scatter() function to create a scatter plot, in order to use it you have to import is by using import matplotlib.pyplot.

    
    import matplotlib.pyplot as plot
    # Create scatterplot
    plot.scatter(df.x, df.y)
    

    Yields below output.

    Customize the scatter plot by modifying the s and c parameters with desired values using the plot.scatter() function. Let's customize.

    
    # Customize the scatter plot
    plot.scatter(df.x, df.y, s=60, c='purple')
    

    Yields below output.

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

    Conclusion

    In this article, I have explained the concept of scatter plot and using the scatter() function how we can plot the given DataFrame into a scatter plot. I also explained how to customize the scatter plot dots color and size with desired values and finally learned how to use the Matplotlib library to create a scatter plot.

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

    Related Articles

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

    References