plt.contour
for contour plots, plt.contourf
for filled contour plots, and plt.imshow
for showing images.# In[1]
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
import numpy as np
# In[2]
def f(x,y):
return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
plt.contour
function.x
values, a grid of y
values, and a grid of z
values.x
and y
values represent positions on the plot, and the z
values will be represented by the contour levels.np.meshgrid
funciton, which builds two-dimensional grids from one-dimensional arrays.# In[3]
x=np.linspace(0,5,50)
y=np.linspace(0,5,40)
X,Y=np.meshgrid(x,y)
Z=f(X,Y)
plt.contour(X,Y,Z,colors='black');
cmap
argument.# In[4]
plt.contour(X,Y,Z,20,cmap='RdGy');
RdGy
(short for Red-Gray) colormap, which is good choice for divergent data.plt.cm
module: plt.cm.<TAB>
plt.contourf
function, which uses largely the same syntax as plt.contour
.plt.colorbar
command, which creates an additional axis with labeled color information for the plot.# In[5]
plt.contourf(X,Y,Z,20,cmap='RdGy')
plt.colorbar();
plt.imshow
function, which offers the interpolation
argument to generate a smooth two-dimensional representation of the data.# In[6]
plt.imshow(Z,extent=[0,5,0,5],origin='lower',cmap='RdGy',
interpolation='gaussian',aspect='equal')
plt.colorbar();
plt.imshow
x
and y
grid, so you must manually specify the extent
[] of the image on the plot.aspect
argument.alpha
parameter) and overplot contours with labels on the contours themselves, using the plt.clabel
function.# In[7]
contours=plt.contour(X,Y,Z,3,colors='black')
plt.clabel(contours,inline=True,fontsize=8)
plt.imshow(Z,extent=[0,5,0,5],origin='lower',cmap='RdGy',alpha=0.5)
plt.colorbar();
plt.contour
, plt.contourf
, and plt.imshow
- gives nearly limitless possibilities for displaying this sort of three-dimensional data within a two-dimensional plot.For more information on this options available in these functions, refer to these url :
1. plt.contour documentation
2. plt.contourf documentation
3. plt.imshow documentation
# In[8]
rng=np.random.default_rng(1701)
data=rng.normal(size=1000)
plt.hist(data);
hist
function has many options to tune both the calculation and the display; here's an example of a more customized histogram.# In[9]
plt.hist(data,bins=30,density=True,alpha=0.5,
histtype='stepfilled',color='steelblue',
edgecolor='none');
bin
parameter specifies the number of horizontal axis sections of the histogram.histtype='stepfilled'
along with some transparency alpha
to be helpful when comparing histograms of several distributions# In[10]
x1=rng.normal(0,0.8,1000)
x2=rng.normal(-2,1,1000)
x3=rng.normal(3,2,1000)
kwargs=dict(histtype='stepfilled',alpha=0.3,density=True,bins=40)
plt.hist(x1,**kwargs)
plt.hist(x2,**kwargs)
plt.hist(x3,**kwargs);
np.histogram
function# In[11]
counts,bin_edges=np.histogram(data,bins=5)
print(counts)
# Out[11]
[ 23 241 491 224 21]
For more information about plt.hist
other available customization options, refer to this url :
plt.hist documentation
x
and y
array drawn from a multivariate Gaussian distribution# In[12]
mean=[0,0]
cov=[[1,1],[1,2]]
x,y=rng.multivariate_normal(mean,cov,10000).T
For more about multivariate_normal
, refer to this url :
numpy.random.Generator.multivariate_normal
plt.hist2d
function.# In[13]
plt.hist2d(x,y,bins=30)
cb=plt.colorbar()
cb.set_label('counts in bin')
plt.hist
, plt.hist2d
has a number of extra options to fine-tune the plot and the binning, which are nicely outlined in the function docstring.plt.hist
has a counterpart in np.histogram
, plt.hist2d
has a counterpart in np.histogram2d
# In[14]
counts,xedges,yedges=np.histogram2d(x,y,bins=30)
print(counts.shape)
# Out[14]
(30, 30)
np.histogramdd
function.plt.hexbin
routine, which represents a two-dimensional dataset binned within a grid of hexagons.# In[15]
plt.hexbin(x,y,gridsize=30)
cb=plt.colorbar(label='count in bin')
plt.hexbin
has a number of additional options, including the ability to specify weights for each point and to change the output in each bin to any Numpy aggregate.scipy.stats
package.# In[16]
from scipy.stats import gaussian_kde
# fit an array of size [Ndim,Nsample]
data=np.vstack([x,y])
kde=gaussian_kde(data)
# evaluate on a regular grid
xgrid=np.linspace(-3.5,3.5,40)
ygrid=np.linspace(-6,6,40)
Xgrid,Ygrid=np.meshgrid(xgrid,ygrid)
Z=kde.evaluate(np.vstack([Xgrid.ravel(),Ygrid.ravel()]))
# plot the result as an image
plt.imshow(Z.reshape(Xgrid.shape),
origin='lower',aspect='auto',extent=[-3.5,3.5,-6,6])
cb=plt.colorbar()
cb.set_label('density')
gaussian_kde
uses a rule of thumb to attempt to find a nearly optimal smoothing length for the input data.