I am plotting 3D data using matplotlib==3.3.4
:
fig = plt.figure(figsize=(15, 10))
ax = fig.gca(projection="3d")
ax.view_init(30, 0)
# facecolors is a 3D volume with some processing
ax.voxels(
x, y, z, facecolors[:, :, :, -1] != 0, facecolors=facecolors, shade=False
)
fig.canvas.draw()
image_flat = np.frombuffer(fig.canvas.tostring_rgb(), dtype="uint8")
image_shape = (*fig.canvas.get_width_height(), 3) # (1500, 1000, 3)
ax.imshow(image_flat.reshape(*image_shape))
plt.show()
(I am making some improvements on BraTS20_3dUnet_3dAutoEncoder with inspiration from Figure to image as a numpy array).
However, when I actually plot the image, there are two copies:
What am I doing wrong? I can't figure out where the second image is coming from.
The NumPy array ordering is (rows, cols, ch). The code image_shape = (*fig.canvas.get_width_height(), 3)
switches rows
and cols
, which leads to the output image being incorrectly shaped, which looks like two copies.
Replace image_shape = (*fig.canvas.get_width_height(), 3)
with:
image_shape = (*fig.canvas.get_width_height()[::-1], 3)
For avoiding confusion, we better use two lines of code:
cols, rows = fig.canvas.get_width_height()
image_shape = (rows, cols, 3)
Reproducible example (using data from here):
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(15, 10))
ax = fig.gca(projection="3d")
ax.view_init(30, 0)
# https://stackoverflow.com/questions/76387953/image-duplicated-when-using-matplotlib-fig-canvas-tostring-rgb
# prepare some coordinates
x, y, z = np.indices((8, 8, 8))
# draw cuboids in the top left and bottom right corners, and a link between
# them
cube1 = (x < 3) & (y < 3) & (z < 3)
cube2 = (x >= 5) & (y >= 5) & (z >= 5)
link = abs(x - y) + abs(y - z) + abs(z - x) <= 2
# combine the objects into a single boolean array
voxelarray = cube1 | cube2 | link
# set the colors of each object
colors = np.empty(voxelarray.shape, dtype=object)
colors[link] = 'red'
colors[cube1] = 'blue'
colors[cube2] = 'green'
# and plot everything
#ax = plt.figure().add_subplot(projection='3d')
ax.voxels(voxelarray, facecolors=colors, edgecolor='k')
fig.canvas.draw()
image_flat = np.frombuffer(fig.canvas.tostring_rgb(), dtype="uint8")
#image_shape = (*fig.canvas.get_width_height(), 3) # (1500, 1000, 3)
#image_shape = (*fig.canvas.get_width_height()[::-1], 3) # It should be (1000, 1500, 3) instead of (1500, 1000, 3)
cols, rows = fig.canvas.get_width_height()
image_shape = (rows, cols, 3)
img = image_flat.reshape(*image_shape)
plt.figure()
plt.imshow(img)
plt.show()
Output image before fixing the code:
Output image after fixing the code: