|
46 | 46 | ishold - return the hold state of the current axes
|
47 | 47 | legend - make an axes legend
|
48 | 48 | loglog - a log log plot
|
| 49 | + matshow - display a matrix in a new figure preserving aspect |
49 | 50 | pcolor - make a pseudocolor plot
|
50 | 51 | pie - make a pie chart
|
51 | 52 | plot - make a line plot
|
|
195 | 196 | from cbook import flatten, is_string_like, exception_to_str, popd, silent_list, iterable
|
196 | 197 | from colors import normalize
|
197 | 198 | from cm import ColormapJet, Grayscale, get_cmap
|
198 |
| -from figure import Figure |
| 199 | +from figure import Figure, figaspect |
199 | 200 | import image
|
200 | 201 | from matplotlib import rcParams, rcParamsDefault, get_backend
|
201 | 202 | from backend_bases import FigureCanvasBase
|
@@ -336,6 +337,7 @@ def plotting():
|
336 | 337 | loglog - a log log plot
|
337 | 338 | imread - load image file into array
|
338 | 339 | imshow - plot image data
|
| 340 | + matshow - display a matrix in a new figure preserving aspect |
339 | 341 | pcolor - make a pseudocolor plot
|
340 | 342 | plot - make a line plot
|
341 | 343 | psd - make a plot of power spectral density
|
@@ -1598,6 +1600,71 @@ def switch_backend(newbackend):
|
1598 | 1600 | from backends import new_figure_manager, error_msg, \
|
1599 | 1601 | draw_if_interactive, show
|
1600 | 1602 |
|
| 1603 | + |
| 1604 | +def matshow(*args,**kw): |
| 1605 | + """Display an array as a matrix. |
| 1606 | +
|
| 1607 | + The origin is set at the upper left hand corner and rows (first dimension |
| 1608 | + of the array) are displayed horizontally. The aspect ratio of the figure |
| 1609 | + window is that of the array, as long as it is possible to fit it within |
| 1610 | + the constraints of your figure.figsize_min/max parameters with no |
| 1611 | + stretching. If the window dimensions can't accomodate this (extremely |
| 1612 | + tall/wide arrays), some stretching will inevitably occur. |
| 1613 | +
|
| 1614 | + matshow() calls imshow() with Aargs and **kwargs, but by default |
| 1615 | + it sets interpolation='nearest' (unless you override it). All |
| 1616 | + other arguments and keywords are passed to imshow(), so see its |
| 1617 | + docstring for further details. |
| 1618 | +
|
| 1619 | + Tick labels for the xaxis are placed on top by default. |
| 1620 | + |
| 1621 | + return value is a (fig, ax, im) tuple |
| 1622 | +
|
| 1623 | + Example usage: |
| 1624 | + |
| 1625 | + def samplemat(dims): |
| 1626 | + aa = zeros(dims) |
| 1627 | + for i in range(min(dims)): |
| 1628 | + aa[i,i] = i |
| 1629 | + return aa |
| 1630 | +
|
| 1631 | + dimlist = [(12,12),(128,64),(64,512),(2048,256)] |
| 1632 | +
|
| 1633 | + |
| 1634 | + for i, d in enumerate(dimlist): |
| 1635 | + fig, ax, im = matshow(samplemat(d)) |
| 1636 | + show() |
| 1637 | + |
| 1638 | + """ |
| 1639 | + |
| 1640 | + arr = args[0] |
| 1641 | + w,h = figaspect(arr) |
| 1642 | + fig = figure(figsize=(w,h)) |
| 1643 | + ax = fig.add_axes([0.0, 0.05, 0.8, 0.8]) |
| 1644 | + |
| 1645 | + ax.xaxis.tick_top() |
| 1646 | + ax.title.set_y(1.05) # raise it up a bit for tick top |
| 1647 | + # Call with 'lower' origin (we'll flip axes later) |
| 1648 | + kw['origin'] = 'lower' |
| 1649 | + |
| 1650 | + # We also set a 'free' aspect ratio b/c we've already done our best to fix |
| 1651 | + # it, while preserving the dimension sanity checks. At this point, if a |
| 1652 | + # bit of rescaling is needed, so be it. Otherwise, we'd get the nasty |
| 1653 | + # white bands we're working so hard to prevent. |
| 1654 | + kw['aspect'] = 'free' |
| 1655 | + |
| 1656 | + # Unless overridden, don't interpolate |
| 1657 | + kw.setdefault('interpolation','nearest') |
| 1658 | + |
| 1659 | + # All other keywords go through to imshow. |
| 1660 | + im = ax.imshow(*args,**kw) |
| 1661 | + |
| 1662 | + # set the x and y lim to equal the matrix dims |
| 1663 | + nr,nc = arr.shape[:2] |
| 1664 | + ax.set_xlim((0,nc)) |
| 1665 | + ax.set_ylim((nr,0)) |
| 1666 | + return fig, ax, im |
| 1667 | + |
1601 | 1668 |
|
1602 | 1669 | ### The following functions were autogenerated by the boilerplate.py
|
1603 | 1670 | ### script. They are simple wrappers around the Axes methods of the
|
@@ -2520,22 +2587,23 @@ def winter():
|
2520 | 2587 |
|
2521 | 2588 | __plotting_all__ = [
|
2522 | 2589 | # plotting
|
| 2590 | + |
2523 | 2591 | 'plotting', 'colormaps', 'get_current_fig_manager', 'connect',
|
2524 | 2592 | 'disconnect', 'get_plot_commands', 'raise_msg_to_str', 'axis',
|
2525 | 2593 | 'axes', 'delaxes', 'clim', 'close', 'clf', 'colorbar', 'draw',
|
2526 | 2594 | 'figtext', 'figimage', 'figlegend', 'figure', 'gca', 'gcf', 'gci',
|
2527 | 2595 | 'get', 'hold', 'ishold', 'isinteractive', 'imread', 'load', 'rc',
|
2528 |
| - 'rcdefaults', 'save', 'savefig', 'set', 'subplot', 'twinx', 'title', |
2529 |
| - 'xlabel', 'ylabel', 'xlim', 'ylim', 'xticks', 'rgrids', |
| 2596 | + 'rcdefaults', 'save', 'savefig', 'set', 'subplot', 'twinx', |
| 2597 | + 'title', 'xlabel', 'ylabel', 'xlim', 'ylim', 'xticks', 'rgrids', |
2530 | 2598 | 'thetagrids', 'yticks', 'polar', 'over', 'ioff', 'ion', 'axhline',
|
2531 | 2599 | 'axhspan', 'axvline', 'axvspan', 'bar', 'barh', 'cohere',
|
2532 | 2600 | 'contour', 'csd', 'errorbar', 'fill', 'hist', 'hlines', 'imshow',
|
2533 |
| - 'loglog', 'pcolor', 'pcolor_classic', 'pie', 'plot', 'plot_date', |
2534 |
| - 'psd', 'scatter', 'scatter_classic', 'semilogx', 'semilogy', |
2535 |
| - 'specgram', 'spy', 'spy2', 'stem', 'vlines', 'cla', 'grid', |
2536 |
| - 'legend', 'table', 'text', 'autumn', 'bone', 'cool', 'copper', |
2537 |
| - 'flag', 'gray', 'hot', 'hsv', 'jet', 'pink', 'prism', 'spring', |
2538 |
| - 'summer', 'winter', |
| 2601 | + 'loglog', 'matshow', 'pcolor', 'pcolor_classic', 'pie', 'plot', |
| 2602 | + 'plot_date', 'psd', 'scatter', 'scatter_classic', 'semilogx', |
| 2603 | + 'semilogy', 'specgram', 'spy', 'spy2', 'stem', 'vlines', 'cla', |
| 2604 | + 'grid', 'legend', 'table', 'text', 'autumn', 'bone', 'cool', |
| 2605 | + 'copper', 'flag', 'gray', 'hot', 'hsv', 'jet', 'pink', 'prism', |
| 2606 | + 'spring', 'summer', 'winter', |
2539 | 2607 |
|
2540 | 2608 | # classes and modules
|
2541 | 2609 |
|
|
0 commit comments