Skip to content

Consolidate and move examples out of pylab #8266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/users/prev_whats_new/whats_new_0.98.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ psd amplitude scaling

Ryan May did a lot of work to rationalize the amplitude scaling of
:func:`~matplotlib.pyplot.psd` and friends. See
:ref:`sphx_glr_gallery_pylab_examples_psd_demo2.py`. and :ref:`sphx_glr_gallery_pylab_examples_psd_demo3.py`.
:ref:`sphx_glr_gallery_pylab_examples_psd_demo.py`.
The changes should increase MATLAB
compatibility and increase scaling options.

Expand Down
4 changes: 2 additions & 2 deletions doc/users/prev_whats_new/whats_new_1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ been improved in the presence of NANs.
See the :ref:`sphx_glr_tutorials_02_intermediate_legend_guide.py` for more detailed explanation and
examples.

.. figure:: ../../gallery/pylab_examples/images/sphx_glr_legend_demo4_001.png
:target: ../../gallery/pylab_examples/legend_demo4.html
.. figure:: ../../gallery/text_labels_and_annotations/images/sphx_glr_legend_demo_004.png
:target: ../../gallery/text_labels_and_annotations/legend_demo.html
:align: center
:scale: 50

Expand Down
4 changes: 2 additions & 2 deletions doc/users/prev_whats_new/whats_new_1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ confidence intervals into the :meth:`~matplotlib.axes.boxplot` method. For
every column of data passed to boxplot, the user can specify an accompanying
median and confidence interval.

.. figure:: ../../gallery/pylab_examples/images/sphx_glr_boxplot_demo3_001.png
:target: ../../gallery/pylab_examples/boxplot_demo3.html
.. figure:: ../../gallery/statistics/images/sphx_glr_boxplot_demo_003.png
:target: ../../gallery/statistics/boxplot_demo.html
:align: center
:scale: 50

Expand Down
4 changes: 2 additions & 2 deletions doc/users/prev_whats_new/whats_new_1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ Till Stensitzki added non-zero baselines to
:func:`~matplotlib.pyplot.stackplot`. They may be symmetric or
weighted.

.. figure:: ../../gallery/pylab_examples/images/sphx_glr_stackplot_demo2_001.png
:target: ../../gallery/pylab_examples/stackplot_demo2.html
.. figure:: ../../gallery/pylab_examples/images/sphx_glr_stackplot_demo_001.png
:target: ../../gallery/pylab_examples/stackplot_demo.html
:align: center
:scale: 50

Expand Down
167 changes: 163 additions & 4 deletions examples/images_contours_and_fields/image_demo.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,176 @@
"""
===================
Displaying an image
===================
==========
Image Demo
==========

Many ways to plot images in Matplotlib.

The most common way to plot images in Matplotlib is with
imshow. The following examples demonstrate much of the
functionality of imshow and the many images you can create.

Simple demo of the imshow function.
"""
from __future__ import print_function

import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
from matplotlib.path import Path
from matplotlib.patches import PathPatch

###############################################################################
# First we'll generate a simple bivariate normal distribution.

delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = Z2 - Z1 # difference of Gaussians

im = plt.imshow(Z, interpolation='bilinear', cmap=cm.RdYlGn,
origin='lower', extent=[-3, 3, -3, 3],
vmax=abs(Z).max(), vmin=-abs(Z).max())

plt.show()


###############################################################################
# It is also possible to show images of pictures.

# A sample image
image_file = cbook.get_sample_data('ada.png')
image = plt.imread(image_file)

fig, ax = plt.subplots()
ax.imshow(image)
ax.axis('off') # clear x- and y-axes


# And another image

w, h = 512, 512

datafile = cbook.get_sample_data('ct.raw.gz', asfileobj=True)
s = datafile.read()
A = np.fromstring(s, np.uint16).astype(float).reshape((w, h))
A /= A.max()

extent = (0, 25, 0, 25)
im = plt.imshow(A, cmap=plt.cm.hot, origin='upper', extent=extent)

markers = [(15.9, 14.5), (16.8, 15)]
x, y = zip(*markers)
plt.plot(x, y, 'o')

plt.title('CT density')

plt.show()


###############################################################################
# Interpolating images
# --------------------
#
# It is also possible to interpolate images before displaying them. Be careful,
# as this may manipulate the way your data looks, but it can be helpful for
# achieving the look you want. Below we'll display the same (small) array,
# interpolated with three different interpolation methods.
#
# The center of the pixel at A[i,j] is plotted at i+0.5, i+0.5. If you
# are using interpolation='nearest', the region bounded by (i,j) and
# (i+1,j+1) will have the same color. If you are using interpolation,
# the pixel center will have the same color as it does with nearest, but
# other pixels will be interpolated between the neighboring pixels.
#
# Earlier versions of matplotlib (<0.63) tried to hide the edge effects
# from you by setting the view limits so that they would not be visible.
# A recent bugfix in antigrain, and a new implementation in the
# matplotlib._image module which takes advantage of this fix, no longer
# makes this necessary. To prevent edge effects, when doing
# interpolation, the matplotlib._image module now pads the input array
# with identical pixels around the edge. e.g., if you have a 5x5 array
# with colors a-y as below::
#
# a b c d e
# f g h i j
# k l m n o
# p q r s t
# u v w x y
#
# the _image module creates the padded array,::
#
# a a b c d e e
# a a b c d e e
# f f g h i j j
# k k l m n o o
# p p q r s t t
# o u v w x y y
# o u v w x y y
#
# does the interpolation/resizing, and then extracts the central region.
# This allows you to plot the full range of your array w/o edge effects,
# and for example to layer multiple images of different sizes over one
# another with different interpolation methods - see
# examples/layer_images.py. It also implies a performance hit, as this
# new temporary, padded array must be created. Sophisticated
# interpolation also implies a performance hit, so if you need maximal
# performance or have very large images, interpolation='nearest' is
# suggested.

A = np.random.rand(5, 5)
plt.figure(1)
plt.imshow(A, interpolation='nearest')
plt.grid(True)

plt.figure(2)
plt.imshow(A, interpolation='bilinear')
plt.grid(True)

plt.figure(3)
plt.imshow(A, interpolation='bicubic')
plt.grid(True)

plt.show()


###############################################################################
# You can specify whether images should be plotted with the array origin
# x[0,0] in the upper left or upper right by using the origin parameter.
# You can also control the default be setting image.origin in your
# matplotlibrc file; see http://matplotlib.org/matplotlibrc

x = np.arange(120).reshape((10, 12))

interp = 'bilinear'
fig, axs = plt.subplots(nrows=2, sharex=True, figsize=(3, 5))
axs[0].set_title('blue should be up')
axs[0].imshow(x, origin='upper', interpolation=interp)

axs[1].set_title('blue should be down')
axs[1].imshow(x, origin='lower', interpolation=interp)
plt.show()


###############################################################################
# Finally, we'll show an image using a clip path.

delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = Z2 - Z1 # difference of Gaussians

path = Path([[0, 1], [1, 0], [0, -1], [-1, 0], [0, 1]])
patch = PathPatch(path, facecolor='none')
plt.gca().add_patch(patch)

im = plt.imshow(Z, interpolation='bilinear', cmap=cm.gray,
origin='lower', extent=[-3, 3, -3, 3],
clip_path=patch, clip_on=True)
im.set_clip_path(patch)

plt.show()
135 changes: 0 additions & 135 deletions examples/pylab_examples/annotation_demo.py

This file was deleted.

Loading