Sam Hammamy
Sam Hammamy

Reputation: 11017

Plotting a probability distribution using matplotlib

I would like to plot the softmax probabilities for a neural network classification task, similar to the plot below

However most of the code I've found on SO and the doc pages for matplotlib are using histograms.

Examples:

plotting histograms whose bar heights sum to 1 in matplotlib

Python: matplotlib - probability mass function as histogram

http://matplotlib.org/gallery.html

But none of them match what I'm trying to achieve in that plot. Code and sample figure are highly appreciated.

example softmax plot

Upvotes: 1

Views: 5918

Answers (1)

alexblae
alexblae

Reputation: 746

I guess you are just looking for a different plot type. Adapted from here:

# Import 
import numpy as np
import matplotlib.pyplot as plt

# Generate random normally distributed data
data=np.random.randn(10000)

# Histogram
heights,bins = np.histogram(data,bins=50)

# Normalize
heights = heights/float(sum(heights))
binMids=bins[:-1]+np.diff(bins)/2.
plt.plot(binMids,heights)

Which produces something like this:

enter image description here

Hope that is what you are looking for.

Upvotes: 2

Related Questions