11

I'm using IPython in pylab mode (all functions at fingertip), and wanted to annotate certain plot, lets say plot([1,3,2]) with rectangle Rectangle((1,1),1,1)

How can I draw a simple rectangle in this pylab mode, that is without using figure, axes, subplots... but reference just created plot in easiest possible way

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
vlad
  • 771
  • 2
  • 10
  • 21

1 Answers1

18

in this pylab mode, that is without using figure, axes, subplots

Figues, axes, and subplots exist in the pylab framework too. If I were using the pylab interface, I'd simply throw a subplot(111) in there and then use sp.add_patch(Rectangle(etc)). But you can also grab the current axes/figure using gca() and gcf():

>>> from pylab import *
>>> plot([1,3,2])
[<matplotlib.lines.Line2D object at 0x102bc8950>]
>>> gca()
<matplotlib.axes.AxesSubplot object at 0x102790cd0>
>>> gca().add_patch(Rectangle((1,1),1,1))
<matplotlib.patches.Rectangle object at 0x102790510>
>>> savefig("rect.png")

line with rectangle

The pylab approach is simple enough for very basic tasks, but doesn't scale as well up to more complex ones.

DSM
  • 342,061
  • 65
  • 592
  • 494