generate matplotlib images without having access to an X server
joseph pareti
joepareti54 at gmail.com
Wed Jan 23 08:47:57 EST 2019
The following piece of code is supposed to generate images for plotting:
(in *bold*, my added / corrected statements)
...
*# JP handle non X server*
*import matplotlib*
*matplotlib.use('Agg')*
# JP
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
...
# Generate images from noise, using the generator network.
f, a = plt.subplots(4, 10, figsize=(10, 4))
for i in range(10):
# Noise input.
z = np.random.uniform(-1., 1., size=[4, noise_dim])
g = sess.run([gen_sample], feed_dict={gen_input: z})
g = np.reshape(g, newshape=(4, 28, 28, 1))
# Reverse colours for better display
g = -1 * (g - 1)
for j in range(4):
# Generate image from noise. Extend to 3 channels for matplot
figure.
img = np.reshape(np.repeat(g[j][:, :, np.newaxis], 3, axis=2),
newshape=(28, 28, 3))
a[j][i].imshow(img)
*# JP HAck*
*# see
https://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
<https://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server>*
*# "tuple packing" ----->
https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences
<https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences>*
*# f.show()*
*# plt.draw()*
*# plt.waitforbuttonpress()*
*# a.savefig('a.png')*
* f.savefig('f.png')*
To make it run, I commented out all plt.* statements. Indeed it runs and it
produces the 'f.png' picture.
However, when the statement:
a.savefig('a.png')
is also activated (i.e. not commented out), the program ends with the
following exception:
* a.savefig('a.png')*
*AttributeError: 'numpy.ndarray' object has no attribute 'savefig'*
Anything I am missing? I am not familiar with the code, just trying to
understand it.
Thank you
--
Regards,
Joseph Pareti - Artificial Intelligence consultant
cell +49 1520 1600 209
cell +39 339 797 0644
More information about the Python-list
mailing list