From: Jochen V. <vo...@se...> - 2004-11-04 12:05:31
|
Hello, I am still trying to understand how to fix the aspect ration of figures generated with matplotlib. I made some progress but still have questions. In my script (appended to this mail) I do the following: 1) First I set up a figure as f=figure(figsize=(3,2),frameon=False) ax=axes() ax.update_datalim(((-0.5,-0.5),(2.5,1.5))) ax.autoscale_view () My plan is that the lower left corner of the figure corresponds to (-0.5, -0.5) and the upper corner of the figure corresponds to (2.5,1.5) now. The x- and y-coordinate ranges have ratio 3:2 and the figure width and height also have ratio 3:2. Thus I expect the aspect ratio to be 1. Questions: Is the combination of update_datalim and autoscale_view a good way to do this? Is there a more direct way to prescribe the coordinate range of the figure? 2) I try to draw some squares using commands like p = Rectangle((0.1, 0.1), 0.8, 0.8, fill=True, fc=(1,0,0)) ax.add_patch(p) The resulting rectangle looks almost square, but inspecting the PostScript file reveals that it has width 44.64 and height 45.502 PostScript units. This seems to much to be caused by rounding errors. Question: why is the rectangle not square? 3) The pattern of squares I draw is centered in the coordinate range. The resulting picture is not centered in the image window (and neither in the PostScript bounding box). Question: why is the picture not centred? All the best and many thanks in advance, Jochen -- http://seehuhn.de/ |
From: John H. <jdh...@ac...> - 2004-11-04 15:26:14
|
>>>>> "Jochen" == Jochen Voss <vo...@se...> writes: Jochen> Hello, I am still trying to understand how to fix the Jochen> aspect ration of figures generated with matplotlib. I Jochen> made some progress but still have questions. Jochen> In my script (appended to this mail) I do the following: Jochen> 1) First I set up a figure as f=figure(figsize=(3,2),frameon=False) ax=axes() 3 >>> print ax.get_position() [0.125, 0.10999999999999999, 0.77500000000000002, 0.79000000000000004] The width and height are not equal in your axes. Perhaps you want ax = axes([0.1, 0.1, 0.8, 0.8]) ax.update_datalim(((-0.5,-0.5),(2.5,1.5))) I don't think calling update datalim manually is recommended. All you need to is set the view limits, eg with the xlim, ylim or axis commands. The datalim is there to assist the Axes class in autoscaling. Since you know the view limits you want, just set the viewlim yourself, after all the plot commands are issued. Eg axis([-0.5,2.5,-0.5,1.5]) Jochen> My plan is that the lower left corner of the figure Jochen> corresponds to (-0.5, -0.5) and the upper corner of the Jochen> figure corresponds to (2.5,1.5) now. The x- and Jochen> y-coordinate ranges have ratio 3:2 and the figure width Jochen> and height also have ratio 3:2. Thus I expect the aspect Jochen> ratio to be 1. When you say lower-left of the figure, do you really mean the 0,0 coordinate in figure coordinates or the 0,0 coordinates in axes coordinates? I think you mean the latter. If you want you axes to consume the entire figure (no room for ticklabels), use ax = axes([0,0,1,1]) Jochen> Questions: Is the combination of update_datalim and Jochen> autoscale_view a good way to do this? Is there a more Jochen> direct way to prescribe the coordinate range of the Jochen> figure? No, I don't think so, as above. Jochen> 2) I try to draw some squares using commands like Jochen> p = Rectangle((0.1, 0.1), 0.8, 0.8, fill=True, Jochen> fc=(1,0,0)) ax.add_patch(p) Jochen> The resulting rectangle looks almost square, but Jochen> inspecting the PostScript file reveals that it has width Jochen> 44.64 and height 45.502 PostScript units. This seems to Jochen> much to be caused by rounding errors. Jochen> Question: why is the rectangle not square? I think it's the problem mentioned above - your axes width and height are not equal. Does the code below help? from matplotlib.matlab import * from matplotlib.patches import Rectangle f = figure(figsize=(3,2),frameon=False) # ax is 0.8*3 inches wide, 0.8*2inches tall, ie 2.4 x 1.6 ax = axes([0.1, 0.1, 0.8, 0.8]) p = Rectangle((0.1, 0.1), 0.8, 0.8, fill=True, fc=(1,0,0)) ax.add_patch(p) axis([-0.5,2.5,-0.5,1.5]) savefig('test.ps') Jochen> 3) The pattern of squares I draw is centered in the Jochen> coordinate range. The resulting picture is not centered Jochen> in the image window (and neither in the PostScript Jochen> bounding box). Jochen> Question: why is the picture not centred? I'm a little confused here. If there is a problem with centering in the bounding box, you might want to talk to the PS maintainer <wink>. Perhaps with the suggestions I've made above incorporated, you can post an example that replicates your problem. Cheers, JDH |
From: Jochen V. <vo...@se...> - 2004-11-04 16:48:07
|
Hello John, On Thu, Nov 04, 2004 at 09:16:59AM -0600, John Hunter wrote: > 3 >>> print ax.get_position() > [0.125, 0.10999999999999999, 0.77500000000000002, 0.79000000000000004] Oh, ok! This explains why my picture is not centred. 0.125 + 0.775 + 0.1 =3D 1, so the left margin is larger. I guess this is to make space for labels, isn't it? > Does the code below help? >=20 > from matplotlib.matlab import * > from matplotlib.patches import Rectangle >=20 > f =3D figure(figsize=3D(3,2),frameon=3DFalse) >=20 > # ax is 0.8*3 inches wide, 0.8*2inches tall, ie 2.4 x 1.6 > ax =3D axes([0.1, 0.1, 0.8, 0.8]) > p =3D Rectangle((0.1, 0.1), 0.8, 0.8, fill=3DTrue, fc=3D(1,0,0)) > ax.add_patch(p) > axis([-0.5,2.5,-0.5,1.5]) > savefig('test.ps') Yes, it does. Thanks a lot! All the best, Jochen --=20 http://seehuhn.de/ |
From: John H. <jdh...@ac...> - 2004-11-04 17:26:26
|
>>>>> "Jochen" == Jochen Voss <vo...@se...> writes: Jochen> Oh, ok! This explains why my picture is not centred. Jochen> 0.125 + 0.775 + 0.1 = 1, so the left margin is larger. I Jochen> guess this is to make space for labels, isn't it? Well, it's just a reverse engineering of matlab's default axes placement, but your reason is as good as any. JDH |