How can I get pyplot images to show on a console app? (Matplotlib)

When working with Matplotlib in console applications, you need to properly configure the backend and use pyplot.show() to display images. This is essential for viewing plots in terminal-based Python environments.

Basic Setup

First, ensure you have the correct backend configured for your console environment ?

import matplotlib
matplotlib.use('TkAgg')  # or 'Qt5Agg' depending on your system
import matplotlib.pyplot as plt
import numpy as np

print("Backend:", matplotlib.get_backend())
Backend: TkAgg

Displaying Images in Console

Here's how to create and display a plot in a console application ?

import numpy as np
import matplotlib.pyplot as plt

# Set figure size and layout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create random data
data = np.random.rand(5, 5)

# Display the image
plt.imshow(data, cmap="copper")
plt.title("Random Data Visualization")
plt.colorbar()

# Show the plot in console
plt.show()

Interactive vs Non-Interactive Mode

You can control whether plots appear automatically ?

import matplotlib.pyplot as plt
import numpy as np

# Turn on interactive mode
plt.ion()

data = np.random.rand(3, 3)
plt.imshow(data, cmap="viridis")
plt.title("Interactive Mode")

# In interactive mode, plot appears immediately
plt.draw()

# Turn off interactive mode
plt.ioff()
plt.show()  # Now you need explicit show()

Common Issues and Solutions

Issue Solution Code
No display appears Check backend matplotlib.use('TkAgg')
Plot doesn't update Use interactive mode plt.ion()
Console blocks Use non-blocking show plt.show(block=False)

Multiple Plots Example

import matplotlib.pyplot as plt
import numpy as np

# Create multiple subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))

# First subplot
data1 = np.random.rand(4, 4)
ax1.imshow(data1, cmap="hot")
ax1.set_title("Hot Colormap")

# Second subplot  
data2 = np.random.rand(4, 4)
ax2.imshow(data2, cmap="cool")
ax2.set_title("Cool Colormap")

plt.tight_layout()
plt.show()

Conclusion

Use plt.show() to display Matplotlib plots in console applications. Ensure the correct backend is configured and consider using interactive mode for dynamic plotting. Always call show() explicitly in non-interactive environments.

Updated on: 2026-03-25T23:16:18+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements