BobelLegend
BobelLegend

Reputation: 89

Python: how to plot the normal distribution of a 3-dimensional array

I have a 3-dimensional array called temprSubset that I have taken the average of across 2 dimensions.

code:

f=MFDataset(filenames)

temprSubset = f.variables['tc'][ : , latitude_lower_limit:latitude_upper_limit , longitude_lower_limit:longitude_upper_limit,] 

tempavg1=temprSubset.mean(axis=tuple(range(0,2)))

I want to plot the standard deviation curve for each average in tempavg1 array but I am lost.

Upvotes: 0

Views: 1228

Answers (1)

xdze2
xdze2

Reputation: 4151

The easy way is to use the hist function. The choice of the number of bins could significantly change the shape of the graph.

Another method, which gives a smooth curve, is the kernel density estimation. The choice of the bandwidth could also change the shape of the obtained graph.

import numpy as np

import matplotlib.pyplot as plt
%matplotlib inline

# Generate some data
data = np.random.normal( size=(5, 50, 150) )  # a random 3D array
average_axes01 = data.mean(axis=(0, 1))

# Using the Kernel density estimation:
from scipy.stats import gaussian_kde

prob_density = gaussian_kde(average_axes01)
std = average_axes01.std()
x_fine = np.linspace(-3*std, 3*std, 29)
probs = prob_density(x_fine)
plt.plot(x_fine, probs, 'r', linewidth=2);

# Using the histogram:
plt.hist(average_axes01, bins=7, normed=True, alpha=.4)

plt.ylabel('probability density function'); plt.xlabel('values');

example graph

Upvotes: 1

Related Questions