Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

figsize does not work when exporting sagews plots to PDF #114

Closed
rolandog opened this issue Sep 15, 2015 · 6 comments
Closed

figsize does not work when exporting sagews plots to PDF #114

rolandog opened this issue Sep 15, 2015 · 6 comments

Comments

@rolandog
Copy link

Hi. I've been trying to export to PDF a Sage WorkSheet that has plots.

When exporting a Notebook to PDF, all plots are scaled to the width of the page (even if there is some vertical overflow when a narrow chart is plotted); I had read in the documentation that the default figsize is 4, so I figured since the charts all seem to occupy the width of the page, that a more reasonable size like 2 would occupy half as much. However, I haven't been able to make it work, and I think that this is a bug.

I tried prepending or appending sage.plot.graphics.Graphics.SHOW_OPTIONS['figsize'] = 2 as was specified in stack overflow about this very same issue, but the results were the same (as was also reported back then). The issue seems to be reported in track ticket #16212, but only as a lack of documentation and not as a problem in the behavior of the PDF export outcome.

Different examples, but same results:

x = var('x')
y_1 = x
y_2 = x^2
the_plot = plot(y_1, (x, 0, 1), fill = y_2, aspect_ratio = 1) + plot(y_2, (x, 0, 1))
the_plot.show(figsize = [2, 2])

I also tried different approaches, such as not using the show() method:

x = var('x')
y_1 = x
y_2 = x^2
the_plot = plot([y_1, y_2], (x, 0, 1), fill = y_2, aspect_ratio = 1, figsize = [2, 2])
the_plot

Or, even omitting the aspect_ratio attribute:

x = var('x')
y_1 = x
y_2 = x^2
the_plot = plot([y_1, y_2], (x, 0, 1), fill = y_2, figsize = [2, 2])
the_plot

The issue I encountered was not shown when displaying the equation in the Notebook in SageCloud (those were displayed properly, and scaled accordingly).

Any workarounds would be greatly appreciated, since I haven't been able to find any documentation to help me out, so far.

P.S. This may not be a bug in itself, as William suggested elsewhere... but I have a hypothesis that it may become a bug when plots have a width/height ratio lower than 0.72 or 0.77 (as discussed here); I will do more testing.

P.P.S. As I also discussed with William, I'll try to implement a fix and ask for help/guidance when needed.

@williamstein
Copy link
Contributor

I will add you to this project as soon as I get your email (or name):

https://cloud.sagemath.com/projects/1e3b2dde-f9eb-4548-9fe6-a9443c655050/

Then I'll show you which Python file to edit, where in that file, and how to test the result. Then you can research solutions (by googling around and so on) until you figure something out. We can add anybody else to the above project who wants to help.

@williamstein
Copy link
Contributor

Roland open the above project. Then look at the file README.md that I'm about to write.

@rolandog
Copy link
Author

Thanks Will. Sorry to take so much to get back at you, had some pressing matter at work. I'll start looking at the link!

@rolandog
Copy link
Author

Will, thanks for your help! I tinkered with line 305 of sagews2pdf.py as you suggested. I changed it from:

s += '\\includegraphics[width=\\textwidth]{%s}\n'%img

to:

# omitting [width=\\textwidth] allows figsize to set displayed size
s += '{\\centering\n\\includegraphics{%s}\n\\par\n}\n'%img

This works and preserves centering, as it is expected, by using:

{\centering
  contents\par
}

The consequences of this are as follows:

Before

  • With s += '\\includegraphics[width=\\textwidth]{%s}\n'%img:
    • When a plot with an aspect ratio (not the aspect_ratio attribute) lower than ~ 0.6 (width / height) was included, the plot would occupy an area taller than the page's height; this could not be controlled by the figsize attribute in either the plot() function or the .show() method.

After

  • With s += '{\\centering\n\\includegraphics{%s}\n\\par\n}\n'%img:
    • Plot images are scaled as set by the figsize attribute, center-aligned and displayed in a paragraph of their own.
    • To achieve the same page-wide plot, a width of 6, and a height no larger than around 8.5 should be used: figsize = [6, 8] yields a near full-page plot.

I've still got to play with 3D plots and see if the issue is present there; I doubt it since in line 326 seems to include s += '\\includegraphics[width=%s\\textwidth]{%s}\n'%(width, filename), which suggests that there is an included width factor that multiplies \textwidth to scale the drawing.

Edit:

  1. If this change is applied, the 2D Plotting and Graphics objects documentation may need to be updated, since both mention that figsize defaults are 4 and [8, 6], respectively.

@williamstein
Copy link
Contributor

(easy since basically done)

@williamstein
Copy link
Contributor

Umh, this was super easy -- should have done this long ago!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants