I try to save the plot to a numpy array in RGB format and find a sample code from Matplotlib: save plot to numpy array.
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
fig.tight_layout(pad=0)
fig.canvas.draw()
data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
print(data.shape)
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
However the length of bytes generated from fig.canvas.tostring_rgb()
is 3686400 and the shape of fig.canvas.get_width_height()
is (640,480,3), which is mismatched, since 640 * 480 *3 = 921600.
It happens only when I run this code on windows using terminal. Jupyer notebook on Windows and Linux output correct bytes which has length 921600.
matplotlib version is 3.4.2.