3
\$\begingroup\$

I want to use Python and Matplotlib to create a data set of images. I want to do this by transforming the plot to a numpy matrix. At the moment it takes about 0.1 second to generate 100 images of size 50x50 pixels:

enter image description here

Question: How can I speed up things a lot?

This is my code:

import matplotlib.pyplot as plt
import numpy as np
import time
import cv2

fig = plt.figure(frameon=False)
ax = fig.add_axes([0., 0., 1., 1.])
fig.set_size_inches((0.5,0.5))
fig_size = fig.canvas.get_width_height() + (3,)
points, = ax.plot([],[])
ax.set_xlim(0.,1.)
ax.set_ylim(0.,1.)
ax.set_axis_off()
ax.set_frame_on(False)
ax.grid(False)

def plot2mat():
    data = np.random.rand(20,2)
    points.set_xdata(data[:,0])
    points.set_ydata(data[:,1])
    fig.canvas.draw()
    fig.canvas.flush_events()
    M = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep="")
    M = M.reshape(fig_size)
    M = cv2.cvtColor(M, cv2.COLOR_BGR2GRAY)
    return M

I = []
t0 = time.time()
runs = 1000
for k in range(runs):
    I.append(plot2mat())
print(time.time()-t0)

plt.close(fig)

for k in range(100):
    plt.subplot(10,10,k+1)
    plt.imshow(I[k])
    plt.axis("off")
plt.savefig("plot.png", dpi=400)
\$\endgroup\$
  • \$\begingroup\$ An execution time of 0.1 second is not much. Do you want to scale this up by much that you're concerned about speed? What's your goal? \$\endgroup\$ – Mast Aug 3 '18 at 17:39
  • 2
    \$\begingroup\$ @Mast I call plot2mat() from another script tens of thousands of times which makes this part a real bottleneck. \$\endgroup\$ – Samuel Aug 3 '18 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.