0

I would like to arrange a number oh hypertools plots into a grid of matplotlib sublots. Normally hypertools.plot renders immediately, but that can be stopped by passing show=False. It also returns ahypertools.DataGeometry` object. How can I get it to render as a subplot in a grid rather than a standalone figure?

Daniel Mahler
  • 7,653
  • 5
  • 51
  • 90

1 Answers1

1

The usual way to write some plotting wrapper for matplotlib would be to allow for an axes to be supplied to the wrapper, e.g.

def plot(data, ax=None):
    if not ax:
        fig, ax = plt.subplots()
    else:
        fig = ax.figure
    # plot to ax ...

hypertools does not follow this standard approach, which would make it very cumbersome to get one of those plots into an existing figure. It would probably be worthwhile putting this as an issue on their GitHub tracker.


The option you have is to move the axes from the figure created by hypertools to your own figure.

This could be done using the approach from this answer. (I cannot test the following, since I do not have hypertools available)

import matplotlib.pyplot as plt
import hypertools

datageom = hypertools.plot(..., show=False)

ax = datageom.ax
ax.remove()

fig2 = plt.figure()
ax.figure=fig2
fig2.axes.append(ax)
fig2.add_axes(ax)

dummy = fig2.add_subplot(231)
ax.set_position(dummy.get_position())
dummy.remove()
# possibly:
# plt.close(datageom.fig)

plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712