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
xandyare the independent variables. - Variable
zis the dependent variable.
Coding example
using Plotsx = 1:15y = 100:115f(x, y) = beginx^2 + y^2endcontour(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
xandydimension data points. - Lines 6–8: We define a function that accepts the
xandycoordinates and returns the result ofx<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