Matplotlib Latex

Matplotlib Latex




🛑 ALL INFORMATION CLICK HERE 👈🏻👈🏻👈🏻

































Matplotlib Latex


Contents »

Matplotlib »

Typesetting »

Matplotlib: latex examples


Github
Download





\ documentclass [ prl , 10 pt , twocolumn ]{ revtex4 }
\ usepackage { graphicx } % Used to import the graphics
\ begin { document }
%...
\ begin { figure }[ t ]
\ begin { center }
\ showthe \ columnwidth % Use this to determine the width of the figure .
\ includegraphics [ width = \ columnwidth ]{ fig1 . eps }
\ caption { \ label { fig : sin_cos } Plot of the sine and cosine functions . }
\ end { center }
\ end { figure }
%...
\ end { document }

This is TeX , Version 3.14159 ( Web2C 7.4 . 5 )
LaTeX2e < 2001 / 06 / 01 >
...
> 246.0 pt .
l . 8 \ showthe \ columnwidth
% Use this to determine the width of the figure .
?
< fig1 . eps > [ 1 ] ( ./ tst . aux ) )
...

#!python numbers=disable
fig_width_pt = 246.0 # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0 / 72.27 # Convert pt to inches
golden_mean = ( sqrt ( 5 ) - 1.0 ) / 2.0 # Aesthetic ratio
fig_width = fig_width_pt * inches_per_pt # width in inches
fig_height = fig_width * golden_mean # height in inches
fig_size = [ fig_width , fig_height ]

#!python numbers=disable
'font.size' : 10 ,
'axes.labelsize' : 10 ,
'font.size' : 10 ,
'text.fontsize' : 10 ,
'legend.fontsize' : 10 ,
'xtick.labelsize' : 8 ,
'ytick.labelsize' : 8 ,

#!python numbers=disable
pylab . axes ([ 0.125 , 0.2 , 0.95 - 0.125 , 0.95 - 0.2 ])

#!python
import pylab
from pylab import arange , pi , sin , cos , sqrt
fig_width_pt = 246.0 # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0 / 72.27 # Convert pt to inch
golden_mean = ( sqrt ( 5 ) - 1.0 ) / 2.0 # Aesthetic ratio
fig_width = fig_width_pt * inches_per_pt # width in inches
fig_height = fig_width * golden_mean # height in inches
fig_size = [ fig_width , fig_height ]
params = { 'backend' : 'ps' ,
'axes.labelsize' : 10 ,
'text.fontsize' : 10 ,
'legend.fontsize' : 10 ,
'xtick.labelsize' : 8 ,
'ytick.labelsize' : 8 ,
'text.usetex' : True ,
'figure.figsize' : fig_size }
pylab . rcParams . update ( params )
# Generate data
x = pylab . arange ( - 2 * pi , 2 * pi , 0.01 )
y1 = sin ( x )
y2 = cos ( x )
# Plot data
pylab . figure ( 1 )
pylab . clf ()
pylab . axes ([ 0.125 , 0.2 , 0.95 - 0.125 , 0.95 - 0.2 ])
pylab . plot ( x , y1 , 'g:' , label = '$\sin(x)$' )
pylab . plot ( x , y2 , '-b' , label = '$\cos(x)$' )
pylab . xlabel ( '$x$ (radians)' )
pylab . ylabel ( '$y$' )
pylab . legend ()
pylab . savefig ( 'fig1.eps' )

\ documentclass [ prl , 10 pt , twocolumn ]{ revtex4 }
\ usepackage { graphicx } % Used to import the graphics
\ usepackage { psfrag }
\ begin { document }
%...
\ begin { figure }[ t ]
\ begin { center }
\ psfrag { sin ( x )}{ $ \ sin ( x ) $ }
\ psfrag { cos ( x )}{ $ \ cos ( x ) $ }
\ psfrag { x ( radians )}{ $ x $ ( radians )}
\ psfrag { y }{ $ y $ }
{ \ footnotesize % Replace tick - lables with smaller font .
\ psfrag { 1.0 }{ 1.0 }
\ psfrag { 0.5 }{ 0.5 }
\ psfrag { 0.0 }{ 0.0 }
\ psfrag { - 0.5 }{ - 0.5 }
\ psfrag { - 1.0 }{ - 1.0 }
\ psfrag { - 8 }{ - 8 }
\ psfrag { - 6 }{ - 6 }
\ psfrag { - 4 }{ - 4 }
\ psfrag { - 2 }{ - 2 }
\ psfrag { 0 }{ 0 }
\ psfrag { 2 }{ 2 }
\ psfrag { 4 }{ 4 }
\ psfrag { 6 }{ 6 }
\ psfrag { 8 }{ 8 }
\ showthe \ columnwidth % Use this to determine the width of the figure .
\ includegraphics [ width = \ columnwidth ]{ fig2 . eps }
} % Note that the psfrag commands only work in the top - most environment .
\ caption { \ label { fig : sin_cos } Plot of the sine and cosine functions . }
\ end { center }
\ end { figure }
%...
\ end { document }

#!python
import pylab
from pylab import arange , pi , sin , cos , sqrt
fig_width_pt = 246.0 # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0 / 72.27 # Convert pt to inch
golden_mean = ( sqrt ( 5 ) - 1.0 ) / 2.0 # Aesthetic ratio
fig_width = fig_width_pt * inches_per_pt # width in inches
fig_height = fig_width * golden_mean # height in inches
fig_size = [ fig_width , fig_height ]
params = { 'backend' : 'ps' ,
'axes.labelsize' : 10 ,
'text.fontsize' : 10 ,
'legend.fontsize' : 10 ,
'xtick.labelsize' : 8 ,
'ytick.labelsize' : 8 ,
'text.usetex' : False ,
'figure.figsize' : fig_size }
pylab . rcParams . update ( params )
# Generate data
x = pylab . arange ( - 2 * pi , 2 * pi , 0.01 )
y1 = sin ( x )
y2 = cos ( x )
# Plot data
# Plot data
pylab . figure ( 1 )
pylab . clf ()
pylab . axes ([ 0.125 , 0.2 , 0.95 - 0.125 , 0.95 - 0.2 ])
pylab . plot ( x , y1 , 'g:' , label = 'sin(x)' )
pylab . plot ( x , y2 , '-b' , label = 'cos(x)' )
pylab . xlabel ( 'x (radians)' )
pylab . ylabel ( 'y' )
pylab . legend ()
pylab . savefig ( 'fig2.eps' )

from matplotlib.font_manager import fontManager , FontProperties
font = FontProperties ( size = 'x-small' );
pylab . legend ( loc = 0 , prop = font );

#!python
from pylab import meshgrid , sin , cos , linspace , contourf , savefig , clf
x , y = meshgrid ( * ( linspace ( - 1 , 1 , 500 ),) * 2 )
z = sin ( 20 * x ** 2 ) * cos ( 30 * y )
c = contourf ( x , y , z , 50 )
savefig ( 'full_vector.pdf' )
clf ()
c = contourf ( x , y , z , 50 , rasterized = True )
savefig ( 'rasterized.pdf' )



Versions

latest



Downloads

html



On Read the Docs

Project Home


Builds



Free document hosting provided by Read the Docs .


This page describes several ways to produce publication quality graphics
with LaTeX.
This section describes a technique following the
["Cookbook/Matplotlib/UsingTex"] guidelines.
Here is the outline of the LaTeX file used to include the figure
(example for `REVTeX4` for publication is APS physics journals with a
two column format.)
The first step is to determine the size of the figure: this way, when
the graphic is included, it will not be resized, and the fonts etc. will
be exactly as you set them rather than scaled (and possibly distored).
This can be done in LaTeX by explicitly setting the width of the figure
and using the `\showthe` command to print this width. (In the example
above, the figure width is set to the `\columnwidth`.)
When the file is processed by LaTeX, look at the output. The example
above produces the following output (Note: LaTeX will pause after the
`\showthe` command, press enter to continue):
Thus, the figure will be 246.0pt wide. There are 1 inch = 72.27pt (in
[La]TeX), so this means that the figure width should be 3.40390 inches.
The heigth depends on the content of the figure, but the golden mean may
be used to make a pleasing figure. Once this is determined, the
`figure.figsize` property can be used to set the default figure size.
Since the figure will not be scaled down, we may explicitly set the font
sizes.
With these smaller plot sizes, the default margins are not enough to
display the axis labels, so we need to specify large margins. We do this
with an explicit call to the `axes()` function. In this example, we
only have one axis. The typeset LaTeX document will have whitespace on
either side of the figure, so we do not need to include this in the
figure. Thus, we keep just a bit of whitespace at the top and to the
right so that the labels do not extend beyond the bounding box, and add
more space to the bottom for the x label:
Here is the python file that generates the plots.
An obvious solution is to greyscale convert your figure, but for readibility, adding dashes is often better.. This maybe implemented for an example of a SigmoidalFunctions with

Note: This section is obsolete. Recent versions matplotlib break the
psfrag functionality (see for example this
discussion .
That being said, one can use the usetex feature to render the LaTeX text
directly with very good results (if you are careful about choosing
fonts). I will try to discuss this here further in the near future. --
MichaelMcNeilForbes
To ensure that your graphics use exactly the same fonts as your
document, you can have LaTeX generate and substitute the text for your
graph using the psfrag package. This is a good option if you have
problems with the `text.usetex` method (for example, if the
appropriate fonts cannot be found.)
To do this, simply use plain text for the labels and then replace them
using the psfrag package. Here are the modified files to make use of
this method:
Another way to set the legend fonts after the legend has been drawn is
to use, for example:
If you have very complex images such as high-resolution contour plots or
three-dimensional plots, saving these to vector graphic formats like PDF
or EPS can result in unacceptably large files (though with the ability
for stunning zooms). One solution is to selectively convert parts of the
plot (not the text labels) to a rasterized image. This can be done with
the so-called "mixed-mode rendering" capability in recent versions of
matplotlib. Here is an example as it should probably work:
however, {{{contourf}}} currently does not support the {{{rasterized}}} option (which is silently ignored). Some other plot elements do however. I am looking into a solution for this. -- MichaelMcNeilForbes
Section author: MichaelMcNeilForbes, Unknown[100], GaelVaroquaux, Unknown[101], EmmanuelleGouillart, Unknown[102], LaurentPerrinet, Unknown[8], Christian Gagnon

© Copyright 2015, Various authors


Revision 5e2833af .




2017-07-13 (last modified), 2006-05-11 (created)


Sign up or log in to customize your list.

more stack exchange communities

company blog


Stack Overflow for Teams
– Start collaborating and sharing organizational knowledge.



Create a free Team
Why Teams?



Asked
7 years, 4 months ago


Modified
7 years, 3 months ago


1,042 1 1 gold badge 17 17 silver badges 31 31 bronze badges



Sorted by:


Reset to default





Highest score (default)


Trending (recent votes count more)


Date modified (newest first)


Date created (oldest first)




2,421 13 13 silver badges 32 32 bronze badges


Stack Overflow

Questions
Help



Products

Teams
Advertising
Collectives
Talent



Company

About
Press
Work Here
Legal
Privacy Policy
Terms of Service
Contact Us
Cookie Settings
Cookie Policy



Stack Exchange Network



Technology




Culture & recreation




Life & arts




Science




Professional




Business





API





Data






Accept all cookies



Customize settings


Find centralized, trusted content and collaborate around the technologies you use most.
Connect and share knowledge within a single location that is structured and easy to search.
I try to make a table with a title in Python using LaTeX.
The following code creates a figure with a title, as I want the title to be.
But now I add two lines to the upper code which make the title wrong.
What can I do to use LaTeX fonts and usual python fonts at the same time? Of course, without modifying the strings s1 and s2.
All you need to do is add a few lines to your file:
To get the a_b_c working like you wanted, you need to do:
Or the b and c will be at the same level.
Thanks for contributing an answer to Stack Overflow!

By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2022.9.9.42970


By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .



Zürich-based Software Engineer with Google; opinions are my own. I am interested in data science, software engineering, 3d-printing, arts, music, microcontrollers, and sports.
There is an email newsletter available. Once signed up, you will receive an email every time a new post is published. That is usually about once or twice a month.

Proudly powered by WordPress
|
Theme: Sin by vsFish .
Cookies This site uses cookies. More Okay

Science, Programming, 3D-Printing, Music, Philosophy, and More…
This blog post explains how plots from matplotlib can be exported to PGF files, which in turn can be imported into L A T E X documents. All figures in this post are links that point to the source code used to generate them.
Skip this part if you are already motivated, i.e. you know why you would want to export from matplotlib to LaTeX.
Suppose you have a Python script that produces a nice plot using matplotlib. You might save the plot with
and it will write a PNG file to the disk, which is ~141 KB in size (in the example).
However, importing this file as an image into a LaTeX document has some drawbacks. Most notably that
The solution to all four problems is exporting a PGF file from matplotlib, so LaTeX itself can take care of rendering the figure.
Adjust your matplotlib script by adding the following lines after import matplotlib :
Update the save command to write to .pgf
and make sure LaTeX is installed on the system. The command pdflatex is needed. On Mac brew cask install mactex does the job.
Running the script will output the histogram file in PGF format which can be imported with LaTeX.
Place the PGF file in the LaTeX project folder and import it with the following code:
The figure will appear in the document.
The PGF figure might be scaled undesirably, e.g. it can be too wide. In that case I found it helpful to determine the text width \textwidth of the document with the command
and adjust the matplotlib figure based on that:
Scaling with matplotlib is better than scaling the entire PGF figure because the font sizes remain the same and match the text in the document.
Nice trick! I had to include package pgf in LaTeX, but apart from that it worked like a charm.
Good point, I should probably extend the post by that. Did you use \usepackage{pgfplots} ?
Yeah, pgfplots worked for me. I was getting “! LaTeX Error: File `pgfpicture.sty’ not found.” before I included it.
This instruction helped me a lot to export plots to my LaTeX report. Unfortunately, if I configure matplotlib to generate PGF files they are not shown in the report. If I disable matplotlib to generate PGF files they ara shown but PGF files are not generated. How can I have both?
That’s a very good question, I’m afraid I do not know the answer to it. If you happen to find a solution please share it here!
Thanks Timo! Windows users should install a tex distribution in order to compile plt.savefig('*.pgf') . MiKTeX worked for me.
Thanks for pointing that out! Have a great day!
Note that this will not work for all plot types. For example, using pcolormesh (even with rasterize=True ) will result in a NotImplementedError .
How do I install Tex so that Python can access it on Mac OS? I am using Anaconda, and am having tremendous trouble in getting Latex installed on the system.
It should be independent of Anaconda. Maybe this answer on tex.stackexchange.com helps.
How could I align the pgf plot to the left in LaTex? It sh
Nikki Blond Anal
New Public Porn
Goth Tranny Femboy

Report Page