TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Tips and tricks to write LaTeX papers in with figures generated in Python

183 pointsby Wookaiabout 6 years ago

19 comments

akshaynabout 6 years ago
&gt; We also recommend to save the command used to generate a figure in the LaTeX file<p>An approach I have adopted recently is Knitr[1], so this layer of indirection goes away. With knitr, my data goes directly into the paper repository, and then my Makefile has something like this:<p><pre><code> %.tex: graphs&#x2F;%.Rnw Rscript -e &quot;library(knitr); knit(&#x27;$?&#x27;)&quot; </code></pre> The nice thing is exactly what the authors recommend: it&#x27;s much easier to enforce a standard appearance across all the figures, and automatically incorporate more recent data into the paper as part of the compilation process.<p>[1] <a href="https:&#x2F;&#x2F;yihui.name&#x2F;knitr&#x2F;" rel="nofollow">https:&#x2F;&#x2F;yihui.name&#x2F;knitr&#x2F;</a>
评论 #19426757 未加载
abhghabout 6 years ago
I&#x27;d also add that for figures Inkscape is invaluable [1]. Save as svg once, and export it as whatever later. I typically export it to PDF (from within Inkscape) for pdflatex.<p>While its typically indispensable for schematics, I often seem to run into the use case of combining previously generated plots or figures, or adding a label&#x2F;text. Since Inkscape can import pngs, this is a breeze with it. I don&#x27;t have to go back to the original code to regenerate plots, or fiddle around with latex to make minor adjustments.<p>For stuff generated via matplotlib, I&#x27;d strongly recommend seaborn as an additional library [2]. This is a wrapper over matplotlib. It can prettify plots with just an import and a &#x27;set&#x27; command. You can, of course, use it to plot too, and for stuff doable in matplotlib using the seaborn alternative is much easier and looks better with little or no work. And they support pandas dataframes.<p>[1] <a href="https:&#x2F;&#x2F;inkscape.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;inkscape.org&#x2F;</a><p>[2] <a href="https:&#x2F;&#x2F;seaborn.pydata.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;seaborn.pydata.org&#x2F;</a>
评论 #19428593 未加载
评论 #19429371 未加载
评论 #19427855 未加载
Jill_the_Pillabout 6 years ago
Having just completed a dissertation in LaTex, with figures online in Overleaf and Dropbox (some of them screenshots), scripts and data spread across two computers and an external hard drive, desperate last minute plot text changes right in the pdf, I just have to ask: WHY DIDN&quot;T YOU POST ALL THIS SOONER?
评论 #19427407 未加载
sigurdjsabout 6 years ago
If you are serious about making beautiful figures in latex, I would seriously recommend using tikz and pgf-plots. It is quite easy to automatically generate tikz-code from python (after all it is supposed to be read and written by humans) and all aspects of the figure can easily be customized. I have been quite successful in generating automated reports with pretty and easily readable figures using tikz and pgf.<p>If anyone is interested I have uploaded a sample script for generating XY-plots from two numpy lists to github. The code is by no means very good, but I just wanted to share in case anyone wants to try this approach.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;sigurdjs&#x2F;python-tikz" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sigurdjs&#x2F;python-tikz</a>
评论 #19429736 未加载
评论 #19431837 未加载
jedbergabout 6 years ago
&gt; When writing LaTeX documents, put one sentence per line in your source file.<p>An interesting tip, never thought of that! It changes the way you write a bit, but it does make finding changes easier, finding errors easier, and forces you to think more about each sentence since you have to hit &quot;enter&quot; at the end of each one.
评论 #19427010 未加载
评论 #19426769 未加载
评论 #19426636 未加载
bonoboTPabout 6 years ago
I find it useful to work with plots in Jupyter notebooks. Use the &quot;%matplotlib notebook&quot; cell magic to get interactive plots inline.<p>Then you can use savefig when it looks good. Then save the code you used into some file near the Latex sources.
评论 #19427357 未加载
mlthoughts2018about 6 years ago
I also recommend separating repetitive parts of plot generating code into template files, such as with mako or jinja2, and then programmatically generate sequences of plots by first piping the data into the jinja2 template, and then using insert commands to insert it into a bigger tex document.<p>I found this helpful when writing a paper where the appendix needed over 35 different tables of regression results, all with the same format but populated with data from different subpopulations, which would need to be regenerated (including updated captions, etc.) any time data cleaning or methodology was changed.
评论 #19426790 未加载
unwindabout 6 years ago
Meta: there seems to be an extra &quot;in&quot; in the title, that makes no sense to me, at least.<p>Not a native speaker, though.
评论 #19426749 未加载
评论 #19426216 未加载
euskeabout 6 years ago
Re: figures in EPS. I think SVG is the way to go. It can be generated with matplotlib or even a simpler script (it&#x27;s just an XML after all). It can be hand edited. It&#x27;s viewable with a browser. And it can be converted to PDF with rsvg-convert.<p>I personally find matplotlib a bit unintuitive to use, so I made a 100-line script for generating SVG. It&#x27;s great.
knolanabout 6 years ago
This is probably most useful for postgrad students getting started with writing with TeX.<p>It’s worth pointing out that the figures are made using the matplotlib library, which is primarily based on Matlab’s plotting functionality. This is perhaps just as useful for new researchers as many of them are taught Matlab exclusively throughout their undergraduate courses.
评论 #19428335 未加载
jonathanpoulterabout 6 years ago
A minor plug: I&#x27;ve found I generate graphs and tables in Jupyter notebooks, so I wrote ipynb-tex, to allow you to reference cells from a notebook directly in your LaTeX documents. This supports tables, and figures.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;poulter7&#x2F;ipynb-tex" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;poulter7&#x2F;ipynb-tex</a>
mycheleabout 6 years ago
I would suggest checking matplotlib2tikz and matlab2tikz to get pgfplot&#x2F;tikz figures from matplotlib and matlab plots
评论 #19427992 未加载
semi-extrinsicabout 6 years ago
One itch which (curiously) I can&#x27;t seem to quite scratch in LaTeX is that it should be possible to say &quot;plot equation \ref{eq:smth} for X in (-4,4)&quot; and just get the bloody graph. Why should I need to define the equation again in a separate place, perhaps even in a separate file?
评论 #19426827 未加载
评论 #19427657 未加载
评论 #19426542 未加载
评论 #19428124 未加载
joseph8thabout 6 years ago
Any opinion on the utility of Emacs Org-mode to organize and manage LaTeX? In particular Org Babel?
评论 #19428322 未加载
tapiaabout 6 years ago
I already implement most of the points mentioned there. The most useful (and new) tip for me was however the rasterization part. I normally like to have pdf figures for my LaTeX papers, but last time I had some graphics with some thousands of points plotted, which were taking too long to be printed if you did that from windows (in Linux there was no problem, that&#x27;s why I didn&#x27;t catch the problem earlier). At the end I decided to save the plot as png, but was not happy about it haha. It would have been good to know the rasterization trick earlier.
评论 #19431852 未加载
Wookaiabout 6 years ago
Thanks all for the great feedback and discussion, I&#x27;ll update this thread once I push an update. If you&#x27;re interested, there was a great discussion on &#x2F;r&#x2F;MachineLearing as well: <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;MachineLearning&#x2F;comments&#x2F;b2oiaj&#x2F;d_best_practice_and_tips_tricks_to_write&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;MachineLearning&#x2F;comments&#x2F;b2oiaj&#x2F;d_b...</a>
stilley2about 6 years ago
Thanks for the write-up! Two notes from my experience: pgf output works well with latex as well (although will slowdown compilation), and I recommend not using the pyplot submodule, especially if you&#x27;ll be running things remotely over ssh and don&#x27;t have a display
评论 #19426143 未加载
评论 #19426803 未加载
billfruitabout 6 years ago
Is there a better and more comprehensive plotting library than Matplotlib, it&#x27;s 3D plots a lack polish. Also it is kind of verbose and require much boilerplate. Its api is sprawling and hard to remember.
评论 #19430089 未加载
评论 #19429680 未加载
musicaleabout 6 years ago
I have one tip for anyone using LaTeX:<p>Please stop using the awful Computer Modern typeface.
评论 #19426918 未加载
评论 #19426802 未加载
评论 #19426753 未加载
评论 #19430017 未加载