Jiemie
Jiemie

Reputation: 51

Print text before plotting an image (matplotlib/imageio)

I'm pretty new to Python (and really new to using MatPlotLib and imageio), and I was wondering if there was a possibility to first plot an image and then print some text.
What I mean: when you give:

print('Test')
plt.imshow(imageio.imread(<location>))

It first prints the text and then the image, but when you give:

plt.imshow(imageio.imread(<location>))
print('Test')

It still prints the text first.
Is there a solution to this? And if not, is there an alternative way to do this?

Upvotes: 5

Views: 2277

Answers (2)

AshlinJP
AshlinJP

Reputation: 373

Use plt.show() after imshow

import matplotlib.pylab as plt 
from numpy import random

Z = random.random((20,20))   # Test data 

plt.imshow(Z, cmap=plt.get_cmap("Spectral"), interpolation='nearest') # Test plot

plt.show()
print("test")

The result:

enter image description here

Upvotes: 1

Pygirl
Pygirl

Reputation: 13349

Use plt.show() after plt.imshow()

C=np.random.rand(500).reshape((20,25))
S=np.random.rand(500).reshape((20,25))

def function(s,c):
    return s*2+c

dc = function(S,C)

import imageio
import matplotlib.pyplot as plt
print('Test')
plt.imshow(dc)
# it first prints the text and then the image, but when you give
# plt.imshow(imageio.imread(<location>))
plt.show()
print('Test')

enter image description here

Upvotes: 0

Related Questions