Plt.contour() Plots Series Of Lines Instead Of A Contour Line
Introduction
Contour plots are a powerful visualization tool used to represent a 3D surface as a 2D image. They are commonly used in various fields such as meteorology, geology, and engineering to visualize data. In this article, we will explore the use of plt.contour()
in Matplotlib to create contour plots. We will also discuss a common issue where plt.contour()
returns a series of lines instead of a closed contour line.
What is plt.contour()?
plt.contour()
is a function in Matplotlib used to create contour plots. It takes in a 2D array of data and plots the contours of the data on a 2D plot. The contours are lines that connect points of equal value in the data.
Common Issue: Series of Lines Instead of Contour Lines
When using plt.contour()
, you may encounter a situation where the function returns a series of lines instead of a closed contour line. This issue can occur when the data is not properly formatted or when the contour levels are not set correctly.
Example Code
Let's consider an example code snippet that demonstrates the use of plt.contour()
:
import numpy as np
import matplotlib.pyplot as plt

Psi = np.random.rand(320, 200)
fig, ax = plt.subplots()
ax.contour(Psi, levels=10)
plt.show()
In this example, we create a 2D array of random data Psi
with dimensions 320x200. We then create a figure and axis using plt.subplots()
. Finally, we create a contour plot using ax.contour()
with 10 contour levels.
Troubleshooting: Series of Lines Instead of Contour Lines
If you encounter a situation where plt.contour()
returns a series of lines instead of a closed contour line, there are several things you can try to troubleshoot the issue:
1. Check the Data Format
Make sure that the data is properly formatted as a 2D array. You can check the shape of the data using np.shape()
:
print(np.shape(Psi))
This should output (320, 200)
if the data is properly formatted.
2. Set Contour Levels Correctly
Make sure that the contour levels are set correctly. You can set the contour levels using the levels
argument in ax.contour()
. For example:
ax.contour(Psi, levels=np.linspace(Psi.min(), Psi.max(), 10))
This sets the contour levels to 10 evenly spaced values between the minimum and maximum values of the data.
3. Use ax.contourf()
Instead of ax.contour()
If you are using ax.contour()
to create a filled contour plot, try using ax.contourf()
instead. ax.contourf()
creates a filled contour plot where the contours are filled with color.
4. Check the Plot Limits
Make sure that the plot limits are set correctly. You can set the plot limits using the _xlim()
and set_ylim()
methods:
ax.set_xlim(0, 320)
ax.set_ylim(0, 200)
This sets the x-axis limits to 0-320 and the y-axis limits to 0-200.
Conclusion
In this article, we discussed the use of plt.contour()
in Matplotlib to create contour plots. We also explored a common issue where plt.contour()
returns a series of lines instead of a closed contour line. By following the troubleshooting steps outlined in this article, you should be able to resolve the issue and create a contour plot with closed contour lines.
Additional Resources
For more information on contour plots and plt.contour()
, refer to the following resources:
- Matplotlib documentation: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.contour.html
- NumPy documentation: https://numpy.org/doc/stable/user/basics.html
Example Use Cases
Contour plots are commonly used in various fields such as:
- Meteorology: to visualize weather patterns and forecast data
- Geology: to visualize topographic data and geological features
- Engineering: to visualize fluid dynamics and heat transfer data
Q: What is the difference between plt.contour() and plt.contourf()?
A: plt.contour()
creates a contour plot where the contours are lines that connect points of equal value in the data. plt.contourf()
, on the other hand, creates a filled contour plot where the contours are filled with color.
Q: How do I set the contour levels in plt.contour()?
A: You can set the contour levels using the levels
argument in ax.contour()
. For example:
ax.contour(Psi, levels=np.linspace(Psi.min(), Psi.max(), 10))
This sets the contour levels to 10 evenly spaced values between the minimum and maximum values of the data.
Q: Why is my contour plot not showing up?
A: There are several reasons why your contour plot may not be showing up. Here are a few things to check:
- Make sure that the data is properly formatted as a 2D array.
- Check that the contour levels are set correctly.
- Make sure that the plot limits are set correctly.
- Check that the axis is not set to a log scale.
Q: How do I add labels to my contour plot?
A: You can add labels to your contour plot using the set_xlabel()
and set_ylabel()
methods. For example:
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
You can also add a title to your plot using the set_title()
method:
ax.set_title('Contour Plot')
Q: How do I save my contour plot to a file?
A: You can save your contour plot to a file using the savefig()
method. For example:
plt.savefig('contour_plot.png')
This will save the plot to a file named contour_plot.png
in the current working directory.
Q: How do I create a 3D contour plot?
A: You can create a 3D contour plot using the plot_surface()
function from the Axes3D
toolkit. For example:
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(Psi, cmap='viridis')
plt.show()
This will create a 3D contour plot of the data in Psi
.
Q: How do I create a contour plot with multiple subplots?
A: You can create a contour plot with multiple subplots using the subplots()
function. For example:
fig, axs = plt.subplots(2, 2)
for ax in axs.flat:
ax.contour(Psi)
plt.show()
This will create a 2x2 grid of subplots, each containing a contour plot of the data in Psi
.
Q: How do I customize the appearance of my contour plot?
A: You can customize the appearance of your contour plot using various options available in the contour()
function. For example, you can change the map using the cmap
argument:
ax.contour(Psi, cmap='viridis')
You can also change the line style using the linestyle
argument:
ax.contour(Psi, linestyle='--')
These are just a few examples of how you can customize the appearance of your contour plot.