Ask Your Question

contour_plot and figsize issue

asked 2011-12-16 01:26:56 -0600

Eugene gravatar image


I am trying to make contour_plot of function and then get result image with custom size using show and keywork figsize.

The strange thing: this code actually ignores figsize keyword:

x,y = var('x,y')
contour_plot(cos(x^2+y^2), (x, -4, 4), (y, -4, 4)).show(xmin=-4,xmax=4,ymin=-4,ymax=4,figsize=(10,6))

But this one respects:

(point((0,0))+contour_plot(cos(x^2+y^2), (x, -4, 4), (y, -4, 4))).show(xmin=-4,xmax=4,ymin=-4,ymax=4,figsize=(8,2))

How to set figsize for contour_plot properly?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2011-12-16 02:34:15 -0600

Jason Grout gravatar image

updated 2011-12-16 02:41:10 -0600

By default, the contour plot has an aspect ratio of 1. To instead fill up the entire figure, set the aspect ratio to 'automatic'. This is what is happening in your second example: the aspect ratio of the entire figure is getting changed by the point graphic.

x,y = var('x,y')
contour_plot(cos(x^2+y^2), (x, -4, 4), (y, -4, 4)).show(xmin=-4,xmax=4,ymin=-4,ymax=4,figsize=(10,6),aspect_ratio='automatic')

Here's a more detailed answer about why the actual image size was getting cropped. By default, the image is created at the proper figure size, but then is cropped to the actual size of the plot within the figure (i.e., the image returned is "tightly" cropped around the graphic). You can turn off this cropping by setting fig_tight=False. This is documented in the show options. In the example below, the aspect ratio is not changed, but the resulting image file is still the requested size.

x,y = var('x,y')
contour_plot(cos(x^2+y^2), (x, -4, 4), (y, -4, 4)).show(xmin=-4,xmax=4,ymin=-4,ymax=4,figsize=(10,6),fig_tight=False)

If you want the contour plot to fill the figure, and still want an image that is exactly the right size, you'll need to change the aspect ratio to 'automatic' and use the fig_tight parameter.

x,y = var('x,y')
contour_plot(cos(x^2+y^2), (x, -4, 4), (y, -4, 4)).show(xmin=-4,xmax=4,ymin=-4,ymax=4,figsize=(10,6),fig_tight=False,aspect_ratio='automatic')
edit flag offensive delete link more


Thank you for such a good and full answer! The problem is solved.

Eugene gravatar imageEugene ( 2011-12-16 04:41:05 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-12-16 01:26:56 -0600

Seen: 93 times

Last updated: Dec 16 '11