2D plots can be made if you have a z value for each x and y value. This can just be a 2D array of z values. One can make a density plot from x and y values, by determining the number of points in a bin. For example we can start with a random set of x and y values.
N=100000
x=np.random.normal(size=N)
y=np.random.normal(size=N)
ax.hist2d(x,y) #makes a plot
ax.hexbin(x,y) #uses hexagon instead of square bins
hist,xed,yed=np.histogram2d(x,y,bins=25) #returns array
with 2D arrays there are two ways to visualize them. One can use a heatmap, where each array value is assigned a color from a colormap to make an image. We do this using the imshow() function. Note that this only plots the array values, the x and y axis will just be the lengths of the array unless we give them other values using the extent keyword. If we want to plot our histogram from above with the edges as the x and y axis we could use
ax.imshow(hist,cmap='brg',extent=[xed[0],xed[-1],yed[0],yed[-1]])
We can change the colormap, there are many of them you can find here. It is always good practice to also include a colorbar to make sense of your colors.
plt.colorbar()
By default the color map connects a color to the values of your array linearly starting with the lowest value to the highest value. You can change that behavior by setting vmin, vmax and norm for your plot. Norm allows you to perform a transformation that makes the mapping not linear, vmin and vmax just adjust the minimum and maximum values.
import matplotlib.colors as colors
ax.imshow(hist,cmap='brg', extent=[xed[0],xed[-1],yed[0],yed[-1]],
norm=colors.LogNorm(vmin = 5, vmax=500))
Note that a logarithmic transform will fail if you have values equal to or less that zero. Instead you can use SymLogNorm() in those cases which takes another keyword linthresh that sets when the log scaling should start.
ax.imshow(hist,cmap='brg', extent=[xed[0],xed[-1],yed[0],yed[-1]],
norm=colors.SymLogNorm(vmin = -100, vmax=100, lintresh=10))
Alternatively to making a heatmap one can instead make a contour plot. The advantage of contour plots if you know the precise values of the contours and can choose them.
cs=ax.contour(hist,[1,5,8],extent=[xed[0],xed[-1],yed[0],yed[-1]]))
ax.clabel(cs,inline=1,fontsize=10,fmt='%d')