Reputation: 1119
I know this is a popular question, but couldn't find out any solution that solved my problem so far. I want to build a figure that maps 1:1 from a numpy array.
With this example:
import numpy as np
import matplotlib.pyplot as plt
my_dpi=100
i = np.full((100, 100, 3), 0.5, dtype=np.double)
plt.tight_layout()
fig, ax = plt.subplots(figsize=(100/my_dpi,100/my_dpi), facecolor='red', dpi=my_dpi)
ax.imshow(i)
ax.axis('off')
plt.show()
the best I can get is:
How can I make sure that the boundaries of the subplot (numpy array) extend to the full extent of the containing figure ?
Upvotes: 0
Views: 70
Reputation: 765
The easiest way IMHO is actually using package Pillow to export the data. The minimum example would be,
import numpy as np
from PIL import Image
i = np.full((100, 100, 3), 0.5, dtype=np.double)
im = Image.fromarray(i, mode='RGB')
im.save("img.png")
In the case of using matplotlib, you may want to try this method, subplots_adjust
. Example:
import numpy as np
import matplotlib.pyplot as plt
my_dpi=100
i = np.full((100, 100, 3), 0.5, dtype=np.double)
fig, ax = plt.subplots(
figsize=(100/my_dpi,100/my_dpi),
facecolor='red', dpi=my_dpi
)
ax.imshow(i)
ax.axis('off')
plt.subplots_adjust(0, 0, 1, 1)
plt.show()
Upvotes: 1