python - set_aspect() and coordinate transforms in matplotlib -
i run seems bug in matplotlib (version 1.4.3
)/pyplot when attempting draw line between subplots: after setting set_aspect("equal")
, appears relevant coordinate transformation functions (transdata
) don't update. execute code below ax.set_aspect("equal")
uncommented see difference.
import matplotlib.pyplot plt import matplotlib mpl f, (ax1, ax2) = plt.subplots(1, 2, sharey='all', sharex='all') ax in (ax1, ax2): # ax.set_aspect("equal") ax.set_ylim([-.2, 1.2]) ax.set_xlim([-.2, 1.2]) # http://stackoverflow.com/questions/17543359/drawing-lines-between-two-plots-in-matplotlib transfigure = f.transfigure.inverted() coord1 = transfigure.transform(ax1.transdata.transform([0,0])) coord2 = transfigure.transform(ax2.transdata.transform([1,0])) line = mpl.lines.line2d((coord1[0],coord2[0]),(coord1[1],coord2[1]), transform=f.transfigure) f.lines.append(line) plt.show()
the problem you're having set_aspect
isn't applied until after draw operation. when you're making line, limits haven't been changed. notice different x limits in second image, while line in same place: line drawn though x limits didn't change, because hadn't been changed yet, , wouldn't changed until plt.show()
. solution add plt.draw()
after set_aspect
, before start working on transforms. following code this, print statements make clear issue limits , transforms @ different times:
import matplotlib.pyplot plt import matplotlib mpl f, (ax1, ax2) = plt.subplots(1, 2, sharey='all', sharex='all') ax in (ax1, ax2): ax.set_ylim([-.2, 1.2]) ax.set_xlim([-0.2, 1.2]) transfigure = f.transfigure.inverted() print ax1.get_xlim(), ax1.transdata.transform([0,0]) ax in (ax1, ax2): ax.set_aspect('equal') print ax1.get_xlim(), ax1.transdata.transform([0,0]) plt.draw() print ax1.get_xlim(), ax1.transdata.transform([0,0]) coord1 = transfigure.transform(ax1.transdata.transform([0,0])) coord2 = transfigure.transform(ax2.transdata.transform([1,0])) line = mpl.lines.line2d((coord1[0],coord2[0]),(coord1[1],coord2[1]), transform=f.transfigure) f.lines.append(line) plt.show()
this point should added docstring set_aspect, , i'll see if can that. it's not bug: aspect can't determined until after plot ready drawn.
Comments
Post a Comment