How to create a contour plot in Julia

Plotting in Julia can be done using various packages like pyplot, plotly, and so on. Plots is one such package with which we can plot data in Julia.

The plots package can be installed using the following commands:

import Pkg;
Pkg.add("Plots")

In the above,

  • The first command imports the package manager.
  • The second command downloads and installs the package.

To use the package, execute the following command:

using Plots

Contour plots

A contour plot consists of two independent-variable arrays and a dependent-variable array (the contour). A contour plot is useful in visualizing how the dependent variable Z changes as a function of the two independent variables X and Y, i.e Z = f(X,Y).

In Julia, contour plots are created using the contour() function of the Plots package.

Note: Go to the Educative Answer Contour plots to learn about the definition of contour plots.

Syntax

using Plots
contour(x, y, z)

Parameters

  • Variables x and y are the independent variables.
  • Variable z is the dependent variable.

Coding example

using Plots
x = 1:15
y = 100:115
f(x, y) = begin
x^2 + y^2
end
contour(x, y, f, fill=true, plot_title="Contour Plot")
savefig("output/plot.png")

Explanation

  • Line 1: We import Plots.
  • Lines 3–4: We define the x and y dimension data points.
  • Lines 6–8: We define a function that accepts the x and y coordinates and returns the result of x<sup>2</sup> + y<sup>2</sup>.
  • Line 10: We use the contour() function to create a contour plot.
  • Line 11: We save the plot to the local disk.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved