Skip to main content

A Python library for generating LaTeX pgfplots/tikz code.

Project description

LatexPlotGenerator

Author: Patrik Dennis Version: v0.0.1

Overview

LatexPlotGenerator is a utility class for generating LaTeX code to create publication-ready plots. It automatically creates:

  1. A filecontents* environment block containing the table data (space-delimited).
  2. A tikzpicture environment with an axis block and one or more \addplot[...] commands.

The end result is a self-contained LaTeX snippet that you can include in your document by simply uploading the .tex file in for instance Overleaf and then writing \input{filename.text}.

Key Features

  • Data input: Accepts pandas.DataFrame, Python dict, numpy.ndarray, or a list-of-lists.
  • Configurable axis options: Title, labels, legend position, grid, figure size, axis limits, etc.
  • Multiple plot lines: Each plot line can have its own legend entry and styling options.
  • Easy customization: Additional TikZ or pgfplots options can be passed as keyword arguments.

Installation

pip install Py2Tikz

Basic Usage

from Py2Tikz import PyTikzPlot
import pandas as pd

# 1. Prepare your data (e.g., as a pandas DataFrame)
data = pd.DataFrame({
    "x": [0, 1, 2, 3, 4],
    "y": [0, 1, 4, 9, 16],
    "z": [0, 2, 3, 6, 10]
})

# 2. Create an instance of LatexPlotGenerator
generator = PyTikzPlot(
    data=data,
    data_filename="mydata.dat",
    latex_filename="myfigure.tex",
)

# 3. Configure the axis
generator.set_title("My Awesome Plot")
generator.set_labels("X-axis", "Y-axis")
generator.set_legend("north east")
generator.set_grid("grid", "major")
generator.set_figsize("8cm", "6cm")

# 4. Add one or more plot lines
generator.add_plot_line(table_x="x", table_y="y", legend="Quadratic", mark="o")
generator.add_plot_line(table_x="x", table_y="z", legend="Random Values", mark="*", color="red", thick=True)

# 5. (Optional) Adjust axis limits
generator.set_xmin(0)
generator.set_xmax(5)
generator.set_ymin(0)
generator.set_ymax(20)

# 6. Generate and save the LaTeX code
generator.save()

Constructor

PyTikzPlot(
    data,
    data_filename,
    latex_filename,
    header=None
)

Parameters

  • data: pd.DataFrame | dict | np.ndarray | list of lists
  • data_filename: str
  • latex_filename: str
  • header: list of str (required for array or list-of-lists)

Methods

set_title(title)

Sets the plot’s title.

set_labels(xlabel, ylabel)

Sets axis labels.

set_legend(legend_pos)

Sets legend position.

set_grid(option, value)

Sets grid options.

set_figsize(width, height)

Sets figure width and height.

set_xmin(xmin), set_xmax(xmax), set_ymin(ymin), set_ymax(ymax)

Set axis limits.

add_plot_line(table_x, table_y, legend, comment="", **line_options)

Adds a line to the plot.

generate_data_block()

Returns the LaTeX filecontents* block.

generate_tikz_picture()

Returns the tikzpicture block.

generate_latex_code()

Returns the full LaTeX code.

save()

Writes the LaTeX code to file.


Data Requirements

  • DataFrame: uses columns as headers.
  • dict: keys are headers, values are columns.
  • array/list-of-lists: must provide header.

Example LaTeX Output

\begin{filecontents*}{mydata.dat}
x y
0 0
1 1
2 4
3 9
4 16
\end{filecontents*}

\begin{figure}[H]
\centering
\begin{tikzpicture}
\centering
  \begin{axis}[
      title={My Plot},
      xlabel={X-axis},
      ylabel={Y-axis},
      legend pos=north east,
      grid=major,
      width=8cm,
      height=6cm,
      xmin=0,
      xmax=5,
      ymin=0,
      ymax=20,
    ]
    \addplot[mark=o] table [x=x, y=y, col sep=space]{mydata.dat};
    \addlegendentry{Quadratic};

  \end{axis}
\end{tikzpicture}
\end{figure}

Contributing

  1. Fork the repository.
  2. Create a new branch.
  3. Test your changes.
  4. Submit a pull request.

Happy plotting!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py2tikz-0.0.2.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Py2Tikz-0.0.2-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file py2tikz-0.0.2.tar.gz.

File metadata

  • Download URL: py2tikz-0.0.2.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.1

File hashes

Hashes for py2tikz-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d6dc2fd4983dba141dfb5ed35965b692f1f3bc1db0e3e580aff741434250bc8d
MD5 6d8ae02362743e29340fb88a0a97dc49
BLAKE2b-256 9bc95fe17da7cb967fc608a88a7bc1835b82b6aa252f3f037e843d2d97035608

See more details on using hashes here.

File details

Details for the file Py2Tikz-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: Py2Tikz-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.1

File hashes

Hashes for Py2Tikz-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 faaf1b6f59a00ac86ebb1577313f5393735581268745fd376c6dcda7ca8de170
MD5 35590ef5aeea0851e55232261e717eae
BLAKE2b-256 9e23e1324ee37d035dfd2b2840b24482cb45a89dba490ab5e1cb7c75dfb40c1a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page