Ответ 1
Это удобный трюк для модульных тестов и т.п., когда вам нужно сравнить пиксель-пиксель с сохраненным сюжетом.
Один из способов - использовать fig.canvas.tostring_rgb
, а затем numpy.fromstring
с approriate dtype. Есть и другие способы, но это тот, который я обычно использую.
например.
import matplotlib.pyplot as plt
import numpy as np
# Make a random plot...
fig = plt.figure()
fig.add_subplot(111)
# If we haven't already shown or saved the plot, then we need to
# draw the figure first...
fig.canvas.draw()
# Now we can save it to a numpy array.
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))