Python - Density Plots with Pandas for a specific attribute

A density plot shows the probability density function of a continuous variable. In Pandas, you can create density plots using the plot.density() method to visualize the distribution of numerical data.

What is a Density Plot?

A density plot is a smoothed version of a histogram that shows the distribution of values in a dataset. It's useful for understanding the shape, central tendency, and spread of your data.

Creating Sample Data

Let's create a sample dataset with age information to demonstrate density plotting ?

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Create sample data
np.random.seed(42)
ages = np.random.normal(28, 5, 100)  # Normal distribution with mean=28, std=5
names = [f'Player_{i}' for i in range(1, 101)]

# Create DataFrame
df = pd.DataFrame({
    'Name': names,
    'Age': ages
})

print(df.head())
      Name        Age
0  Player_1  30.967143
1  Player_2  26.765694
2  Player_3  32.173956
3  Player_4  33.225435
4  Player_5  28.972656

Basic Density Plot

Create a simple density plot for the Age attribute ?

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Create sample data
np.random.seed(42)
ages = np.random.normal(28, 5, 100)
df = pd.DataFrame({'Age': ages})

# Create density plot
df.Age.plot.density(color='green')
plt.title('Age Distribution - Density Plot')
plt.xlabel('Age')
plt.ylabel('Density')
plt.show()

Customizing Density Plots

You can customize the appearance with different colors, line styles, and fill options ?

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Create sample data
np.random.seed(42)
df = pd.DataFrame({
    'Age': np.random.normal(28, 5, 100),
    'Score': np.random.normal(75, 15, 100)
})

# Create customized density plot
plt.figure(figsize=(10, 6))

# Plot Age density
df.Age.plot.density(color='blue', alpha=0.7, linewidth=2, label='Age')

# Plot Score density  
df.Score.plot.density(color='red', alpha=0.7, linewidth=2, label='Score')

plt.title('Density Plots Comparison')
plt.xlabel('Values')
plt.ylabel('Density')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

Density Plot with Fill

Create a filled density plot for better visualization ?

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Create sample data
np.random.seed(42)
ages = np.random.normal(28, 5, 100)
df = pd.DataFrame({'Age': ages})

# Create filled density plot
ax = df.Age.plot.density(color='green', alpha=0.6, figsize=(8, 5))
ax.fill_between(ax.lines[0].get_xdata(), ax.lines[0].get_ydata(), alpha=0.3, color='green')

plt.title('Age Distribution - Filled Density Plot')
plt.xlabel('Age')
plt.ylabel('Density')
plt.grid(True, alpha=0.3)
plt.show()

Parameters

Parameter Description Example
color Line color 'green', 'red', '#FF5733'
alpha Transparency (0-1) 0.7
linewidth Line thickness 2
figsize Figure size (width, height) (10, 6)

Conclusion

Density plots are excellent for visualizing the distribution of continuous variables in your dataset. Use plot.density() to create smooth distribution curves and customize them with colors, transparency, and other styling options for better data visualization.

Updated on: 2026-03-26T13:46:07+05:30

653 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements