Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between plt.show and cv2.imshow in Matplotlib?
plt.show() and cv2.imshow() are two different methods for displaying images in Python. While plt.show() is part of Matplotlib and displays images in a Matplotlib figure window, cv2.imshow() is part of OpenCV and creates a native system window for image display.
Key Differences
| Aspect | plt.show() | cv2.imshow() |
|---|---|---|
| Library | Matplotlib | OpenCV |
| Color Format | RGB | BGR |
| Window Type | Matplotlib figure | Native system window |
| Multiple Images | Subplots supported | Separate windows |
Using plt.show() with Matplotlib
Matplotlib displays images in RGB format and provides rich plotting capabilities ?
import cv2
from matplotlib import pyplot as plt
# Read image with OpenCV (BGR format)
img_bgr = cv2.imread("image.jpg")
# Convert BGR to RGB for proper display
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(img_bgr) # Will show incorrect colors
plt.title("BGR in Matplotlib (Wrong Colors)")
plt.axis("off")
plt.subplot(1, 2, 2)
plt.imshow(img_rgb) # Will show correct colors
plt.title("RGB in Matplotlib (Correct Colors)")
plt.axis("off")
plt.show()
Using cv2.imshow() with OpenCV
OpenCV displays images in native system windows and expects BGR format ?
import cv2
# Read image with OpenCV (BGR format)
img = cv2.imread("image.jpg")
# Display in OpenCV window
cv2.imshow("OpenCV Window", img)
# Wait for key press and close window
cv2.waitKey(0)
cv2.destroyAllWindows()
Color Format Comparison
The main difference is how each library handles color channels ?
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# Method 1: Using OpenCV to read, Matplotlib to display
img_cv2 = cv2.imread("image.jpg")
img_rgb = cv2.cvtColor(img_cv2, cv2.COLOR_BGR2RGB)
# Method 2: Using Matplotlib to read and display
img_plt = mpimg.imread("image.jpg")
plt.figure(figsize=(12, 4))
plt.subplot(1, 3, 1)
plt.imshow(img_cv2)
plt.title("CV2 BGR in Matplotlib")
plt.axis("off")
plt.subplot(1, 3, 2)
plt.imshow(img_rgb)
plt.title("CV2 ? RGB in Matplotlib")
plt.axis("off")
plt.subplot(1, 3, 3)
plt.imshow(img_plt)
plt.title("Direct Matplotlib Read")
plt.axis("off")
plt.tight_layout()
plt.show()
When to Use Each Method
Use plt.show() when:
- Creating scientific plots or data visualizations
- Need subplots or multiple images in one figure
- Want to save figures to files
- Working in Jupyter notebooks
Use cv2.imshow() when:
- Building real-time computer vision applications
- Need fast image display without plotting overhead
- Working with video streams
- Want native window controls
Conclusion
Choose plt.show() for scientific visualization and analysis, while cv2.imshow() is better for computer vision applications. Remember that OpenCV uses BGR format while Matplotlib expects RGB format.
