pypgfplots

GitHub Repository

PDFs are generated by running pdflatex on TeX files produced by python script (on left). On right, are the PNGs generated from PDF by following command.

convert -density 300 -quality 90 filename.pdf filename.png 

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import sys
import pypgfplots 
import numpy as np

np.random.seed( 0 )

x = np.arange( 0, 1, 0.1 )
y1 = np.random.randint( 0, 11, len(x) )
y2 = np.random.randint( 0, 11, len(x) )

# There are two y-series on one y-axis. User should make sure that y1 and y2 can
# be plotted together.
pypgfplots.standalone( (x, y1, y2 )
        , outfile = '%s.tex' % sys.argv[0] 
        , xlabel = 'Index', ylabel = '$\frac{a}{b}$'
        , title = "Plot with Index."
        , legend = [ "Series A", "Series B" ]
        , axis_attrib = 'legend pos=outer north east'
        , label = r'\bf a.'
        )
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Having multiple y-axis with 1 x-axis not very well tested. This is a corner
# case and should be avoided.
import sys
import pypgfplots 
import numpy as np

np.random.seed( 1 )

x = np.arange( 0, 1, 0.1 )
y1 = np.random.randint( 0, 11, len(x) )
y2 = np.random.randint( 11, 101, len(x) )

# There are two y-series on two different y-axis. One will be on left and
# another will be on right.
# be plotted together.
pypgfplots.standalone( (x, y1), (x, y2)
        , outfile = '%s.tex' % sys.argv[0] 
        , xlabel = 'Time'
        , color = [ 'red', 'blue' ]
        , ylabel = [ 'conc', 'N' ]
        , title = "Plot with Index."
        , legends = [ "Series A", "Series B" ]
        , axis_attrib = 'legend pos=outer north east'
        , label = r'\bf a.'
        )
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import sys
import pypgfplots
import numpy as np

np.random.seed( 10 )

mat = np.random.rand( 10, 10 )

pypgfplots.standalone( 
        matrix = mat
        , outfile = '%s.tex' % sys.argv[0] 
        , title = 'Measurement matrix'
        , xlabel = 'Index'
        , ylabel = 'Index'
        , label = r'\bf b.'
        )
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import sys
import pypgfplots
import numpy as np

np.random.seed( 0 )
# Genearte a random matrix.
mat = np.random.rand( 10, 10 )

pypgfplots.standalone( matrix = mat
        , outfile = '%s.tex' % sys.argv[0] 
        , title = 'Measurement matrix'
        , xlabel = 'Index'
        , ylabel = 'Index'
         # If indices are not given then we compute the tick location.
        , ytick = [ (1,'c1'), (3,'c2'), (6,'c3') ]  
        , label = r'\bf b.'
        )
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Plot subplot in grid.
import sys
import pypgfplots
import numpy as np

np.random.seed( 10 )

m1 = np.random.rand( 10, 10 )
m2 = np.random.rand( 10, 10 )
y1 = np.random.rand( 100 )
y2 = np.random.rand( 100 )

pypgfplots.standalone( 
        subplots = { 
              (0,0) : dict( matrix = m1, title = 'mat A', label = 'a.' )
            , (0,1) : dict( xy = (np.arange(0, 100,1), y1), label = 'b.' )
            , (1,0) : dict( histogram = y1, label = 'b.'
                , xlabel = 'bins', ylabel = 'Count'
                , plot_attrib='fill=blue!20')
            , (1,1) : dict( matrix = m2, title = 'B', label = 'd.' )
            }
        , outfile = '%s.pdf' % sys.argv[0] 
        , title = 'Measurement matrix'
        , label = r'\bf b.'
        )
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import sys
import pypgfplots
import numpy as np

np.random.seed( 10 )

# generate a very large matrix. 
mat = np.random.rand( 1000, 1000 )

pypgfplots.standalone( 
        matrix = mat
        , outfile = '%s.tex' % sys.argv[0] 
        , title = 'A very large matrix for pdflatex'
        , shader = 'interp'
        , xlabel = 'A(i)'
        , ylabel = 'f(A(i))'
        , every = (10,20) # Plot every 10th row and 20th column.
        , label = r'\bf b.'
        )
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Plot subplot in grid.
import sys
import pypgfplots
import numpy as np

np.random.seed( 10 )

m1 = np.random.rand( 10, 10 )
m2 = np.random.rand( 10, 10 )
y1 = np.random.rand( 100 )
y2 = np.random.rand( 100 )

pypgfplots.standalone( 
        subplots = { 
              (0,0) : dict( matrix = m1, title = 'mat A', label = 'a.' )
            , (0,1) : dict( xy = (np.arange(0, 100,1), y1), label = 'b.' )
            , (1,0) : dict( histogram = y1, label = 'b.'
                , xlabel = 'bins', ylabel = 'Count'
                , plot_attrib='fill=blue!20')
            , (1,1) : dict( matrix = m2, title = 'B', label = 'd.' )
            }
        , outfile = '%s.tex' % sys.argv[0] 
        , title = 'Measurement matrix'
        , label = r'\bf b.'
        )