GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • All
  • Best
  • Libs
  • Learn

This view

  • Original page
  • New address

GeezerVison is showing python-graph-gallery.com — Density chart with Matplotlib

Density chart with Matplotlib

logo of a chart:Density

This post describes how to build a basic density chart with Python and the Matplotlib library. It uses the gaussian_kde() function to compute the density and plot it thanks to the plot() function.

Density sectionAbout this chart

Warning: the seaborn library provides a kdeplot() function allowing to build a density chart much more easily. Check it out here!

From dummy data

Justin Peel suggested this nice solution on stackoverflow to build a density chart from a vector of value with matplotlib.

Density chart with Matplotlib: From dummy data

# Import libraries
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import gaussian_kde

# Build dummy data
data = [1.5]*7 + [2.5]*2 + [3.5]*8 + [4.5]*3 + [5.5]*1 + [6.5]*8

# Build a "density" function based on the dataset
# When you give a value from the X axis to this function, it returns the according value on the Y axis
density = gaussian_kde(data)
density.covariance_factor = lambda : .25
density._compute_covariance()

# Create a vector of 200 values going from 0 to 8:
xs = np.linspace(0, 8, 200)

# Set the figure size
plt.figure(figsize=(14, 8))

# Make the chart
# We're actually building a line chart where x values are set all along the axis and y value are
# the corresponding values from the density function
plt.plot(xs,density(xs))
plt.show()
Dataviz caveat card: Histograms: mind the bin size

⚠️ Avoid the 61 most common dataviz mistakes

The dataviz caveats I see the most, as a deck of 61 cards: a chart that is not ideal, and a better alternative. Free.

I respect your privacy. Unsubscribe anytime.

From a Pandas dataframe

Density chart with Matplotlib: From a Pandas dataframe

# libraries
import matplotlib.pyplot as plt
import pandas as pd

# load dataset from github and convert it to a long format
data = pd.read_csv("https://raw.githubusercontent.com/zonination/perceptions/master/probly.csv")
data = pd.melt(data, var_name='text', value_name='value')

# take only "Almost No Chance", "About Even", "Probable", "Almost Certainly"
data = data.loc[data.text.isin(["We Believe"])]

# Build a "density" function based on the dataset
# When you give a value from the X axis to this function, it returns the according value on the Y axis
density = gaussian_kde(data.value)
density.covariance_factor = lambda : .25
density._compute_covariance()

# Create a vector of 200 values going from 0 to 8:
xs = np.linspace(0, 100, 200)

# Set the figure size
plt.figure(figsize=(14,8))

# plot
plt.fill_between( xs, density(xs), color="#69b3a2", alpha=0.4)

# title
plt.title("How probable something is when someone says 'We believe'", loc='left', fontsize=18)
plt.title("python graph gallery", loc='right', fontsize=13, color='grey', style='italic')

# Axis name
plt.xlabel("probability (%)")

# Remove Y axis
plt.yticks([])

plt.show()

Related posts

Density Chart with Multiple Groups

Density Chart with Multiple Groups

Mirror Density and Histogram Chart

Mirror Density and Histogram Chart

Seaborn Titles

Seaborn Titles

Percent stacked barplot

Percent stacked barplot

Apply a style sheet to Matplotlib

Apply a style sheet to Matplotlib

Distribution plot with quantiles

Distribution plot with quantiles

Related chart types

logo of a chart:Violin

Violin

logo of a chart:Density

Density

logo of a chart:Histogram

Histogram

logo of a chart:Box1

Boxplot

logo of a chart:Joyplot

Ridgeline

logo of a chart:Beeswarm2Big

Beeswarm

300 projects

💡 Out of dataviz inspiration?

I have spent years collecting the most beautiful data visualizations out there. Get the list of my top 300 dataviz projects, hand-picked, for free.

I respect your privacy. Unsubscribe anytime.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.