VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/contour-plots/

⇱ Contour plots - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Contour plots

Last Updated : 22 Jan, 2021

A contour plot is a graphical method to visualize the 3-D surface by plotting constant Z slices called contours in a 2-D format. The contour plot is an alternative to a 3-D surface plot

The contour plot is formed by:

  • Vertical axis: Independent variable 2
  • Horizontal axis: Independent variable 1
  • Lines: iso-response values, can be calculated with the help (x,y).

The independent variable usually restricted to a regular grid. The actual techniques for determining the correct iso-response values are rather complex and almost always computer-generated.

The contour plot is used to depict the change in Z values as compared to X and Y values. If the data (or function) do not form a regular grid, you typically need to perform a 2-D interpolation to form a regular grid.

For one variable data, a run sequence/ histogram is considered necessary. For two-variable data, a scatter plot is considered necessary. The contour plots can also polar co-ordinates (r,theta) instead of traditional rectangular (x, y, z) coordinates. 

Types of Contour Plot:

  • Rectangular Contour plot: A projection of 2D-plot in 2D-rectangular canvas. It is the most common form of the contour plot.
  • Polar contour plot: Polar contour plot isplotted by using the polar coordinates r and theta. The response variable here is the collection of values generated while passing r and theta into the given function, where r is the distance from origin and theta is the angle from the positive x axis.
  • Ternary contour plot: Ternary contour plot is used to represent the relationship between 3 explanatory variables and the response variable in the form of a filled triangle.

Contour plot can be plotted in different programming languages:

  • Python/ Matplotlib: Contour plot can be plotted using plt.contour or plt.contourf functions, where plt is matplotlib.pyplot. The difference between these two that plot.contour generates hollow contour plot, the plt.contourf generated filled.
  • Matlab: functions such as contourf (2d-plot) and contour3 (3D-contour) can be used for contour plotting
  • R: can create a contour plot with filled.contour functions in R.

Implementations:

  • Rectangular Contour Plot: Below is the sample code for plotting rectangular contour plots in Python and matplotlib.
  • Polar Contour plot: For plotting polar contour plot we need to define first r and theta. Below is the sample code for plotting polar contour plots using matplotlib subplots.
  • Ternary Contour Plot: Matplotlib does not provide a definitive API for plotting Ternary Contour plot, however, there are many other package which does that. IN this example, we will be using Plotly library.

References:

Comment