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:
- A
filecontents*environment block containing the table data (space-delimited). - A
tikzpictureenvironment with anaxisblock 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, Pythondict,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
- Ensure Python 3.x is installed.
- Install the necessary Python libraries if they are not already available:
pip install numpy pandas
- Place
pytikz.pyin your project directory (or install it as part of your Python environment).
Basic Usage
from pytikz import LatexPlotGenerator
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 = LatexPlotGenerator(
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
LatexPlotGenerator(
data,
data_filename,
latex_filename,
header=None
)
Parameters
data:pd.DataFrame|dict|np.ndarray|listof listsdata_filename: strlatex_filename: strheader: 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 Awesome 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
- Fork the repository.
- Create a new branch.
- Test your changes.
- Submit a pull request.
Happy plotting!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py2tikz-0.0.1.tar.gz.
File metadata
- Download URL: py2tikz-0.0.1.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc1d26e95bce6cfe993effe3cc55f256301fec23f8ec6dc53ac8ba31ffb45571
|
|
| MD5 |
57294bd44d462e2a647a430cfb59c5f2
|
|
| BLAKE2b-256 |
9d37056a32db93918895142f22a17bfa6b418362d9efb383cc562b883de5e885
|
File details
Details for the file Py2Tikz-0.0.1-py3-none-any.whl.
File metadata
- Download URL: Py2Tikz-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4c04d2b9dc3045f0b4e2452d9773d8bcac3406420af9a5410fd4a007e9cd87f
|
|
| MD5 |
e7b75e373a0318257c68167f7a3d6ff4
|
|
| BLAKE2b-256 |
869cb8a738f41f1c70227d02949ad773aa1f48c90c51e062ef5dc95d3110d0d3
|