tfplot.figure

Figure utilities.

tfplot.figure.to_array(fig)[source]

Convert a matplotlib figure fig into a 3D numpy array.

Example

>>> fig, ax = tfplot.subplots(figsize=(4, 4))
>>> # draw whatever, e.g. ax.text(0.5, 0.5, "text")
>>> im = to_array(fig)   # ndarray [288, 288, 4]
Parameters:fig – A matplotlib.figure.Figure object.
Returns:A numpy ndarray of shape (?, ?, 4), containing an RGB-A image of the figure.
tfplot.figure.to_summary(fig, tag)[source]

Convert a matplotlib figure fig into a TensorFlow Summary object that can be directly fed into Summary.FileWriter.

Example

>>> fig, ax = ...    # (as above)
>>> summary = to_summary(fig, tag='MyFigure/image')
>>> type(summary)
tensorflow.core.framework.summary_pb2.Summary
>>> summary_writer.add_summary(summary, global_step=global_step)
Parameters:
  • fig – A matplotlib.figure.Figure object.
  • tag (string) – The tag name of the created summary.
Returns:

A TensorFlow Summary protobuf object containing the plot image as a image summary.

tfplot.figure.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)[source]

Create a figure and a set of subplots, as in pyplot.subplots().

It works almost similar to pyplot.subplots(), but differ from it in that it does not involve any side effect as pyplot does (e.g. modifying thread states such as current figure or current subplot).

(docstrings inherited from matplotlib.pyplot.subplots)

Parameters:
  • ncols (nrows,) – Number of rows/columns of the subplot grid.
  • sharey (sharex,) –

    Controls sharing of properties among x (sharex) or y (sharey) axes:

    • True or ‘all’: x- or y-axis will be shared among all subplots.
    • False or ‘none’: each subplot x- or y-axis will be independent.
    • ’row’: each subplot row will share an x- or y-axis.
    • ’col’: each subplot column will share an x- or y-axis.

    When subplots have a shared x-axis along a column, only the x tick labels of the bottom subplot are created. Similarly, when subplots have a shared y-axis along a row, only the y tick labels of the first column subplot are created. To later turn other subplots’ ticklabels on, use tick_params().

  • squeeze (bool, optional, default: True) –
    • If True, extra dimensions are squeezed out from the returned array of Axes:
      • if only one subplot is constructed (nrows=ncols=1), the resulting single Axes object is returned as a scalar.
      • for Nx1 or 1xM subplots, the returned object is a 1D numpy object array of Axes objects.
      • for NxM, subplots with N>1 and M>1 are returned as a 2D array.
    • If False, no squeezing at all is done: the returned Axes object is always a 2D array containing Axes instances, even if it ends up being 1x1.
  • subplot_kw (dict, optional) – Dict with keywords passed to the add_subplot() call used to create each subplot.
  • gridspec_kw (dict, optional) – Dict with keywords passed to the GridSpec constructor used to create the grid the subplots are placed on.
  • **fig_kw – All additional keyword arguments are passed to the figure() call.
Returns:

  • fig (matplotlib.figure.Figure object)
  • ax (Axes object or array of Axes objects.) – ax can be either a single matplotlib.axes.Axes object or an array of Axes objects if more than one subplot was created. The dimensions of the resulting array can be controlled with the squeeze keyword, see above.

Examples

First create some toy data:

>>> x = np.linspace(0, 2*np.pi, 400)
>>> y = np.sin(x**2)

Creates just a figure and only one subplot

>>> fig, ax = tfplot.subplots()
>>> ax.plot(x, y)
>>> ax.set_title('Simple plot')

Creates two subplots and unpacks the output array immediately

>>> f, (ax1, ax2) = tfplot.subplots(1, 2, sharey=True)
>>> ax1.plot(x, y)
>>> ax1.set_title('Sharing Y axis')
>>> ax2.scatter(x, y)

Creates four polar axes, and accesses them through the returned array

>>> fig, axes = tfplot.subplots(2, 2, subplot_kw=dict(polar=True))
>>> axes[0, 0].plot(x, y)
>>> axes[1, 1].scatter(x, y)

Share a X axis with each column of subplots

>>> tfplot.subplots(2, 2, sharex='col')

Share a Y axis with each row of subplots

>>> tfplot.subplots(2, 2, sharey='row')

Share both X and Y axes with all subplots

>>> tfplot.subplots(2, 2, sharex='all', sharey='all')

Note that this is the same as

>>> tfplot.subplots(2, 2, sharex=True, sharey=True)

See also

figure(), subplot()