Menu

Exploring Data with Scatter Plots and Histograms

Exploring Data with Scatter Plots and Histograms

Scatter plots and histograms help investigate relationships and distributions. Plotly adds interactive features that make these analyses easier to explore.

Creating an Interactive Scatter Plot

A scatter plot displays the relationship between two numerical variables.

import pandas as pd

import plotly.express as px

df = pd.DataFrame({

"Advertising_Spend": [1000, 1500, 2000, 2500, 3000, 3500],

"Sales": [8000, 10000, 13000, 15000, 18000, 19000],

"Region": ["North", "South", "North", "East", "South", "East"]

})

fig = px.scatter(

df,

x="Advertising_Spend",

y="Sales",

color="Region",

title="Advertising Spend vs Sales"

)

fig.show()

The chart can help reveal whether higher advertising expenditure is associated with higher sales and whether the relationship differs across regions.

An observed relationship does not establish causation. Other factors, such as seasonality or product demand, may influence sales.

Creating a Histogram

Histograms group numerical values into intervals to show their distribution.

df = pd.DataFrame({

"Delivery_Time": [20, 25, 30, 22, 35, 40, 28, 32, 45, 38, 27, 31]

})

fig = px.histogram(

df,

x="Delivery_Time",

nbins=6,

title="Distribution of Delivery Times",

labels={"Delivery_Time": "Delivery Time (Minutes)"}

)

fig.show()

The nbins parameter controls the approximate number of bins. Changing the bin count can change how the distribution appears, so choose a setting that makes the pattern understandable without hiding important details.

Using Bubble Charts

A bubble chart adds a third numerical dimension through marker size.

df = pd.DataFrame({

"Advertising": [1000, 2000, 3000, 4000],

"Sales": [8000, 12000, 18000, 22000],

"Customers": [50, 80, 120, 160]

})

fig = px.scatter(

df,

x="Advertising",

y="Sales",

size="Customers",

title="Advertising, Sales, and Customer Count"

)

fig.show()

Here, horizontal position represents advertising expenditure, vertical position represents sales, and bubble size represents customer count.

Use bubble sizes carefully. Large differences in marker size can make smaller observations difficult to see.


Python for Data Science

VA

Vishalini A



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details