Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b11d9d

Browse files
committed
Merge pull request #4315 from alimehdi92/3420_feature
ENH : added resize parameter figimage Match the figure size to image size
2 parents 5da286b + 54e24c4 commit 1b11d9d

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
updated figimage to take optional resize parameter
2+
----------------------------------------------------
3+
Added the ability to plot simple 2D-Array using plt.figimage(X, resize=True).
4+
This is useful for plotting simple 2D-Array without the Axes or whitespacing
5+
around the image.
6+
Example:
7+
data = np.random.random( [500, 500] )
8+
plt.figimage(data, resize=True)

lib/matplotlib/figure.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ def figimage(self, X,
576576
vmin=None,
577577
vmax=None,
578578
origin=None,
579+
resize=False,
579580
**kwargs):
580581
"""
581582
Adds a non-resampled image to the figure.
@@ -603,6 +604,8 @@ def figimage(self, X,
603604
========= =========================================================
604605
Keyword Description
605606
========= =========================================================
607+
resize a boolean, True or False. If "True", then re-size the
608+
Figure to match the given image size.
606609
xo or yo An integer, the *x* and *y* image offset in pixels
607610
cmap a :class:`matplotlib.colors.Colormap` instance, e.g.,
608611
cm.jet. If *None*, default to the rc ``image.cmap``
@@ -637,6 +640,11 @@ def figimage(self, X,
637640
if not self._hold:
638641
self.clf()
639642

643+
if resize:
644+
dpi = self.get_dpi()
645+
figsize = [x / float(dpi) for x in (X.shape[1], X.shape[0])]
646+
self.set_size_inches(figsize, forward=True)
647+
640648
im = FigureImage(self, cmap, norm, xo, yo, origin, **kwargs)
641649
im.set_array(X)
642650
im.set_alpha(alpha)

lib/matplotlib/image.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,10 +1304,10 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
13041304
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
13051305
from matplotlib.figure import Figure
13061306

1307-
figsize = [x / float(dpi) for x in (arr.shape[1], arr.shape[0])]
1308-
fig = Figure(figsize=figsize, dpi=dpi, frameon=False)
1307+
fig = Figure(dpi=dpi, frameon=False)
13091308
canvas = FigureCanvas(fig)
1310-
im = fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)
1309+
im = fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin,
1310+
resize=True)
13111311
fig.savefig(fname, dpi=dpi, format=format, transparent=True)
13121312

13131313

0 commit comments

Comments
 (0)