### Install tikzplotlib Source: https://github.com/nschloe/tikzplotlib/wiki/Home.org Install tikzplotlib systemwide using setup.py. Ensure Python can find the script. ```bash python3 setup.py install --user ``` -------------------------------- ### Install tikzplotlib Source: https://github.com/nschloe/tikzplotlib/blob/main/README.md Install the package using pip. ```bash pip install tikzplotlib ``` -------------------------------- ### Get Preamble Code Source: https://github.com/nschloe/tikzplotlib/blob/main/README.md Retrieve the required preamble code programmatically. ```python import tikzplotlib tikzplotlib.Flavors.latex.preamble() # or tikzplotlib.Flavors.context.preamble() ``` -------------------------------- ### Get TikZ Code as String Source: https://github.com/nschloe/tikzplotlib/blob/main/README.md Use get_tikz_code() instead of save() if you want the TikZ code as a string. ```python import tikzplotlib tikz_string = tikzplotlib.get_tikz_code() ``` -------------------------------- ### Export Heatmap with Colorbar to TikZ Source: https://context7.com/nschloe/tikzplotlib/llms.txt Export image plots (heatmaps) with colorbars to TikZ. This example shows how to create a heatmap and save it with a specified DPI for image resolution. ```python import matplotlib import matplotlib.pyplot as plt import numpy as np import tikzplotlib fig = plt.figure() # Create heatmap data x, y = np.ogrid[-10:10:100j, -10:10:100j] extent = (x.min(), x.max(), y.min(), y.max()) z = x * y # Plot with colorbar cmap = matplotlib.cm.get_cmap("viridis") plt.imshow(z, extent=extent, cmap=cmap, origin="lower") plt.colorbar(label="Intensity") plt.xlabel("X") plt.ylabel("Y") plt.title("Heatmap with Colorbar") # Export with custom DPI for image resolution tikzplotlib.save( "heatmap.tex", dpi=150, # Resolution for rasterized elements ) plt.close() ``` -------------------------------- ### Get LaTeX Preamble for TikZplotlib Source: https://context7.com/nschloe/tikzplotlib/llms.txt Retrieve the necessary LaTeX preamble code to include tikzplotlib output in your LaTeX or ConTeXt documents. This ensures all required packages and settings are present. ```python import tikzplotlib # Get standard LaTeX preamble preamble = tikzplotlib.Flavors.latex.preamble() print(preamble) # Output: # \usepackage[utf8]{inputenc} # \usepackage{pgfplots} # \DeclareUnicodeCharacter{2212}{−} # \usepgfplotslibrary{groupplots,dateplot} # \usetikzlibrary{patterns,shapes.arrows} # \pgfplotsset{compat=newest} ``` ```python # Get ConTeXt preamble context_preamble = tikzplotlib.Flavors.context.preamble() print(context_preamble) # Output: # \setupcolors[state=start] # \usemodule[tikz] # \usemodule[pgfplots] # \usepgfplotslibrary[groupplots,dateplot] # \usetikzlibrary[patterns,shapes.arrows] # \pgfplotsset{compat=newest} # \unexpanded\def\startgroupplot{\groupplot} # \unexpanded\def\stopgroupplot{\endgroupplot} ``` -------------------------------- ### Export Histogram Plot to TikZ Source: https://context7.com/nschloe/tikzplotlib/llms.txt Export histogram plots with legends and transparency to TikZ format. This example demonstrates how to create and save a histogram with multiple bars and labels. ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib fig = plt.figure() ax = fig.add_subplot(111) np.random.seed(123) ax.hist(10 + 2 * np.random.randn(1000), bins=20, label="men", alpha=0.7) ax.hist(12 + 3 * np.random.randn(1000), bins=20, label="women", alpha=0.5) ax.set_xlabel("Value") ax.set_ylabel("Frequency") ax.set_title("Histogram Comparison") ax.legend() tikzplotlib.save("histogram.tex") plt.close() ``` -------------------------------- ### Export Filled Contour Plot to TikZ Source: https://context7.com/nschloe/tikzplotlib/llms.txt Export filled contour plots to TikZ. This example demonstrates creating a contour plot with a colorbar and saving it to a .tex file. ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib fig = plt.figure() ax = plt.gca() # Create contour data nbins = 50 xi, yi = np.mgrid[0:2:nbins*1j, 0:2:nbins*1j] zi = 2 - (xi - 1)**2 - (yi - 1)**2 # Create filled contour cs = ax.contourf(xi, yi, zi, levels=10, cmap="coolwarm") plt.colorbar(cs) ax.set_xlim(0, 2) ax.set_ylim(0, 2) ax.set_xlabel("X axis") ax.set_ylabel("Y axis") ax.set_title("Filled Contour Plot") tikzplotlib.save("contour.tex") plt.close() ``` -------------------------------- ### Get TikZ Code as String Source: https://context7.com/nschloe/tikzplotlib/llms.txt The `get_tikz_code()` function returns the TikZ/PGFPlots code as a string, which is useful for programmatic embedding or further processing. ```APIDOC ## get_tikz_code Get the TikZ/PGFPlots code as a string instead of writing to a file. Useful when you need to embed the code programmatically or perform post-processing. ### Method GET ### Endpoint /tikzplotlib/get_tikz_code ### Parameters #### Query Parameters - **figure** (string) - Optional - Specifies the figure to use. Defaults to 'gcf' (get current figure). - **axis_width** (string) - Optional - Sets the width of the axis environment (e.g., `\figwidth`, `5cm`). - **axis_height** (string) - Optional - Sets the height of the axis environment (e.g., `5cm`). - **textsize** (float) - Optional - The target LaTeX text size in points (pt). - **strict** (boolean) - Optional - If true, PGFPlots will attempt to determine tick positions strictly. Defaults to false. - **wrap** (boolean) - Optional - If true, includes the `tikzpicture` environment. Defaults to true. - **add_axis_environment** (boolean) - Optional - If true, includes the `axis` environment. Defaults to true. - **extra_axis_parameters** (set of strings) - Optional - Additional parameters to pass to the `axis` environment (e.g., `{"xlabel near ticks", "ylabel near ticks"}`). - **include_disclaimer** (boolean) - Optional - If true, adds a comment with the tikzplotlib version. Defaults to true. - **standalone** (boolean) - Optional - If true, generates a complete, compilable LaTeX document. Defaults to false. - **float_format** (string) - Optional - The format string for floating-point values (e.g., ".15g"). - **filepath** (string) - Optional - Required when `externalize_tables` is true. The base path for the output .tex file. - **externalize_tables** (boolean) - Optional - If true, exports plot data to separate .dat files. Defaults to false. - **override_externals** (boolean) - Optional - If true, overwrites existing external data files. Defaults to false. - **tex_relative_path_to_data** (string) - Optional - The relative path from the LaTeX file to the data files when `externalize_tables` is true. ### Response #### Success Response (200) - **code** (string) - The generated TikZ/PGFPlots code as a string. ### Request Example ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib np.random.seed(42) x = np.linspace(0, 100, 101) y = x + 15 * np.random.rand(101) plt.figure() plt.scatter(x, y) plt.xlabel("X axis") plt.ylabel("Y axis") plt.title("Scatter Plot") code = tikzplotlib.get_tikz_code( figure="gcf", axis_width="\\figwidth", axis_height="5cm", textsize=11.0, strict=False, wrap=True, add_axis_environment=True, extra_axis_parameters={"xlabel near ticks", "ylabel near ticks"}, include_disclaimer=True, standalone=False, float_format=".15g", ) print(code) plt.close() ``` ### Request Example with Externalized Tables ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib t = np.arange(0.0, 2.0, 0.1) s = np.sin(2 * np.pi * t) s2 = np.cos(2 * np.pi * t) plt.figure() plt.plot(t, s, "o-", lw=1.5, label="sin") plt.plot(t, s2, "o-", lw=3, alpha=0.3, label="cos") plt.xlabel("time(s)") plt.ylabel("Voltage (mV)") plt.title("Externalized Tables Example") plt.grid(True) code = tikzplotlib.get_tikz_code( filepath="plot.tex", externalize_tables=True, override_externals=True, tex_relative_path_to_data="data/", ) print(code) plt.close() ``` ### Request Example for Standalone Document ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib fig = plt.figure() ax = fig.add_subplot(111) x = np.arange(3) y1 = [1, 2, 3] y2 = [3, 2, 4] y3 = [5, 3, 1] w = 0.25 ax.bar(x - w, y1, w, color="b", align="center", label="Group A") ax.bar(x, y2, w, color="g", align="center", label="Group B") ax.bar(x + w, y3, w, color="r", align="center", label="Group C") ax.legend() code = tikzplotlib.get_tikz_code(standalone=True) print(code) plt.close() ``` ``` -------------------------------- ### Export Subplot Grid to TikZ Source: https://context7.com/nschloe/tikzplotlib/llms.txt Export figures with multiple subplots to TikZ using PGFPlots groupplots. This example sets up a figure with multiple axes for creating complex layouts. ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib fig = plt.figure(figsize=(10, 8)) an = np.linspace(0, 2 * np.pi, 50) ``` -------------------------------- ### Export Dual Axis Plot to TikZ Source: https://context7.com/nschloe/tikzplotlib/llms.txt Export figures with twin axes (secondary y-axis) to TikZ. This example shows how to plot data on both primary and secondary y-axes and save the figure. ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib fig = plt.figure() ax = fig.add_subplot(111) dat = np.linspace(0, 10, 20) # Primary axis ax.plot(dat, dat**2, "b-", label="Quadratic") ax.set_xlabel("X axis") ax.set_ylabel("Primary Y (blue)", color="b") ax.tick_params(axis="y", labelcolor="b") # Secondary axis ax2 = ax.twinx() ax2.plot(dat, dat**0.5, "r-", label="Square root") ax2.set_ylabel("Secondary Y (red)", color="r") ax2.tick_params(axis="y", labelcolor="r") plt.title("Dual Axis Plot") tikzplotlib.save("dual_axis.tex") plt.close() ``` -------------------------------- ### Run Unit Tests Source: https://github.com/nschloe/tikzplotlib/blob/main/README.md Execute the test suite using pytest. ```bash pytest ``` -------------------------------- ### Generate Standalone LaTeX Documents Source: https://context7.com/nschloe/tikzplotlib/llms.txt Create a fully compilable LaTeX document including the necessary preamble and packages for PGFPlots. ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib # Create bar chart fig = plt.figure() ax = fig.add_subplot(111) x = np.arange(3) y1 = [1, 2, 3] y2 = [3, 2, 4] y3 = [5, 3, 1] w = 0.25 ax.bar(x - w, y1, w, color="b", align="center", label="Group A") ax.bar(x, y2, w, color="g", align="center", label="Group B") ax.bar(x + w, y3, w, color="r", align="center", label="Group C") ax.legend() # Generate standalone LaTeX document code = tikzplotlib.get_tikz_code(standalone=True) # This produces compilable document with: # \documentclass{standalone} # \usepackage[utf8]{inputenc} # \usepackage{pgfplots} # \DeclareUnicodeCharacter{2212}{−} # \usepgfplotslibrary{groupplots,dateplot} # \usetikzlibrary{patterns,shapes.arrows} ``` -------------------------------- ### Export Figures for ConTeXt Source: https://context7.com/nschloe/tikzplotlib/llms.txt Generates a mesh plot using pcolormesh and exports it for ConTeXt using the 'context' flavor. Also demonstrates retrieving the TikZ code as a string. ```python import matplotlib.cm as cm import matplotlib.pyplot as plt import numpy as np import tikzplotlib # Create mesh plot x, y = np.meshgrid(np.linspace(0, 1, 50), np.linspace(0, 1, 50)) z = x**2 - y**2 fig = plt.figure() plt.pcolormesh(x, y, z, cmap=cm.viridis, shading="gouraud") plt.colorbar() plt.xlabel("X") plt.ylabel("Y") # Export for ConTeXt tikzplotlib.save( "plot.tex", flavor="context", # Use ConTeXt syntax (\startaxis, \stopaxis, etc.) ) # Or get code as string code = tikzplotlib.get_tikz_code(flavor="context") # Output uses: \starttikzpicture ... \stoptikzpicture # Instead of: \begin{tikzpicture} ... \end{tikzpicture} plt.close() ``` -------------------------------- ### LaTeX Preamble Requirements Source: https://github.com/nschloe/tikzplotlib/blob/main/README.md Include necessary packages and settings in the document header for PGFPlots support. ```latex \usepackage[utf8]{inputenc} \usepackage{pgfplots} \DeclareUnicodeCharacter{2212}{−} \usepgfplotslibrary{groupplots,dateplot} \usetikzlibrary{patterns,shapes.arrows} \pgfplotsset{compat=newest} ``` ```latex \setupcolors[state=start] \usemodule[tikz] \usemodule[pgfplots] \usepgfplotslibrary[groupplots,dateplot] \usetikzlibrary[patterns,shapes.arrows] \pgfplotsset{compat=newest} \unexpanded\def\startgroupplot{\groupplot} \unexpanded\def\stopgroupplot{\endgroupplot} ``` -------------------------------- ### Create a 2x2 Subplot Grid Source: https://context7.com/nschloe/tikzplotlib/llms.txt Generates a figure with four subplots: a circle, a sine wave, a cosine wave, and a clipped tangent wave. Saves the figure as 'subplots.tex'. ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib an = np.linspace(0, 2 * np.pi, 100) plt.subplot(221) plt.plot(np.cos(an), np.sin(an)) plt.title("Circle", fontsize=10) plt.subplot(222) plt.plot(an, np.sin(an)) plt.title("Sine Wave", fontsize=10) plt.subplot(223) plt.plot(an, np.cos(an)) plt.title("Cosine Wave", fontsize=10) plt.subplot(224) plt.plot(an, np.tan(an)) plt.ylim(-5, 5) plt.title("Tangent (clipped)", fontsize=10) plt.tight_layout() tikzplotlib.save("subplots.tex") plt.close() ``` -------------------------------- ### Include TikZ in LaTeX Source: https://github.com/nschloe/tikzplotlib/blob/main/README.md Use the input command to include the generated file in your TeX source. ```latex \input{/path/to/mytikz.tex} ``` -------------------------------- ### Customize PGFPlots Output with Extra Parameters Source: https://context7.com/nschloe/tikzplotlib/llms.txt Creates a plot of sine and cosine waves and retrieves the TikZ code as a string, applying custom axis width, height, and additional PGFPlots/TikZ parameters. ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib t = np.linspace(0, 2*np.pi, 100) plt.figure() plt.plot(t, np.sin(t), label="sin") plt.plot(t, np.cos(t), label="cos") plt.legend() plt.grid(True) code = tikzplotlib.get_tikz_code( axis_width="10cm", axis_height="6cm", extra_axis_parameters={ "legend pos=outer north east", "grid=major", "xlabel near ticks", "ylabel near ticks", "minor tick num=1", }, extra_tikzpicture_parameters={ "scale=0.8", "transform shape", }, extra_lines_start=[ "% Custom comment at start", "\\tikzset{every mark/.append style={scale=0.5}}", ], ) print(code) plt.close() ``` -------------------------------- ### Export Area Plots with Fill Between Source: https://context7.com/nschloe/tikzplotlib/llms.txt Creates a figure with two filled areas and a reference line, then exports it as 'fill_between.tex'. Requires Matplotlib and TikZplotlib. ```python import matplotlib.pyplot as plt import tikzplotlib fig = plt.figure(figsize=(7, 4)) ax = fig.add_subplot(111) # Fill between with transparency ax.fill_between([1, 2, 3, 4], [2, 3, 2, 3], [3, 4, 3, 4], color="red", alpha=0.2, label="Range A") ax.fill_between([1, 2, 3, 4], [4, 5, 4, 5], [5, 6, 5, 6], color="blue", alpha=0.2, label="Range B") ax.plot([1, 2, 3, 4], [2, 4, 3, 5], "k-", linewidth=2, label="Reference") ax.set_xlabel("X") ax.set_ylabel("Y") ax.grid(True) ax.legend() tikzplotlib.save("fill_between.tex") plt.close() ``` -------------------------------- ### Define and Set Figure Dimensions in LaTeX Source: https://github.com/nschloe/tikzplotlib/wiki/Home.org Define and set the figure height and width using LaTeX length commands, corresponding to the variables used in tikz_save. ```latex \newlength\figureheight \newlength\figurewidth \setlength\figureheight{4cm} \setlength\figurewidth{6cm} \input{myfile.tikz} ``` -------------------------------- ### Set Pgfplots Compatibility Source: https://github.com/nschloe/tikzplotlib/wiki/Home.org Set the Pgfplots compatibility to 'newest' to utilize features from the latest Pgfplots package. ```latex \pgfplotsset{compat=newest} ``` -------------------------------- ### Include TikZ Figure in LaTeX Source: https://github.com/nschloe/tikzplotlib/wiki/Home.org Include the generated TikZ file in your LaTeX source using the \input command. Ensure TikZ and Pgfplots packages are loaded. ```latex \usepackage{tikz} \usepackage{pgfplots} ``` -------------------------------- ### Save TikZ with Figure Dimensions Source: https://github.com/nschloe/tikzplotlib/wiki/Home.org Save a TikZ file with specified figure height and width. Ensure dimensions are large enough to avoid compilation errors. ```python tikz_save( 'myfile.tikz', figureheight='4cm', figurewidth='6cm' ) ``` -------------------------------- ### Retrieve TikZ Code as String Source: https://context7.com/nschloe/tikzplotlib/llms.txt Generate PGFPlots code as a string for programmatic use. Allows fine-grained control over axis dimensions, formatting, and environment wrapping. ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib # Create scatter plot np.random.seed(42) x = np.linspace(0, 100, 101) y = x + 15 * np.random.rand(101) plt.figure() plt.scatter(x, y) plt.xlabel("X axis") plt.ylabel("Y axis") plt.title("Scatter Plot") # Get TikZ code as string code = tikzplotlib.get_tikz_code( figure="gcf", # Use current figure (default) axis_width="\\figwidth", # Custom width macro axis_height="5cm", # Fixed height textsize=11.0, # Target LaTeX text size in pt strict=False, # Let PGFPlots choose tick positions wrap=True, # Include tikzpicture environment add_axis_environment=True, # Include axis environment extra_axis_parameters={"xlabel near ticks", "ylabel near ticks"}, include_disclaimer=True, # Add tikzplotlib version comment standalone=False, # Don't include document wrapper float_format=".15g", # Precision for float values ) print(code) plt.close() ``` -------------------------------- ### Clean Figure Before Export Source: https://github.com/nschloe/tikzplotlib/blob/main/README.md Use clean_figure to simplify curves and remove points outside axes limits before saving. ```python import matplotlib.pyplot as plt import numpy as np # ... do your plotting import tikzplotlib tikzplotlib.clean_figure() tikzplotlib.save("test.tex") ``` -------------------------------- ### Export Data to External Tables Source: https://context7.com/nschloe/tikzplotlib/llms.txt Separate plot data into external .dat files to improve TeX compilation performance and data management. Requires a filepath to be specified. ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib # Create figure with data t = np.arange(0.0, 2.0, 0.1) s = np.sin(2 * np.pi * t) s2 = np.cos(2 * np.pi * t) plt.figure() plt.plot(t, s, "o-", lw=1.5, label="sin") plt.plot(t, s2, "o-", lw=3, alpha=0.3, label="cos") plt.xlabel("time(s)") plt.ylabel("Voltage (mV)") plt.title("Externalized Tables Example") plt.grid(True) # Get code with externalized data tables code = tikzplotlib.get_tikz_code( filepath="plot.tex", # Required for externalization externalize_tables=True, # Save data to .dat files override_externals=True, # Overwrite existing .dat files tex_relative_path_to_data="data/", # Path from LaTeX file to data ) print(code) # Output will reference: \addplot table {data/plot_000.dat}; plt.close() ``` -------------------------------- ### Save TikZ with LaTeX Variables for Dimensions Source: https://github.com/nschloe/tikzplotlib/wiki/Home.org Save a TikZ file using LaTeX variables for figure height and width, allowing dimension control within the LaTeX document. ```python tikz_save('myfile.tikz', figureheight = '\figureheight', figurewidth = '\figurewidth' ) ``` -------------------------------- ### Save Matplotlib Plot as TikZ Source: https://github.com/nschloe/tikzplotlib/blob/main/README.md Replace pyplot.show() with tikzplotlib.save() to export the current figure. ```python import tikzplotlib tikzplotlib.save("mytikz.tex") # or tikzplotlib.save("mytikz.tex", flavor="context") ``` -------------------------------- ### Save Matplotlib Figures to TikZ Source: https://context7.com/nschloe/tikzplotlib/llms.txt Export a matplotlib figure directly to a .tex file. Supports custom encoding and alternative flavors like ConTeXt. ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib # Create a matplotlib figure t = np.arange(0.0, 2.0, 0.1) s = np.sin(2 * np.pi * t) s2 = np.cos(2 * np.pi * t) plt.style.use("ggplot") plt.plot(t, s, "o-", lw=4.1, label="sin") plt.plot(t, s2, "o-", lw=4.1, label="cos") plt.xlabel("time (s)") plt.ylabel("Voltage (mV)") plt.title("Simple plot $\\frac{\\alpha}{2}$") plt.legend() plt.grid(True) # Save to TikZ file tikzplotlib.save("output.tex") # Save with custom encoding tikzplotlib.save("output.tex", encoding="utf-8") # Save for ConTeXt instead of LaTeX tikzplotlib.save("output.tex", flavor="context") plt.close() ``` -------------------------------- ### Clean Figure for TikZ Export Source: https://context7.com/nschloe/tikzplotlib/llms.txt Optimize a matplotlib figure before export by removing points outside visible limits, simplifying curves, and reducing point density. This reduces TikZ file size while maintaining visual quality. Use this when file size is a concern. ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib # Create figure with many data points x = np.linspace(0, 100, 1000) y = np.sin(x) + 0.1 * np.random.randn(1000) fig, ax = plt.subplots(1, 1, figsize=(5, 5)) ax.plot(x, y) ax.set_ylim([-1.5, 1.5]) ax.set_xlim([20, 80]) # Get code without cleaning (more points) raw_code = tikzplotlib.get_tikz_code() raw_lines = raw_code.count("\n") # Clean the figure to reduce points tikzplotlib.clean_figure( fig=fig, # Figure to clean (None = gcf) target_resolution=600, # Target PPI resolution scale_precision=1.0, # Precision scaling factor ) # Get optimized code (fewer points) clean_code = tikzplotlib.get_tikz_code() clean_lines = clean_code.count("\n") print(f"Lines saved: {raw_lines - clean_lines}") plt.close() ``` -------------------------------- ### Save Matplotlib Figure to TikZ File Source: https://context7.com/nschloe/tikzplotlib/llms.txt The `save()` function exports a matplotlib figure directly to a .tex file in PGFPlots/TikZ format. It accepts all parameters available in `get_tikz_code()`. ```APIDOC ## save Save a matplotlib figure directly to a TikZ/PGFPlots .tex file. This is the primary function for exporting figures and supports all the same parameters as `get_tikz_code()`. ### Method POST ### Endpoint /tikzplotlib/save ### Parameters #### Query Parameters - **filepath** (string) - Required - The path to the output .tex file. - **encoding** (string) - Optional - The encoding to use for the output file (e.g., "utf-8"). - **flavor** (string) - Optional - The TeX flavor to use ('latex' or 'context'). Defaults to 'latex'. ### Request Example ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib t = np.arange(0.0, 2.0, 0.1) s = np.sin(2 * np.pi * t) s2 = np.cos(2 * np.pi * t) plt.style.use("ggplot") plt.plot(t, s, "o-", lw=4.1, label="sin") plt.plot(t, s2, "o-", lw=4.1, label="cos") plt.xlabel("time (s)") plt.ylabel("Voltage (mV)") plt.title("Simple plot $\frac{\alpha}{2}$") plt.legend() plt.grid(True) tikzplotlib.save("output.tex") tikzplotlib.save("output.tex", encoding="utf-8") tikzplotlib.save("output.tex", flavor="context") plt.close() ``` ### Response #### Success Response (200) - **message** (string) - Confirmation message that the file was saved successfully. ``` -------------------------------- ### Save Matplotlib Plot to TikZ Format Source: https://github.com/nschloe/tikzplotlib/blob/main/README.md Use this function to save a matplotlib figure to a .tex file in PGFPlots format. Ensure matplotlib is closed and defaults are reset afterwards. ```python import matplotlib.pyplot as plt import numpy as np plt.style.use("ggplot") t = np.arange(0.0, 2.0, 0.1) s = np.sin(2 * np.pi * t) s2 = np.cos(2 * np.pi * t) plt.plot(t, s, "o-", lw=4.1) plt.plot(t, s2, "o-", lw=4.1) plt.xlabel("time (s)") plt.ylabel("Voltage (mV)") plt.title("Simple plot $\\frac{\\alpha}{2}$") plt.grid(True) import tikzplotlib tikzplotlib.save("test.tex") ``` ```python import matplotlib as mpl plt.close() mpl.rcParams.update(mpl.rcParamsDefault) ``` -------------------------------- ### Save Matplotlib Figure to TikZ Source: https://github.com/nschloe/tikzplotlib/wiki/Home.org Save a matplotlib figure to a .tikz file using tikz_save. This function converts the plot into Pgfplots (TikZ) code. ```python from tikzplotlib import save as tikz_save tikz_save( 'myfile.tikz' ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.