Introduction to Density and Contour Plots in MatPlotLib
At the commencement of our series pertaining to the MatPlotLib library, we first explored introductory topics pertaining to the subject such as installation, importing, and setting up configurations. This may be further explored here. This was immediately followed by a presentation of the basic mechanics of plot construction and customization, a discussion which you may address here. Finally, in our most recent article, we elaborated in greater depth on a more sophisticated plot type, the scatter plot, which you can view here. In this article, however, we dive into a much more complex form of plotting, specifically using Density and Contour plots in MatPlotLib. Let’s explore this topic further herein.
Conceptualizing Density and Contour Plots
The implementation of density and contour plots revolves around modeling three dimensional data. While a large amount of data can be efficiently modeled in two-dimensions, oftentimes, data in the real world is much more complex. In these instances, three-dimensional modeling could be an option. This is efficaciously performed using the density and contour plots in MatPlotLib.
Perhaps you may have seen our previous series addressing multivariate functions. If not, you can check this series out here. It elaborates on computations with multivariate functions, so this might be a good introduction to this subject matter if you have not had previous experience. Nevertheless, these are the typical functions which confer the necessity of modeling data in a three dimensional format.
We will not labor upon the intricacies of multivariate functions, but suffice it to say that these functions typically adhere to the form z=f(x,y). In this manner, ‘f’ represents a function that takes inputs ‘x’ and ‘y’ and creates a computation on these variables which returns the output ‘z’. With that, we may create data by defining a function that performs computations on our input to create outputs. The code for this appears as follows:
Before checking out the specificities of the plot, let’s take a look at the code which creates this plot. Firstly, we create values for ‘x’ and ‘y’ using the Numpy linspace function. We then define a function that accepts values for ‘x’ and ‘y’. We take a detour to create all of the different (x,y) pairs using the Numpy meshgrid function. Because we have 100 ‘x’ values and 50 ‘y’ values, we should obtain 500 (x,y) pairs. Each one of these pairs then become used to create ‘z’ values. We then use the plot contour function, specifying the values of ‘x’, ‘y’, and ‘z’. Additionally, we specify the color of the plot as black. The plot created by this code appears as follows:
Customizing Contour Plots
Specifying Contour Plot Color
In the contour plot above, we ought to note that the dashed lines represent values that are negative in the ‘z’ dimension, while solid lines reflect positive values in the ‘z’ dimension. As demonstrated in previous discussions of line plots and scatter plots, one of the basic customization features we have available is control over the color of the plot. If we specify the color keyword argument with blue, the plot appears as follows:
We can also differentiate the data with differentially colored lines using the cmap argument. For example, we can create a plot based upon a scale of red and gray colors by specifying:
Executing the preceding code produces a plot that appears as:
Sometimes it can be difficult to interpret the significance of contour plots. For that reason, filled contour plots can be quite useful for improved interpretation. In the case of filled contour plots, which are really better described as density plots, confer the magnitude of z-dimensional distance represented by the density of the color.
The filled contour plot is created using the plt.contourf function, and is easily controlled in the same manner as the normal contour plot function. In addition to creating the filled contour plot, we must create a reference color bar that confers the value associated with a particular intensity of the color. We execute this with the plt.colorbar function. Let’s take a moment to check out the code that creates this plot:
Firstly, take note that when applying the ‘plt.contourf()’ function, we utilize the same ‘x’, ‘y’, ‘z’ values, but we also change the color map argument using the ‘viridis’ specification. This colormap uses a variety of yellows and greens in creating the filled contour plots. Additionally, we create the color bar function so that a particular intensity of color refers to a specific magnitude in the z-dimension. Take a look at the plot created by this code:
Many people find this type of plot to be much easier to interpret than the general contour plot.
Customizing the Density Plot
In the density plot above, note that the most intense regions of yellow represent peaks while deep blue colors confer the troughs. However, one issue with our plot is that our color bar, and thus the plot itself, does not have smooth differentiations between color. Rather, the steps between color gradients are discrete. To do this, employ the plt.imshow() which interprets the data as an image.
The plt.imshow function takes several different parameters. Firstly, the user must specify the data container to be represented by the color gradient. In our case, this data object is ‘z’. We also show the range of the ‘x’ and y’ axes in a list. We also specify the location of the origin and finally, specify the color map. The code for executing this appears as follows:
As we can see, the color bar now exhibits a continuous gradient rather than discrete color changes. However, looking at the image, the differences between values still appear as discrete differences. This makes our interpretation of the figure much more precise. Let’s take a look at the plot procured by this code:
The Take Away
The contour and density plots we have elaborated upon herein demonstrate the power of modeling data in this manner. Perhaps the greatest utility is the ability to model three dimensional data for multivariate functions. Furthermore, as with a variety of other MatPlotLib functions, the library offers a broad cornucopia of customization features. These offer the user much more control over the plot they develop. In our next article, we elaborate in great lengths on the various features of histograms. Hopefully this is something you will find useful. Until then, if you seek to explore the subject of contour and density plots in MatPlotLib further, take a look at the MatPlotLib manual. It may be found here.