Location Research Breakthrough Possible @S-Logix pro@slogix.in

How to Plot Bubble Chart Using Matplotlib, Numpy, and Pandas

Bubble Chart Using Matplotlib, Numpy, and Pandas

Condition for Plotting Bubble Chart Using Matplotlib, Numpy, and Pandas

  • Description:
    A bubble chart is a data visualization that displays three dimensions of data: two on the X and Y axes, and the third dimension represented by the size of the bubbles. It is useful for visualizing relationships between continuous variables and highlighting patterns or outliers.
Step-by-Step Process
  • Import Libraries:
    Import necessary libraries such as matplotlib, numpy, and pandas for data manipulation and plotting.
  • Prepare Data:
    Organize your data, ensuring that you have at least three variables to represent on the X, Y, and bubble size axes.
  • Create the Plot:
    Use the scatter() function in matplotlib to create a scatter plot with an additional dimension for the bubble size.
  • Customize the Plot:
    Adjust the color, transparency (alpha), labels, and titles.
Sample Source Code
  • # Code for Bubble Chart

    import matplotlib.pyplot as plt

    import pandas as pd

    data = {
    'Product': ['Product A', 'Product B', 'Product C', 'Product D', 'Product E'],
    'Price': [10, 15, 12, 20, 25],
    'Units Sold': [200, 150, 300, 100, 250],
    'Profit': [500, 700, 1200, 300, 800]
    }

    df = pd.DataFrame(data)

    plt.figure(figsize=(10, 6))

    # Create the bubble chart
    # X-axis: Price, Y-axis: Units Sold, Size of bubbles: Profit
    plt.scatter(df['Price'], df['Units Sold'], s=df['Profit'], alpha=0.5, c=df['Profit'], cmap='viridis', edgecolors="w", linewidth=0.5)

    for i in range(len(df)):
    plt.text(df['Price'][i], df['Units Sold'][i], df['Product'][i], fontsize=12, ha='center')

    plt.title('Bubble Chart: Product Price vs Units Sold vs Profit', fontsize=14)

    plt.xlabel('Price', fontsize=12)
    plt.ylabel('Units Sold', fontsize=12)

    plt.show()
Screenshots
  • Bubble Chart Output