Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Make a Bell Curve in Python?
A bell curve (normal distribution) is a fundamental concept in statistics that appears when we plot many random observations. Python's Plotly library provides excellent tools for creating these visualizations. This article demonstrates three practical methods to create bell curves using different datasets.
Understanding Bell Curves
The normal distribution emerges naturally when averaging many observations. For example, rolling two dice and summing their values creates a bell-shaped pattern ? the sum of 7 occurs most frequently, while extreme values (2 or 12) are rare.
Example 1: Bell Curve from Dice Roll Simulation
Let's simulate 2000 dice rolls and create a bell curve from their sums:
import plotly.figure_factory as ff
import random
# Generate dice roll data
dice_sums = []
for i in range(2000):
dice_one = random.randint(1, 6)
dice_two = random.randint(1, 6)
dice_sums.append(dice_one + dice_two)
print(f"Sample data: {dice_sums[:10]}")
# Create bell curve
bell_curve = ff.create_distplot([dice_sums], ["Dice Sum"], show_hist=False)
bell_curve.show()
Sample data: [8, 6, 9, 7, 5, 11, 8, 9, 6, 7]
This creates a smooth bell curve showing the normal distribution of dice sum values, with 7 being the most common result.
Example 2: Bell Curve from Height Data
Using real-world data often produces better bell curves. Here's how to visualize height distribution:
import plotly.figure_factory as ff
import pandas as pd
import numpy as np
# Generate sample height data (since we can't access external files)
np.random.seed(42)
heights = np.random.normal(68, 4, 1000) # Mean: 68 inches, Std: 4
# Create DataFrame
df = pd.DataFrame({'Height': heights})
print(f"Height statistics:\nMean: {df['Height'].mean():.2f}")
print(f"Standard deviation: {df['Height'].std():.2f}")
# Create bell curve
height_curve = ff.create_distplot([df['Height'].tolist()], ["Height (inches)"], show_hist=False)
height_curve.show()
Height statistics: Mean: 67.84 Standard deviation: 4.05
Example 3: Bell Curve from Weight Data
Similarly, we can create a bell curve for weight distribution:
import plotly.figure_factory as ff
import pandas as pd
import numpy as np
# Generate sample weight data
np.random.seed(42)
weights = np.random.normal(150, 25, 1000) # Mean: 150 pounds, Std: 25
# Create DataFrame
df = pd.DataFrame({'Weight': weights})
print(f"Weight statistics:\nMean: {df['Weight'].mean():.2f} pounds")
print(f"Standard deviation: {df['Weight'].std():.2f} pounds")
# Create bell curve
weight_curve = ff.create_distplot([df['Weight'].tolist()], ["Weight (pounds)"], show_hist=False)
weight_curve.show()
Weight statistics: Mean: 149.67 pounds Standard deviation: 25.05 pounds
Customizing Bell Curves
You can customize the appearance of your bell curves:
import plotly.figure_factory as ff
import numpy as np
# Generate sample data
np.random.seed(42)
data = np.random.normal(100, 15, 1000)
# Create customized bell curve
fig = ff.create_distplot(
[data],
["Sample Data"],
show_hist=False,
colors=['red']
)
# Update layout
fig.update_layout(
title="Custom Bell Curve",
xaxis_title="Values",
yaxis_title="Density"
)
fig.show()
Key Parameters
| Parameter | Description | Example |
|---|---|---|
show_hist |
Show/hide histogram bars |
False for smooth curve |
colors |
Customize curve colors | ['blue', 'red'] |
bin_size |
Histogram bin width | 0.2 |
Conclusion
Plotly's create_distplot() function makes it easy to create bell curves from any dataset. Use simulated data for demonstrations or real-world data for meaningful analysis. The key is having enough data points (1000+) to see the characteristic bell shape clearly.
