Matplotlib can't show image in WSL
About 262 wordsLess than 1 minute
2026-07-28
It is a common problem on WSL that GUI based applications cannot be displayed. For example, when you try to use matplotlib to show an image, it may not work as expected. This is because WSL does not have a native display server.
Luckily, current WSL 2 supports Linux GUI applications through WSLg on Windows 10 build 19044+ and Windows 11. From an administrator PowerShell terminal, update and restart WSL:
wsl --update
wsl --shutdownThen reopen WSL and test:
sudo apt update
sudo apt install x11-apps -y
xeyesIf xeyes opens normally, GUI forwarding is working. Then we should be able to use matplotlib to show images in WSL.
It is probably a better solution to save the image to a file:
import matplotlib.pyplot as plt
plt.savefig('output.png') # Save the plot as a PNG fileOr you can use plt.imsave to save an image array directly:
import matplotlib.pyplot as plt
import numpy as np
# Create a sample image array
image_array = np.random.rand(100, 100, 3) # Random RGB image
plt.imsave('output_image.png', image_array) # Save the image arrayIf we are using Jupyter Notebook or Jupyter Lab, we can use the %matplotlib inline magic command to display plots directly in the notebook:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
# Create a sample image array
image_array = np.random.rand(100, 100, 3) # Random RGB image
plt.imshow(image_array) # Display the image
plt.axis('off') # Turn off axis
plt.show() # Show the plot