├── .codecov.yml ├── .editorconfig ├── .flake8 ├── .github ├── ISSUE_TEMPLATE.md └── workflows │ └── ci.yml ├── .gitignore ├── .pre-commit-config.yaml ├── .readthedocs.yml ├── LICENSE ├── README.md ├── doc ├── conf.py └── index.rst ├── justfile ├── pyproject.toml ├── src └── tikzplotlib │ ├── __about__.py │ ├── __init__.py │ ├── _axes.py │ ├── _cleanfigure.py │ ├── _color.py │ ├── _files.py │ ├── _hatches.py │ ├── _image.py │ ├── _legend.py │ ├── _line2d.py │ ├── _markers.py │ ├── _patch.py │ ├── _path.py │ ├── _quadmesh.py │ ├── _save.py │ ├── _text.py │ └── _util.py ├── tests ├── .gitignore ├── __init__.py ├── helpers.py ├── lena.png ├── refresh_reference_files.py ├── rotated_labels.py ├── test_annotate.py ├── test_annotate_reference.tex ├── test_arrows.py ├── test_arrows_reference.tex ├── test_axvline.py ├── test_axvline_reference.tex ├── test_barchart.py ├── test_barchart_errorbars.py ├── test_barchart_errorbars_reference.tex ├── test_barchart_legend.py ├── test_barchart_legend_reference.tex ├── test_barchart_reference.tex ├── test_basic_sin.py ├── test_basic_sin_reference.tex ├── test_boxplot.py ├── test_boxplot_reference.tex ├── test_cleanfigure.py ├── test_colorbars.py ├── test_colorbars_reference.tex ├── test_context.py ├── test_context_reference.tex ├── test_contourf.py ├── test_contourf_reference.tex ├── test_custom_collection.py ├── test_custom_collection_reference.tex ├── test_datetime.py ├── test_datetime_paths.py ├── test_datetime_paths_reference.tex ├── test_datetime_reference.tex ├── test_deterministic_output.py ├── test_dual_axis.py ├── test_dual_axis_reference.tex ├── test_errorband.py ├── test_errorband_reference.tex ├── test_errorbar.py ├── test_errorbar_reference.tex ├── test_escape_chars.py ├── test_escape_chars_reference.tex ├── test_externalize_tables.py ├── test_externalize_tables_reference.tex ├── test_fancy_colorbar.py ├── test_fancy_colorbar_reference.tex ├── test_fancybox.py ├── test_fancybox_reference.tex ├── test_fillstyle.py ├── test_fillstyle_reference.tex ├── test_hatch.py ├── test_hatch_reference.tex ├── test_heat.py ├── test_heat_reference.tex ├── test_histogram.py ├── test_histogram_reference.tex ├── test_horizontal_alignment.py ├── test_horizontal_alignment_reference.tex ├── test_image_plot_lower.py ├── test_image_plot_lower_reference.tex ├── test_image_plot_upper.py ├── test_image_plot_upper_reference.tex ├── test_legend3.py ├── test_legend3_reference.tex ├── test_legend_best_location.py ├── test_legend_best_location_reference.tex ├── test_legend_columns.py ├── test_legend_columns_reference.tex ├── test_legend_labels.py ├── test_legend_labels_reference.tex ├── test_legend_line_scatter.py ├── test_legend_line_scatter_reference.tex ├── test_legends.py ├── test_legends2.py ├── test_legends2_reference.tex ├── test_legends_reference.tex ├── test_line_collection.py ├── test_line_collection_reference.tex ├── test_line_color_marker.py ├── test_line_color_marker_reference.tex ├── test_line_dashes.py ├── test_line_dashes_reference.tex ├── test_line_set_data.py ├── test_line_set_data_reference.tex ├── test_loglogplot.py ├── test_loglogplot_reference.tex ├── test_logplot.py ├── test_logplot_base.py ├── test_logplot_base_reference.tex ├── test_logplot_reference.tex ├── test_marker.py ├── test_marker_reference.tex ├── test_noise.py ├── test_noise2.py ├── test_noise2_reference.tex ├── test_noise_reference.tex ├── test_pandas_dataframe.py ├── test_pandas_dataframe_reference.tex ├── test_patch_styles.py ├── test_patch_styles_reference.tex ├── test_patches.py ├── test_patches_reference.tex ├── test_quadmesh.py ├── test_quadmesh_reference.tex ├── test_rotated_labels.py ├── test_scatter.py ├── test_scatter_colormap.py ├── test_scatter_colormap_reference.tex ├── test_scatter_different_colors.py ├── test_scatter_different_colors_reference.tex ├── test_scatter_different_sizes.py ├── test_scatter_different_sizes_reference.tex ├── test_scatter_reference.tex ├── test_sharex_and_y.py ├── test_sharex_and_y_reference.tex ├── test_steps.py ├── test_steps_reference.tex ├── test_subplot4x4.py ├── test_subplot4x4_reference.tex ├── test_subplots.py ├── test_subplots_reference.tex ├── test_subplots_with_colorbars.py ├── test_subplots_with_colorbars_reference.tex ├── test_text_overlay.py ├── test_text_overlay_reference.tex ├── test_tick_positions.py ├── test_tick_positions_reference.tex ├── test_viridis.py └── test_viridis_reference.tex └── tox.ini /.codecov.yml: -------------------------------------------------------------------------------- 1 | comment: no 2 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 4 7 | insert_final_newline = true 8 | end_of_line = lf 9 | 10 | [*.{yaml,yml}] 11 | indent_size = 2 12 | -------------------------------------------------------------------------------- /.flake8: -------------------------------------------------------------------------------- 1 | [flake8] 2 | ignore = E203, E266, E501, W503, C901 3 | max-line-length = 88 4 | max-complexity = 20 5 | select = B,C,E,F,W,T4,B9 6 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Please provide a minimal working example (MWE) that shows what isn't working as 2 | expected. Needed is 3 | 4 | * the Python code, 5 | * the output TeX (Pgfplots), and 6 | * the expected TeX (Pgfplots). 7 | 8 | Perhaps the [Pgfplots manual](http://mirror.ctan.org/tex-archive/graphics/pgf/contrib/pgfplots/doc/pgfplots.pdf) 9 | is helpful here. 10 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | lint: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - name: Check out repo 16 | uses: actions/checkout@v2 17 | - name: Set up Python 18 | uses: actions/setup-python@v2 19 | - name: Run pre-commit 20 | uses: pre-commit/action@v2.0.3 21 | 22 | build: 23 | runs-on: ${{ matrix.os }} 24 | strategy: 25 | matrix: 26 | os: [ubuntu-latest, windows-latest, macOS-latest] 27 | python-version: ["3.7", "3.8", "3.9", "3.10"] 28 | steps: 29 | - uses: actions/setup-python@v2 30 | with: 31 | python-version: ${{ matrix.python-version }} 32 | - uses: actions/checkout@v2 33 | # - name: Install system dependencies 34 | # run: sudo apt-get install -y texlive-latex-base texlive-latex-extra context python3-tk 35 | - name: Test with tox 36 | run: | 37 | pip install tox 38 | tox -- --cov tikzplotlib --cov-report xml --cov-report term 39 | - uses: codecov/codecov-action@v1 40 | if: ${{ matrix.python-version == '3.10' && matrix.os == 'ubuntu-latest' }} 41 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.swp 3 | *.png 4 | *.pdf 5 | doc/_build/ 6 | MANIFEST 7 | dist/ 8 | build/ 9 | .cache/ 10 | *.egg-info/ 11 | .pytest_cache/ 12 | .tox/ 13 | mytikz.tex 14 | test.tex 15 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/PyCQA/isort 3 | rev: 5.10.1 4 | hooks: 5 | - id: isort 6 | 7 | - repo: https://github.com/psf/black 8 | rev: 22.1.0 9 | hooks: 10 | - id: black 11 | language_version: python3 12 | 13 | - repo: https://github.com/PyCQA/flake8 14 | rev: 4.0.1 15 | hooks: 16 | - id: flake8 17 | -------------------------------------------------------------------------------- /.readthedocs.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | sphinx: 3 | configuration: doc/conf.py 4 | 5 | 6 | python: 7 | install: 8 | - method: pip 9 | path: . 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2010-2021 Nico Schlömer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /doc/conf.py: -------------------------------------------------------------------------------- 1 | # Configuration file for the Sphinx documentation builder. 2 | # 3 | # This file only contains a selection of the most common options. For a full 4 | # list see the documentation: 5 | # https://www.sphinx-doc.org/en/master/usage/configuration.html 6 | 7 | # -- Path setup -------------------------------------------------------------- 8 | 9 | import datetime 10 | import pathlib 11 | 12 | # If extensions (or modules to document with autodoc) are in another directory, 13 | # add these directories to sys.path here. If the directory is relative to the 14 | # documentation root, use os.path.abspath to make it absolute, like shown here. 15 | # 16 | # import os 17 | # import sys 18 | # sys.path.insert(0, os.path.abspath('.')) 19 | from configparser import ConfigParser 20 | 21 | # Sphinx 1.* compat (for readthedocs) 22 | master_doc = "index" 23 | 24 | p = ConfigParser() 25 | this_dir = pathlib.Path(__file__).resolve().parent 26 | p.read(this_dir / ".." / "setup.cfg") 27 | 28 | # -- Project information ----------------------------------------------------- 29 | project = p["metadata"]["name"] 30 | year = datetime.datetime.utcnow().year 31 | author = p["metadata"]["author"] 32 | copyright = f"2010-{year}, {author}" 33 | 34 | release = p["metadata"]["version"] 35 | 36 | 37 | # -- General configuration --------------------------------------------------- 38 | 39 | # Add any Sphinx extension module names here, as strings. They can be 40 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 41 | # ones. 42 | extensions = ["sphinx.ext.autodoc"] 43 | 44 | # Add any paths that contain templates here, relative to this directory. 45 | templates_path = ["_templates"] 46 | 47 | # List of patterns, relative to source directory, that match files and 48 | # directories to ignore when looking for source files. 49 | # This pattern also affects html_static_path and html_extra_path. 50 | exclude_patterns = [] 51 | 52 | 53 | # -- Options for HTML output ------------------------------------------------- 54 | 55 | # The theme to use for HTML and HTML Help pages. See the documentation for 56 | # a list of builtin themes. 57 | # 58 | html_theme = "alabaster" 59 | 60 | # Add any paths that contain custom static files (such as style sheets) here, 61 | # relative to this directory. They are copied after the builtin static files, 62 | # so a file named "default.css" will overwrite the builtin "default.css". 63 | # html_static_path = ["_static"] 64 | 65 | html_theme_options = { 66 | # "logo": "meshplex-logo.svg", 67 | "github_user": "nschloe", 68 | "github_repo": "tikzplotlib", 69 | "github_banner": True, 70 | "github_button": False, 71 | } 72 | -------------------------------------------------------------------------------- /doc/index.rst: -------------------------------------------------------------------------------- 1 | .. tikzplotlib documentation master file, created by 2 | sphinx-quickstart on Thu Feb 4 12:49:27 2021. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to tikzplotlib's documentation! 7 | ======================================= 8 | 9 | .. toctree:: 10 | :maxdepth: 2 11 | :caption: Contents: 12 | 13 | Methods 14 | ======= 15 | 16 | .. automodule:: tikzplotlib 17 | :members: 18 | 19 | Indices and tables 20 | ================== 21 | 22 | * :ref:`genindex` 23 | * :ref:`modindex` 24 | * :ref:`search` 25 | -------------------------------------------------------------------------------- /justfile: -------------------------------------------------------------------------------- 1 | version := `python3 -c "from src.tikzplotlib.__about__ import __version__; print(__version__)"` 2 | 3 | default: 4 | @echo "\"just publish\"?" 5 | 6 | publish: 7 | @if [ "$(git rev-parse --abbrev-ref HEAD)" != "main" ]; then exit 1; fi 8 | gh release create "v{{version}}" 9 | flit publish 10 | 11 | clean: 12 | @find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf 13 | @rm -rf src/*.egg-info/ build/ dist/ .tox/ ./doc/_build/ 14 | 15 | format: 16 | isort . 17 | black . 18 | blacken-docs README.md 19 | 20 | lint: 21 | black --check . 22 | flake8 . 23 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["flit_core >=3.2,<4"] 3 | build-backend = "flit_core.buildapi" 4 | 5 | [tool.isort] 6 | profile = "black" 7 | 8 | [project] 9 | name = "tikzplotlib" 10 | authors = [{name = "Nico Schlömer", email = "nico.schloemer@gmail.com"}] 11 | description = "Convert matplotlib figures into TikZ/PGFPlots" 12 | readme = "README.md" 13 | license = {file = "LICENSE"} 14 | classifiers = [ 15 | "Development Status :: 5 - Production/Stable", 16 | "Framework :: Matplotlib", 17 | "License :: OSI Approved :: MIT License", 18 | "Operating System :: OS Independent", 19 | "Programming Language :: Python", 20 | "Programming Language :: Python :: 3", 21 | "Programming Language :: Python :: 3.7", 22 | "Programming Language :: Python :: 3.8", 23 | "Programming Language :: Python :: 3.9", 24 | "Programming Language :: Python :: 3.10", 25 | "Topic :: Multimedia :: Graphics :: Graphics Conversion", 26 | "Topic :: Scientific/Engineering :: Visualization", 27 | ] 28 | keywords = ["latex", "tikz", "matplotlib", "graphics"] 29 | dynamic = ["version"] 30 | requires-python = ">=3.7" 31 | dependencies = [ 32 | "matplotlib >= 1.4.0", 33 | "numpy", 34 | "Pillow", 35 | "webcolors", 36 | ] 37 | 38 | [project.urls] 39 | Code = "https://github.com/nschloe/tikzplotlib" 40 | Issues = "https://github.com/nschloe/tikzplotlib/issues" 41 | Funding = "https://github.com/sponsors/nschloe" 42 | -------------------------------------------------------------------------------- /src/tikzplotlib/__about__.py: -------------------------------------------------------------------------------- 1 | __version__ = "0.10.1" 2 | -------------------------------------------------------------------------------- /src/tikzplotlib/__init__.py: -------------------------------------------------------------------------------- 1 | """Script to convert Matplotlib generated figures into TikZ/PGFPlots figures. 2 | """ 3 | from .__about__ import __version__ 4 | from ._cleanfigure import clean_figure 5 | from ._save import Flavors, get_tikz_code, save 6 | 7 | __all__ = [ 8 | "__version__", 9 | "get_tikz_code", 10 | "save", 11 | "clean_figure", 12 | "Flavors", 13 | ] 14 | -------------------------------------------------------------------------------- /src/tikzplotlib/_color.py: -------------------------------------------------------------------------------- 1 | import matplotlib as mpl 2 | import numpy as np 3 | import webcolors 4 | 5 | # RGB values (as taken from xcolor.dtx): 6 | builtin_colors = { 7 | "white": [1, 1, 1], 8 | "lightgray": [0.75, 0.75, 0.75], 9 | "gray": [0.5, 0.5, 0.5], 10 | "darkgray": [0.25, 0.25, 0.25], 11 | "black": [0, 0, 0], 12 | # 13 | "red": [1, 0, 0], 14 | "green": [0, 1, 0], 15 | "blue": [0, 0, 1], 16 | "brown": [0.75, 0.5, 0.25], 17 | "lime": [0.75, 1, 0], 18 | "orange": [1, 0.5, 0], 19 | "pink": [1, 0.75, 0.75], 20 | "purple": [0.75, 0, 0.25], 21 | "teal": [0, 0.5, 0.5], 22 | "violet": [0.5, 0, 0.5], 23 | # The colors cyan, magenta, yellow, and olive are also 24 | # predefined by xcolor, but their RGB approximation of the 25 | # native CMYK values is not very good. Don't use them here. 26 | } 27 | 28 | 29 | def _get_closest_colour_name(rgb): 30 | match = None 31 | mindiff = 1.0e15 32 | for h, name in webcolors.CSS3_HEX_TO_NAMES.items(): 33 | r = int(h[1:3], 16) 34 | g = int(h[3:5], 16) 35 | b = int(h[5:7], 16) 36 | 37 | diff = (rgb[0] - r) ** 2 + (rgb[1] - g) ** 2 + (rgb[2] - b) ** 2 38 | if diff < mindiff: 39 | match = name 40 | mindiff = diff 41 | 42 | if mindiff == 0: 43 | break 44 | 45 | return match, mindiff 46 | 47 | 48 | def mpl_color2xcolor(data, matplotlib_color): 49 | """Translates a matplotlib color specification into a proper LaTeX xcolor.""" 50 | # Convert it to RGBA. 51 | my_col = np.array(mpl.colors.ColorConverter().to_rgba(matplotlib_color)) 52 | 53 | # If the alpha channel is exactly 0, then the color is really 'none' 54 | # regardless of the RGB channels. 55 | if my_col[-1] == 0.0: 56 | return data, "none", my_col 57 | 58 | # Check if it exactly matches any of the colors already available. 59 | # This case is actually treated below (alpha==1), but that loop 60 | # may pick up combinations with black before finding the exact 61 | # match. Hence, first check all colors. 62 | for name, rgb in builtin_colors.items(): 63 | if all(my_col[:3] == rgb): 64 | return data, name, my_col 65 | 66 | # Don't handle gray colors separately. They can be specified in xcolor as 67 | # 68 | # {gray}{0.6901960784313725} 69 | # 70 | # but this float representation hides the fact that this is actually an 71 | # RGB255 integer value, 176. 72 | 73 | # convert to RGB255 74 | rgb255 = np.array(my_col[:3] * 255, dtype=int) 75 | 76 | name, diff = _get_closest_colour_name(rgb255) 77 | if diff > 0: 78 | if np.all(my_col[0] == my_col[:3]): 79 | name = f"{name}{rgb255[0]}" 80 | else: 81 | name = f"{name}{rgb255[0]}{rgb255[1]}{rgb255[2]}" 82 | data["custom colors"][name] = ("RGB", ",".join([str(val) for val in rgb255])) 83 | 84 | return data, name, my_col 85 | -------------------------------------------------------------------------------- /src/tikzplotlib/_files.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | 4 | def _gen_filepath(data, nb_key, ext): 5 | rel_filepath = Path(f"{data['base name']}-{data[nb_key]:03d}{ext}") 6 | 7 | if data["rel data path"]: 8 | rel_filepath = data["rel data path"] / rel_filepath 9 | 10 | return data["output dir"] / rel_filepath, rel_filepath 11 | 12 | 13 | def new_filepath(data, file_kind, ext): 14 | """Returns an available filepath. 15 | 16 | :param file_kind: Name under which numbering is recorded, such as 'img' or 17 | 'table'. 18 | :type file_kind: str 19 | 20 | :param ext: Filename extension. 21 | :type ext: str 22 | 23 | :returns: (filepath, rel_filepath) where filepath is a path in the 24 | filesystem and rel_filepath is the path to be used in the tex 25 | code. 26 | """ 27 | 28 | nb_key = file_kind + "number" 29 | if nb_key not in data.keys(): 30 | data[nb_key] = -1 31 | 32 | data[nb_key] += 1 33 | filepath, rel_filepath = _gen_filepath(data, nb_key, ext) 34 | if not data["override externals"]: 35 | # Make sure not to overwrite anything. 36 | while filepath.is_file(): 37 | data[nb_key] += 1 38 | filepath, rel_filepath = _gen_filepath(data, nb_key, ext) 39 | 40 | return filepath, rel_filepath 41 | -------------------------------------------------------------------------------- /src/tikzplotlib/_hatches.py: -------------------------------------------------------------------------------- 1 | r""" 2 | Map matplotlib hatches to tikz patterns 3 | 4 | For matplotlib hatches, see: 5 | https://matplotlib.org/3.1.1/gallery/shapes_and_collections/hatch_demo.html 6 | 7 | For patterns in tikzpgf: 8 | Ch 26 Pattern Lbrary in the manual 9 | Requires \usetikzlibrary{patterns} 10 | """ 11 | 12 | # These methods exist, and might be relevant (in the future?): 13 | # matplotlib.backend_bases.GraphicsContextBase.set/get_hatch_color 14 | # matplotlib.backend_bases.GraphicsContextBase.set/get_hatch_linewidth 15 | # hatch_density is mentioned in mpl API Changes in 2.0.1 16 | 17 | 18 | import warnings 19 | 20 | BAD_MP_HATCH = ["o", "O"] # Bad hatch/pattern correspondence 21 | UNUSED_PGF_PATTERN = ["dots"] 22 | _MP_HATCH2PGF_PATTERN = { 23 | "-": "horizontal lines", 24 | "|": "vertical lines", 25 | "/": "north east lines", 26 | "\\": "north west lines", 27 | "+": "grid", 28 | "x": "crosshatch", 29 | ".": "crosshatch dots", 30 | "*": "fivepointed stars", 31 | "o": "sixpointed stars", 32 | "O": "bricks", 33 | } 34 | 35 | 36 | def add_custom_pattern(mpl_hatch, pattern_name, pattern_definition=None): 37 | """ 38 | The patterns of tikzpgf are quite simple, and cannot be customized but for the 39 | color. A solution is to expose a function like this one to allow the user to 40 | populate _MP_HATCH2PGF_PATTERN with custom (hatch, pattern) pairs. mpl does no 41 | validation of the hatch string, it just ignores it if it does not recognize it, so 42 | it is possible to have string be a mpl_hatch. 43 | 44 | If the pattern definition is passed, it could be added at the start of the code in a 45 | similar fashion to 46 | > data["custom colors"] = {} 47 | in get_tikz_code(). tikzplotlib pattern definitions would mend the bad 48 | correspondence between the mpl hatches and tikz patterns, with custom patterns for 49 | the mpl hatches 'o' and 'O'. 50 | 51 | Want some opinions on this before I implement it.. 52 | 53 | From https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.patches.Patch.html: 54 | > Letters can be combined, in which case all the specified hatchings are done. 55 | > If same letter repeats, it increases the density of hatching of that pattern. 56 | To achieve something like this, custom patterns must be created 57 | https://tex.stackexchange.com/questions/29359/pgfplots-how-to-fill-the-area- 58 | under-a-curve-with-oblique-lines-hatching-as-a/29367#29367 59 | """ 60 | 61 | 62 | def __validate_hatch(hatch): 63 | """Warn about the shortcomings of patterns""" 64 | if len(hatch) > 1: 65 | warnings.warn( 66 | f"tikzplotlib: Hatch '{hatch}' cannot be rendered. " 67 | + "Only single character hatches are supported, e.g., " 68 | + r"{'/', '\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}. " 69 | + f"Hatch '{hatch[0]}' will be used." 70 | ) 71 | hatch = hatch[0] 72 | 73 | if hatch in BAD_MP_HATCH: 74 | warnings.warn( 75 | f"tikzplotlib: The hatches {BAD_MP_HATCH} do not have good PGF" 76 | + " counterparts." 77 | ) 78 | return hatch 79 | 80 | 81 | def _mpl_hatch2pgfp_pattern(data, hatch, color_name, color_rgba): 82 | r""" 83 | Translates a hatch from matplotlib to the corresponding pattern in PGFPlots. 84 | 85 | Input: 86 | hatch - str, like {'/', '\', '|', '-', '+', 'x', 'o', 'O', '.', '*'} 87 | color_name - str, xcolor or custom color name 88 | color_rgba - np.array, the rgba value of the color 89 | Output: 90 | draw_options - list, empty or with a post action string 91 | """ 92 | hatch = __validate_hatch(hatch) 93 | try: 94 | pgfplots_pattern = _MP_HATCH2PGF_PATTERN[hatch] 95 | except KeyError: 96 | warnings.warn(f"tikzplotlib: The hatch {hatch} is ignored.") 97 | return data, [] 98 | 99 | data["tikz libs"].add("patterns") 100 | 101 | pattern_options = [f"pattern={pgfplots_pattern}"] 102 | if color_name != "black": 103 | # PGFPlots render patterns in 'pattern color' (default: black) 104 | pattern_options += [f"pattern color={color_name}"] 105 | if color_rgba[3] != 1: 106 | ff = data["float format"] 107 | # PGFPlots render patterns according to opacity fill. 108 | # This change is within the scope of the postaction 109 | pattern_options.append(f"fill opacity={color_rgba[3]:{ff}}") 110 | 111 | # Add pattern as postaction to allow color fill and pattern together 112 | # https://tex.stackexchange.com/questions/24964/ 113 | # how-to-combine-fill-and-pattern-in-a-pgfplot-bar-plot 114 | postaction = f"postaction={{{', '.join(pattern_options)}}}" 115 | 116 | return data, [postaction] 117 | -------------------------------------------------------------------------------- /src/tikzplotlib/_image.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import PIL 4 | 5 | from . import _files 6 | 7 | 8 | def draw_image(data, obj): 9 | """Returns the PGFPlots code for an image environment.""" 10 | content = [] 11 | 12 | filepath, rel_filepath = _files.new_filepath(data, "img", ".png") 13 | 14 | # store the image as in a file 15 | img_array = obj.get_array() 16 | 17 | dims = img_array.shape 18 | if len(dims) == 2: # the values are given as one real number: look at cmap 19 | clims = obj.get_clim() 20 | plt.imsave( 21 | fname=filepath, 22 | arr=img_array, 23 | cmap=obj.get_cmap(), 24 | vmin=clims[0], 25 | vmax=clims[1], 26 | origin=obj.origin, 27 | ) 28 | else: 29 | # RGB (+alpha) information at each point 30 | assert len(dims) == 3 and dims[2] in [3, 4] 31 | # convert to PIL image 32 | if obj.origin == "lower": 33 | img_array = np.flipud(img_array) 34 | 35 | # Convert mpl image to PIL 36 | if img_array.dtype != np.uint8: 37 | img_array = np.uint8(img_array * 255) 38 | image = PIL.Image.fromarray(img_array) 39 | 40 | # If the input image is PIL: 41 | # image = PIL.Image.fromarray(img_array) 42 | 43 | image.save(filepath, origin=obj.origin) 44 | 45 | # write the corresponding information to the TikZ file 46 | extent = obj.get_extent() 47 | 48 | # the format specification will only accept tuples 49 | if not isinstance(extent, tuple): 50 | extent = tuple(extent) 51 | 52 | # Explicitly use \pgfimage as includegrapics command, as the default 53 | # \includegraphics fails unexpectedly in some cases 54 | ff = data["float format"] 55 | # Always use slash in file paths, see 56 | # 57 | # 58 | posix_filepath = rel_filepath.as_posix() 59 | content.append( 60 | "\\addplot graphics [includegraphics cmd=\\pgfimage," 61 | f"xmin={extent[0]:{ff}}, xmax={extent[1]:{ff}}, " 62 | f"ymin={extent[2]:{ff}}, ymax={extent[3]:{ff}}] {{{posix_filepath}}};\n" 63 | ) 64 | return data, content 65 | -------------------------------------------------------------------------------- /src/tikzplotlib/_markers.py: -------------------------------------------------------------------------------- 1 | # for matplotlib markers, see https://matplotlib.org/api/markers_api.html 2 | _MP_MARKER2PGF_MARKER = { 3 | ".": "*", # point 4 | # ",": # pixel 5 | "o": "o", # circle 6 | "+": "+", # plus 7 | "P": "+", # actually plus filled 8 | "x": "x", # x 9 | "X": "x", # actually x filled 10 | "None": None, 11 | " ": None, 12 | "": None, 13 | } 14 | 15 | # the following markers are only available with PGF's plotmarks library 16 | # See 17 | # , 18 | # chapter 4.7, page 183 ff. 19 | _MP_MARKER2PLOTMARKS = { 20 | "v": ("triangle", ["rotate=180"]), # triangle down 21 | "^": ("triangle", []), # triangle up 22 | "<": ("triangle", ["rotate=270"]), # triangle left 23 | ">": ("triangle", ["rotate=90"]), # triangle right 24 | "1": ("Mercedes star flipped", []), 25 | "2": ("Mercedes star", []), 26 | "3": ("Mercedes star", ["rotate=90"]), 27 | "4": ("Mercedes star", ["rotate=270"]), 28 | "s": ("square", []), 29 | "p": ("pentagon", []), 30 | "*": ("asterisk", []), 31 | "h": ("star", []), # hexagon 1 32 | "H": ("star", []), # hexagon 2 33 | "d": ("diamond", []), # diamond 34 | "D": ("diamond", []), # thin diamond 35 | "|": ("|", []), # vertical line 36 | "_": ("-", []), # horizontal line 37 | } 38 | 39 | 40 | def _mpl_marker2pgfp_marker(data, mpl_marker: str, is_filled: bool): 41 | """Translates a marker style of matplotlib to the corresponding style 42 | in PGFPlots. 43 | """ 44 | # try default list 45 | try: 46 | pgfplots_marker = _MP_MARKER2PGF_MARKER[mpl_marker] 47 | except KeyError: 48 | pass 49 | else: 50 | if is_filled and pgfplots_marker == "o": 51 | pgfplots_marker = "*" 52 | data["tikz libs"].add("plotmarks") 53 | marker_options = [] 54 | return data, pgfplots_marker, marker_options 55 | 56 | # try plotmarks list 57 | try: 58 | data["tikz libs"].add("plotmarks") 59 | pgfplots_marker, marker_options = _MP_MARKER2PLOTMARKS[mpl_marker] 60 | except KeyError: 61 | # There's no equivalent for the pixel marker (,) in Pgfplots. 62 | pass 63 | else: 64 | if is_filled and pgfplots_marker not in ["|", "-", "asterisk", "star"]: 65 | pgfplots_marker += "*" 66 | return data, pgfplots_marker, marker_options 67 | 68 | return data, None, [] 69 | -------------------------------------------------------------------------------- /src/tikzplotlib/_quadmesh.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | 3 | from . import _files 4 | 5 | 6 | def draw_quadmesh(data, obj): 7 | """Returns the PGFPlots code for an graphics environment holding a 8 | rendering of the object. 9 | """ 10 | content = [] 11 | 12 | # Generate file name for current object 13 | filepath, rel_filepath = _files.new_filepath(data, "img", ".png") 14 | 15 | # Get the dpi for rendering and store the original dpi of the figure 16 | dpi = data["dpi"] 17 | fig_dpi = obj.figure.get_dpi() 18 | obj.figure.set_dpi(dpi) 19 | 20 | # Render the object and save as png file 21 | from matplotlib.backends.backend_agg import RendererAgg 22 | 23 | cbox = obj.get_clip_box() 24 | width = int(round(cbox.extents[2])) 25 | height = int(round(cbox.extents[3])) 26 | ren = RendererAgg(width, height, dpi) 27 | obj.draw(ren) 28 | 29 | # Generate a image from the render buffer 30 | image = Image.frombuffer( 31 | "RGBA", ren.get_canvas_width_height(), ren.buffer_rgba(), "raw", "RGBA", 0, 1 32 | ) 33 | # Crop the image to the actual content (removing the the regions otherwise 34 | # used for axes, etc.) 35 | # 'image.crop' expects the crop box to specify the left, upper, right, and 36 | # lower pixel. 'cbox.extents' gives the left, lower, right, and upper 37 | # pixel. 38 | box = ( 39 | int(round(cbox.extents[0])), 40 | 0, 41 | int(round(cbox.extents[2])), 42 | int(round(cbox.extents[3] - cbox.extents[1])), 43 | ) 44 | cropped = image.crop(box) 45 | cropped.save(filepath) 46 | 47 | # Restore the original dpi of the figure 48 | obj.figure.set_dpi(fig_dpi) 49 | 50 | # write the corresponding information to the TikZ file 51 | extent = obj.axes.get_xlim() + obj.axes.get_ylim() 52 | 53 | # Explicitly use \pgfimage as includegrapics command, as the default 54 | # \includegraphics fails unexpectedly in some cases 55 | ff = data["float format"] 56 | posix_filepath = rel_filepath.as_posix() 57 | content.append( 58 | "\\addplot graphics [includegraphics cmd=\\pgfimage," 59 | f"xmin={extent[0]:{ff}}, xmax={extent[1]:{ff}}, " 60 | f"ymin={extent[2]:{ff}}, ymax={extent[3]:{ff}}] {{{posix_filepath}}};\n" 61 | ) 62 | 63 | return data, content 64 | -------------------------------------------------------------------------------- /src/tikzplotlib/_util.py: -------------------------------------------------------------------------------- 1 | import matplotlib.transforms 2 | import numpy as np 3 | 4 | 5 | def has_legend(axes): 6 | return axes.get_legend() is not None 7 | 8 | 9 | def get_legend_text(obj): 10 | """Check if line is in legend.""" 11 | leg = obj.axes.get_legend() 12 | if leg is None: 13 | return None 14 | 15 | keys = [h.get_label() for h in leg.legendHandles if h is not None] 16 | values = [t.get_text() for t in leg.texts] 17 | 18 | label = obj.get_label() 19 | d = dict(zip(keys, values)) 20 | if label in d: 21 | return d[label] 22 | 23 | return None 24 | 25 | 26 | def transform_to_data_coordinates(obj, xdata, ydata): 27 | """The coordinates might not be in data coordinates, but could be sometimes in axes 28 | coordinates. For example, the matplotlib command 29 | axes.axvline(2) 30 | will have the y coordinates set to 0 and 1, not to the limits. Therefore, a 31 | two-stage transform has to be applied: 32 | 1. first transforming to display coordinates, then 33 | 2. from display to data. 34 | """ 35 | if obj.axes is not None and obj.get_transform() != obj.axes.transData: 36 | points = np.array([xdata, ydata]).T 37 | transform = matplotlib.transforms.composite_transform_factory( 38 | obj.get_transform(), obj.axes.transData.inverted() 39 | ) 40 | return transform.transform(points).T 41 | return xdata, ydata 42 | -------------------------------------------------------------------------------- /tests/.gitignore: -------------------------------------------------------------------------------- 1 | tex/ 2 | zzz_readme_test.py 3 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nschloe/tikzplotlib/450712b4014799ec5f151f234df84335c90f4b9d/tests/__init__.py -------------------------------------------------------------------------------- /tests/helpers.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pathlib 3 | import subprocess 4 | import tempfile 5 | 6 | import matplotlib 7 | import matplotlib.pyplot as plt 8 | 9 | import tikzplotlib 10 | 11 | 12 | def print_tree(obj, indent=""): 13 | """Recursively prints the tree structure of the matplotlib object.""" 14 | if isinstance(obj, matplotlib.text.Text): 15 | print(indent, type(obj).__name__, f'("{obj.get_text()}")') 16 | else: 17 | print(indent, type(obj).__name__) 18 | 19 | for child in obj.get_children(): 20 | print_tree(child, indent + " ") 21 | 22 | 23 | # https://stackoverflow.com/a/845432/353337 24 | def _unidiff_output(expected, actual): 25 | import difflib 26 | 27 | expected = expected.splitlines(1) 28 | actual = actual.splitlines(1) 29 | diff = difflib.unified_diff(expected, actual) 30 | return "".join(diff) 31 | 32 | 33 | def assert_equality( 34 | plot, filename, assert_compilation=False, flavor="latex", **extra_get_tikz_code_args 35 | ): 36 | plot() 37 | code = tikzplotlib.get_tikz_code( 38 | include_disclaimer=False, 39 | float_format=".8g", 40 | flavor=flavor, 41 | **extra_get_tikz_code_args, 42 | ) 43 | plt.close("all") 44 | 45 | this_dir = pathlib.Path(__file__).resolve().parent 46 | with open(this_dir / filename, encoding="utf-8") as f: 47 | reference = f.read() 48 | assert reference == code, filename + "\n" + _unidiff_output(reference, code) 49 | 50 | if assert_compilation: 51 | plot() 52 | code = tikzplotlib.get_tikz_code( 53 | include_disclaimer=False, 54 | standalone=True, 55 | flavor=flavor, 56 | **extra_get_tikz_code_args, 57 | ) 58 | plt.close("all") 59 | assert _compile(code, flavor) is not None, code 60 | 61 | 62 | def _compile(code, flavor): 63 | _, tmp_base = tempfile.mkstemp() 64 | 65 | tex_file = tmp_base + ".tex" 66 | with open(tex_file, "w", encoding="utf-8") as f: 67 | f.write(code) 68 | 69 | # change into the directory of the TeX file 70 | os.chdir(os.path.dirname(tex_file)) 71 | 72 | # compile the output to pdf 73 | cmdline = dict( 74 | latex=["pdflatex", "--interaction=nonstopmode"], 75 | context=["context", "--nonstopmode"], 76 | )[flavor] 77 | try: 78 | subprocess.check_output(cmdline + [tex_file], stderr=subprocess.STDOUT) 79 | except subprocess.CalledProcessError as e: 80 | print(f"{cmdline[0]} output:") 81 | print("=" * 70) 82 | print(e.output.decode("utf-8")) 83 | print("=" * 70) 84 | output_pdf = None 85 | else: 86 | output_pdf = tmp_base + ".pdf" 87 | 88 | return output_pdf 89 | 90 | 91 | def compare_mpl_tex(plot, flavor="latex"): 92 | plot() 93 | code = tikzplotlib.get_tikz_code(standalone=True) 94 | directory = os.getcwd() 95 | filename = "test-0.png" 96 | plt.savefig(filename) 97 | plt.close("all") 98 | 99 | pdf_file = _compile(code, flavor) 100 | pdf_dirname = os.path.dirname(pdf_file) 101 | 102 | # Convert PDF to PNG. 103 | subprocess.check_output( 104 | ["pdftoppm", "-r", "1000", "-png", pdf_file, "test"], stderr=subprocess.STDOUT 105 | ) 106 | png_path = os.path.join(pdf_dirname, "test-1.png") 107 | 108 | os.rename(png_path, os.path.join(directory, "test-1.png")) 109 | -------------------------------------------------------------------------------- /tests/lena.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nschloe/tikzplotlib/450712b4014799ec5f151f234df84335c90f4b9d/tests/lena.png -------------------------------------------------------------------------------- /tests/refresh_reference_files.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import importlib.util 3 | import pathlib 4 | 5 | import matplotlib.pyplot as plt 6 | 7 | import tikzplotlib as tpl 8 | 9 | 10 | def _main(): 11 | parser = argparse.ArgumentParser(description="Refresh all reference TeX files.") 12 | parser.parse_args() 13 | 14 | this_dir = pathlib.Path(__file__).resolve().parent 15 | 16 | test_files = [ 17 | f 18 | for f in this_dir.iterdir() 19 | if (this_dir / f).is_file() and f.name[:5] == "test_" and f.name[-3:] == ".py" 20 | ] 21 | test_modules = [f.name[:-3] for f in test_files] 22 | 23 | # remove some edge cases 24 | test_modules.remove("test_rotated_labels") 25 | test_modules.remove("test_deterministic_output") 26 | test_modules.remove("test_cleanfigure") 27 | test_modules.remove("test_context") 28 | 29 | for mod in test_modules: 30 | module = importlib.import_module(mod) 31 | module.plot() 32 | 33 | code = tpl.get_tikz_code(include_disclaimer=False, float_format=".8g") 34 | plt.close("all") 35 | 36 | tex_filename = mod + "_reference.tex" 37 | with open(this_dir / tex_filename, "w", encoding="utf8") as f: 38 | f.write(code) 39 | 40 | 41 | if __name__ == "__main__": 42 | _main() 43 | -------------------------------------------------------------------------------- /tests/rotated_labels.py: -------------------------------------------------------------------------------- 1 | desc = "Rotated labels" 2 | phash = "a91fa2a3713dcc0b" 3 | 4 | 5 | def plot(): 6 | from matplotlib import pyplot as plt 7 | 8 | x = [1, 2, 3, 4] 9 | y = [1, 4, 9, 6] 10 | 11 | fig = plt.figure() 12 | 13 | plt.subplot(611) 14 | plt.plot(x, y, "ro") 15 | plt.xticks(x, rotation="horizontal") 16 | 17 | plt.subplot(612) 18 | plt.plot(x, y, "ro") 19 | plt.xticks(x, rotation="vertical") 20 | 21 | ax = plt.subplot(613) 22 | ax.plot(x, y, "ro") 23 | plt.xticks(y, rotation=-25) 24 | 25 | ax = plt.subplot(614) 26 | ax.plot(x, y, "ro") 27 | plt.yticks(y, rotation=-25) 28 | 29 | ax = plt.subplot(615) 30 | ax.plot(x, y, "ro") 31 | 32 | for idx, label in enumerate(ax.get_xticklabels()): 33 | label.set_rotation(45 if idx % 2 == 0 else 0) 34 | 35 | ax = plt.subplot(616) 36 | ax.plot(x, y, "ro") 37 | 38 | for idx, label in enumerate(ax.get_yticklabels()): 39 | label.set_rotation(90 if idx % 2 == 0 else 0) 40 | 41 | return fig 42 | -------------------------------------------------------------------------------- /tests/test_annotate.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | fig = plt.figure(1, figsize=(8, 5)) 7 | ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1, 5), ylim=(-4, 3)) 8 | t = np.arange(0.0, 5.0, 0.2) 9 | s = np.cos(2 * np.pi * t) 10 | ax.plot(t, s, color="blue") 11 | ax.annotate( 12 | "text", 13 | xy=(4.0, 1.0), 14 | xycoords="data", 15 | xytext=(4.5, 1.5), 16 | textcoords="data", 17 | arrowprops=dict(arrowstyle="->", ec="r"), 18 | ) 19 | ax.annotate( 20 | "arrowstyle", 21 | xy=(0, 1), 22 | xycoords="data", 23 | xytext=(-50, 30), 24 | textcoords="offset points", 25 | arrowprops=dict(arrowstyle="->"), 26 | ) 27 | ax.annotate( 28 | "no arrow", 29 | xy=(0, 1), 30 | xycoords="data", 31 | xytext=(50, -30), 32 | textcoords="offset pixels", 33 | ) 34 | return fig 35 | 36 | 37 | def test(): 38 | from .helpers import assert_equality 39 | 40 | assert_equality(plot, __file__[:-3] + "_reference.tex") 41 | 42 | 43 | if __name__ == "__main__": 44 | plot() 45 | plt.show() 46 | -------------------------------------------------------------------------------- /tests/test_annotate_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | tick align=outside, 7 | tick pos=left, 8 | x grid style={darkgray176}, 9 | xmin=-1, xmax=5, 10 | xtick style={color=black}, 11 | y grid style={darkgray176}, 12 | ymin=-4, ymax=3, 13 | ytick style={color=black} 14 | ] 15 | \addplot [semithick, blue] 16 | table {% 17 | 0 1 18 | 0.2 0.30901699 19 | 0.4 -0.80901699 20 | 0.6 -0.80901699 21 | 0.8 0.30901699 22 | 1 1 23 | 1.2 0.30901699 24 | 1.4 -0.80901699 25 | 1.6 -0.80901699 26 | 1.8 0.30901699 27 | 2 1 28 | 2.2 0.30901699 29 | 2.4 -0.80901699 30 | 2.6 -0.80901699 31 | 2.8 0.30901699 32 | 3 1 33 | 3.2 0.30901699 34 | 3.4 -0.80901699 35 | 3.6 -0.80901699 36 | 3.8 0.30901699 37 | 4 1 38 | 4.2 0.30901699 39 | 4.4 -0.80901699 40 | 4.6 -0.80901699 41 | 4.8 0.30901699 42 | }; 43 | \draw[->,draw=red] (axis cs:4.5,1.5) -- (axis cs:4,1); 44 | \draw (axis cs:4.5,1.5) node[ 45 | scale=0.5, 46 | anchor=base west, 47 | text=black, 48 | rotate=0.0 49 | ]{text}; 50 | \draw[->,draw=black] (axis cs:0,1) ++(-50pt,30pt) -- (axis cs:0,1); 51 | \draw (axis cs:0,1) ++(-50pt,30pt) node[ 52 | scale=0.5, 53 | anchor=base west, 54 | text=black, 55 | rotate=0.0 56 | ]{arrowstyle}; 57 | \draw (axis cs:50,-30) node[ 58 | scale=0.5, 59 | anchor=base west, 60 | text=black, 61 | rotate=0.0 62 | ]{no arrow}; 63 | \end{axis} 64 | 65 | \end{tikzpicture} 66 | -------------------------------------------------------------------------------- /tests/test_arrows.py: -------------------------------------------------------------------------------- 1 | import matplotlib.patches as mpatches 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | # https://matplotlib.org/examples/pylab_examples/fancyarrow_demo.html 6 | def plot(): 7 | styles = mpatches.ArrowStyle.get_styles() 8 | 9 | ncol = 2 10 | nrow = (len(styles) + 1) // ncol 11 | figheight = nrow + 0.5 12 | fig1 = plt.figure(1, (4.0 * ncol / 1.5, figheight / 1.5)) 13 | fontsize = 0.2 * 70 14 | 15 | ax = fig1.add_axes([0, 0, 1, 1], frameon=False, aspect=1.0) 16 | 17 | ax.set_xlim(0, 4 * ncol) 18 | ax.set_ylim(0, figheight) 19 | 20 | def to_texstring(s): 21 | s = s.replace("<", r"$<$") 22 | s = s.replace(">", r"$>$") 23 | s = s.replace("|", r"$|$") 24 | return s 25 | 26 | for i, (stylename, styleclass) in enumerate(sorted(styles.items())): 27 | x = 3.2 + (i // nrow) * 4 28 | y = figheight - 0.7 - i % nrow # /figheight 29 | p = mpatches.Circle((x, y), 0.2) 30 | ax.add_patch(p) 31 | 32 | ax.annotate( 33 | to_texstring(stylename), 34 | (x, y), 35 | (x - 1.2, y), 36 | # xycoords="figure fraction", textcoords="figure fraction", 37 | ha="right", 38 | va="center", 39 | size=fontsize, 40 | arrowprops=dict( 41 | arrowstyle=stylename, 42 | patchB=p, 43 | shrinkA=5, 44 | shrinkB=5, 45 | fc="k", 46 | ec="k", 47 | connectionstyle="arc3,rad=-0.05", 48 | ), 49 | bbox=dict(boxstyle="square", fc="w"), 50 | ) 51 | 52 | ax.xaxis.set_visible(False) 53 | ax.yaxis.set_visible(False) 54 | return plt.gcf() 55 | 56 | 57 | # if __name__ == "__main__": 58 | # import helpers 59 | # 60 | # # fig = plot() 61 | # # helpers.print_tree(fig) 62 | # # plt.show() 63 | # 64 | # plot() 65 | # import tikzplotlib 66 | # code = tikzplotlib.get_tikz_code(include_disclaimer=False, standalone=True) 67 | # plt.close() 68 | # helpers._does_compile(code) 69 | -------------------------------------------------------------------------------- /tests/test_arrows_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{steelblue31119180}{RGB}{31,119,180} 5 | 6 | \begin{axis}[ 7 | tick align=outside, 8 | tick pos=left, 9 | x grid style={darkgray176}, 10 | xmin=0, xmax=8, 11 | xtick style={color=black}, 12 | y grid style={darkgray176}, 13 | ymin=0, ymax=8.5, 14 | ytick style={color=black} 15 | ] 16 | \draw[draw=none,fill=steelblue31119180] (axis cs:3.2,7.8) circle (0.2); 17 | \draw[draw=none,fill=steelblue31119180] (axis cs:3.2,6.8) circle (0.2); 18 | \draw[draw=none,fill=steelblue31119180] (axis cs:3.2,5.8) circle (0.2); 19 | \draw[draw=none,fill=steelblue31119180] (axis cs:3.2,4.8) circle (0.2); 20 | \draw[draw=none,fill=steelblue31119180] (axis cs:3.2,3.8) circle (0.2); 21 | \draw[draw=none,fill=steelblue31119180] (axis cs:3.2,2.8) circle (0.2); 22 | \draw[draw=none,fill=steelblue31119180] (axis cs:3.2,1.8) circle (0.2); 23 | \draw[draw=none,fill=steelblue31119180] (axis cs:3.2,0.8) circle (0.2); 24 | \draw[draw=none,fill=steelblue31119180] (axis cs:7.2,7.8) circle (0.2); 25 | \draw[draw=none,fill=steelblue31119180] (axis cs:7.2,6.8) circle (0.2); 26 | \draw[draw=none,fill=steelblue31119180] (axis cs:7.2,5.8) circle (0.2); 27 | \draw[draw=none,fill=steelblue31119180] (axis cs:7.2,4.8) circle (0.2); 28 | \draw[draw=none,fill=steelblue31119180] (axis cs:7.2,3.8) circle (0.2); 29 | \draw[draw=none,fill=steelblue31119180] (axis cs:7.2,2.8) circle (0.2); 30 | \draw[draw=none,fill=steelblue31119180] (axis cs:7.2,1.8) circle (0.2); 31 | \draw[draw=none,fill=steelblue31119180] (axis cs:7.2,0.8) circle (0.2); 32 | \draw[-,draw=black] (axis cs:2,7.8) -- (axis cs:3.2,7.8); 33 | \draw (axis cs:2,7.8) node[ 34 | scale=0.7, 35 | fill=white, 36 | draw=black, 37 | line width=0.4pt, 38 | inner sep=3pt, 39 | anchor=east, 40 | text=black, 41 | rotate=0.0 42 | ]{-}; 43 | \draw[->,draw=black] (axis cs:2,6.8) -- (axis cs:3.2,6.8); 44 | \draw (axis cs:2,6.8) node[ 45 | scale=0.7, 46 | fill=white, 47 | draw=black, 48 | line width=0.4pt, 49 | inner sep=3pt, 50 | anchor=east, 51 | text=black, 52 | rotate=0.0 53 | ]{-$>$}; 54 | \draw[-|,draw=black] (axis cs:2,5.8) -- (axis cs:3.2,5.8); 55 | \draw (axis cs:2,5.8) node[ 56 | scale=0.7, 57 | fill=white, 58 | draw=black, 59 | line width=0.4pt, 60 | inner sep=3pt, 61 | anchor=east, 62 | text=black, 63 | rotate=0.0 64 | ]{-[}; 65 | \draw[-latex,draw=black] (axis cs:2,4.8) -- (axis cs:3.2,4.8); 66 | \draw (axis cs:2,4.8) node[ 67 | scale=0.7, 68 | fill=white, 69 | draw=black, 70 | line width=0.4pt, 71 | inner sep=3pt, 72 | anchor=east, 73 | text=black, 74 | rotate=0.0 75 | ]{-$|$$>$}; 76 | \draw[<-,draw=black] (axis cs:2,3.8) -- (axis cs:3.2,3.8); 77 | \draw (axis cs:2,3.8) node[ 78 | scale=0.7, 79 | fill=white, 80 | draw=black, 81 | line width=0.4pt, 82 | inner sep=3pt, 83 | anchor=east, 84 | text=black, 85 | rotate=0.0 86 | ]{$<$-}; 87 | \draw[<->,draw=black] (axis cs:2,2.8) -- (axis cs:3.2,2.8); 88 | \draw (axis cs:2,2.8) node[ 89 | scale=0.7, 90 | fill=white, 91 | draw=black, 92 | line width=0.4pt, 93 | inner sep=3pt, 94 | anchor=east, 95 | text=black, 96 | rotate=0.0 97 | ]{$<$-$>$}; 98 | \draw[<-[,draw=black] (axis cs:2,1.8) -- (axis cs:3.2,1.8); 99 | \draw (axis cs:2,1.8) node[ 100 | scale=0.7, 101 | fill=white, 102 | draw=black, 103 | line width=0.4pt, 104 | inner sep=3pt, 105 | anchor=east, 106 | text=black, 107 | rotate=0.0 108 | ]{$<$-[}; 109 | \draw[latex-,draw=black] (axis cs:2,0.8) -- (axis cs:3.2,0.8); 110 | \draw (axis cs:2,0.8) node[ 111 | scale=0.7, 112 | fill=white, 113 | draw=black, 114 | line width=0.4pt, 115 | inner sep=3pt, 116 | anchor=east, 117 | text=black, 118 | rotate=0.0 119 | ]{$<$$|$-}; 120 | \draw[latex-latex,draw=black] (axis cs:6,7.8) -- (axis cs:7.2,7.8); 121 | \draw (axis cs:6,7.8) node[ 122 | scale=0.7, 123 | fill=white, 124 | draw=black, 125 | line width=0.4pt, 126 | inner sep=3pt, 127 | anchor=east, 128 | text=black, 129 | rotate=0.0 130 | ]{$<$$|$-$|$$>$}; 131 | \draw[|-,draw=black] (axis cs:6,6.8) -- (axis cs:7.2,6.8); 132 | \draw (axis cs:6,6.8) node[ 133 | scale=0.7, 134 | fill=white, 135 | draw=black, 136 | line width=0.4pt, 137 | inner sep=3pt, 138 | anchor=east, 139 | text=black, 140 | rotate=0.0 141 | ]{]-}; 142 | \draw[]->,draw=black] (axis cs:6,5.8) -- (axis cs:7.2,5.8); 143 | \draw (axis cs:6,5.8) node[ 144 | scale=0.7, 145 | fill=white, 146 | draw=black, 147 | line width=0.4pt, 148 | inner sep=3pt, 149 | anchor=east, 150 | text=black, 151 | rotate=0.0 152 | ]{]-$>$}; 153 | \draw[|-|,draw=black] (axis cs:6,4.8) -- (axis cs:7.2,4.8); 154 | \draw (axis cs:6,4.8) node[ 155 | scale=0.7, 156 | fill=white, 157 | draw=black, 158 | line width=0.4pt, 159 | inner sep=3pt, 160 | anchor=east, 161 | text=black, 162 | rotate=0.0 163 | ]{]-[}; 164 | \draw[-latex,very thick,draw=black] (axis cs:6,3.8) -- (axis cs:7.2,3.8); 165 | \draw (axis cs:6,3.8) node[ 166 | scale=0.7, 167 | fill=white, 168 | draw=black, 169 | line width=0.4pt, 170 | inner sep=3pt, 171 | anchor=east, 172 | text=black, 173 | rotate=0.0 174 | ]{fancy}; 175 | \draw[-latex,very thick,draw=black] (axis cs:6,2.8) -- (axis cs:7.2,2.8); 176 | \draw (axis cs:6,2.8) node[ 177 | scale=0.7, 178 | fill=white, 179 | draw=black, 180 | line width=0.4pt, 181 | inner sep=3pt, 182 | anchor=east, 183 | text=black, 184 | rotate=0.0 185 | ]{simple}; 186 | \draw[-latex,very thick,draw=black] (axis cs:6,1.8) -- (axis cs:7.2,1.8); 187 | \draw (axis cs:6,1.8) node[ 188 | scale=0.7, 189 | fill=white, 190 | draw=black, 191 | line width=0.4pt, 192 | inner sep=3pt, 193 | anchor=east, 194 | text=black, 195 | rotate=0.0 196 | ]{wedge}; 197 | \draw[|-|,draw=black] (axis cs:6,0.8) -- (axis cs:7.2,0.8); 198 | \draw (axis cs:6,0.8) node[ 199 | scale=0.7, 200 | fill=white, 201 | draw=black, 202 | line width=0.4pt, 203 | inner sep=3pt, 204 | anchor=east, 205 | text=black, 206 | rotate=0.0 207 | ]{$|$-$|$}; 208 | \end{axis} 209 | 210 | \end{tikzpicture} 211 | -------------------------------------------------------------------------------- /tests/test_axvline.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | fig = plt.figure() 7 | np.random.seed(123) 8 | s = np.random.normal(0, 1, 10) 9 | plt.gca().set_ylim(-1.0, +1.0) 10 | plt.hist(s, 30) 11 | plt.axvline(1.96) 12 | return fig 13 | 14 | 15 | def test(): 16 | from .helpers import assert_equality 17 | 18 | assert_equality(plot, __file__[:-3] + "_reference.tex") 19 | -------------------------------------------------------------------------------- /tests/test_axvline_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{steelblue31119180}{RGB}{31,119,180} 5 | 6 | \begin{axis}[ 7 | tick align=outside, 8 | tick pos=left, 9 | x grid style={darkgray176}, 10 | xmin=-2.6460132, xmax=2.179334, 11 | xtick style={color=black}, 12 | y grid style={darkgray176}, 13 | ymin=-1, ymax=1, 14 | ytick style={color=black} 15 | ] 16 | \draw[draw=none,fill=steelblue31119180] (axis cs:-2.4266792,0) rectangle (axis cs:-2.2907421,1); 17 | \draw[draw=none,fill=steelblue31119180] (axis cs:-2.2907421,0) rectangle (axis cs:-2.1548049,0); 18 | \draw[draw=none,fill=steelblue31119180] (axis cs:-2.1548049,0) rectangle (axis cs:-2.0188677,0); 19 | \draw[draw=none,fill=steelblue31119180] (axis cs:-2.0188677,0) rectangle (axis cs:-1.8829305,0); 20 | \draw[draw=none,fill=steelblue31119180] (axis cs:-1.8829305,0) rectangle (axis cs:-1.7469933,0); 21 | \draw[draw=none,fill=steelblue31119180] (axis cs:-1.7469933,0) rectangle (axis cs:-1.6110561,0); 22 | \draw[draw=none,fill=steelblue31119180] (axis cs:-1.6110561,0) rectangle (axis cs:-1.4751189,1); 23 | \draw[draw=none,fill=steelblue31119180] (axis cs:-1.4751189,0) rectangle (axis cs:-1.3391817,0); 24 | \draw[draw=none,fill=steelblue31119180] (axis cs:-1.3391817,0) rectangle (axis cs:-1.2032445,0); 25 | \draw[draw=none,fill=steelblue31119180] (axis cs:-1.2032445,0) rectangle (axis cs:-1.0673073,1); 26 | \draw[draw=none,fill=steelblue31119180] (axis cs:-1.0673073,0) rectangle (axis cs:-0.93137012,0); 27 | \draw[draw=none,fill=steelblue31119180] (axis cs:-0.93137012,0) rectangle (axis cs:-0.79543293,1); 28 | \draw[draw=none,fill=steelblue31119180] (axis cs:-0.79543293,0) rectangle (axis cs:-0.65949574,0); 29 | \draw[draw=none,fill=steelblue31119180] (axis cs:-0.65949574,0) rectangle (axis cs:-0.52355855,1); 30 | \draw[draw=none,fill=steelblue31119180] (axis cs:-0.52355855,0) rectangle (axis cs:-0.38762135,1); 31 | \draw[draw=none,fill=steelblue31119180] (axis cs:-0.38762135,0) rectangle (axis cs:-0.25168416,0); 32 | \draw[draw=none,fill=steelblue31119180] (axis cs:-0.25168416,0) rectangle (axis cs:-0.11574697,0); 33 | \draw[draw=none,fill=steelblue31119180] (axis cs:-0.11574697,0) rectangle (axis cs:0.020190225,0); 34 | \draw[draw=none,fill=steelblue31119180] (axis cs:0.020190225,0) rectangle (axis cs:0.15612742,0); 35 | \draw[draw=none,fill=steelblue31119180] (axis cs:0.15612742,0) rectangle (axis cs:0.29206461,1); 36 | \draw[draw=none,fill=steelblue31119180] (axis cs:0.29206461,0) rectangle (axis cs:0.4280018,0); 37 | \draw[draw=none,fill=steelblue31119180] (axis cs:0.4280018,0) rectangle (axis cs:0.563939,0); 38 | \draw[draw=none,fill=steelblue31119180] (axis cs:0.563939,0) rectangle (axis cs:0.69987619,0); 39 | \draw[draw=none,fill=steelblue31119180] (axis cs:0.69987619,0) rectangle (axis cs:0.83581338,0); 40 | \draw[draw=none,fill=steelblue31119180] (axis cs:0.83581338,0) rectangle (axis cs:0.97175057,0); 41 | \draw[draw=none,fill=steelblue31119180] (axis cs:0.97175057,0) rectangle (axis cs:1.1076878,1); 42 | \draw[draw=none,fill=steelblue31119180] (axis cs:1.1076878,0) rectangle (axis cs:1.243625,0); 43 | \draw[draw=none,fill=steelblue31119180] (axis cs:1.243625,0) rectangle (axis cs:1.3795622,1); 44 | \draw[draw=none,fill=steelblue31119180] (axis cs:1.3795622,0) rectangle (axis cs:1.5154993,0); 45 | \draw[draw=none,fill=steelblue31119180] (axis cs:1.5154993,0) rectangle (axis cs:1.6514365,1); 46 | \addplot [semithick, steelblue31119180] 47 | table {% 48 | 1.96 -1 49 | 1.96 1 50 | }; 51 | \end{axis} 52 | 53 | \end{tikzpicture} 54 | -------------------------------------------------------------------------------- /tests/test_barchart.py: -------------------------------------------------------------------------------- 1 | """Bar Chart test 2 | This tests plots a simple bar chart. Bar charts are plotted as 3 | rectangle patches witch are difficult to tell from other rectangle 4 | patches that should not be plotted in PGFPlots (e.g. axis, legend) 5 | """ 6 | import matplotlib.pyplot as plt 7 | import numpy as np 8 | 9 | 10 | def plot(): 11 | # plot data 12 | fig = plt.figure() 13 | ax = fig.add_subplot(111) 14 | 15 | x = np.arange(3) 16 | y1 = [1, 2, 3] 17 | y2 = [3, 2, 4] 18 | y3 = [5, 3, 1] 19 | w = 0.25 20 | 21 | ax.bar(x - w, y1, w, color="b", align="center") 22 | ax.bar(x, y2, w, color="g", align="center") 23 | ax.bar(x + w, y3, w, color="r", align="center") 24 | 25 | return fig 26 | 27 | 28 | def test(): 29 | from .helpers import assert_equality 30 | 31 | assert_equality(plot, "test_barchart_reference.tex") 32 | -------------------------------------------------------------------------------- /tests/test_barchart_errorbars.py: -------------------------------------------------------------------------------- 1 | """ Bar Chart With Errorbar test 2 | This tests plots a bar chart with error bars. The errorbars need to be drawn 3 | at the correct z-order to be successful. 4 | 5 | """ 6 | import matplotlib.pyplot as plt 7 | import numpy as np 8 | 9 | 10 | def plot(): 11 | # plot data 12 | fig = plt.figure() 13 | ax = fig.add_subplot(111) 14 | 15 | x = np.arange(3) 16 | y1 = [1, 2, 3] 17 | y1err = [0.1, 0.2, 0.5] 18 | y2 = [3, 2, 4] 19 | y2err = [0.4, 0.2, 0.5] 20 | y3 = [5, 3, 1] 21 | y3err = [0.1, 0.2, 0.1] 22 | w = 0.25 23 | 24 | errBarStyle = dict(ecolor="black", lw=5, capsize=8, capthick=5) 25 | 26 | ax.bar(x - w, y1, w, color="b", yerr=y1err, align="center", error_kw=errBarStyle) 27 | ax.bar(x, y2, w, color="g", yerr=y2err, align="center", error_kw=errBarStyle) 28 | ax.bar(x + w, y3, w, color="r", yerr=y3err, align="center", error_kw=errBarStyle) 29 | 30 | return fig 31 | 32 | 33 | def test(): 34 | from .helpers import assert_equality 35 | 36 | assert_equality(plot, "test_barchart_errorbars_reference.tex") 37 | -------------------------------------------------------------------------------- /tests/test_barchart_errorbars_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{green01270}{RGB}{0,127,0} 5 | 6 | \begin{axis}[ 7 | tick align=outside, 8 | tick pos=left, 9 | x grid style={darkgray176}, 10 | xmin=-0.5125, xmax=2.5125, 11 | xtick style={color=black}, 12 | y grid style={darkgray176}, 13 | ymin=0, ymax=5.355, 14 | ytick style={color=black} 15 | ] 16 | \draw[draw=none,fill=blue] (axis cs:-0.375,0) rectangle (axis cs:-0.125,1); 17 | \draw[draw=none,fill=blue] (axis cs:0.625,0) rectangle (axis cs:0.875,2); 18 | \draw[draw=none,fill=blue] (axis cs:1.625,0) rectangle (axis cs:1.875,3); 19 | \draw[draw=none,fill=green01270] (axis cs:-0.125,0) rectangle (axis cs:0.125,3); 20 | \draw[draw=none,fill=green01270] (axis cs:0.875,0) rectangle (axis cs:1.125,2); 21 | \draw[draw=none,fill=green01270] (axis cs:1.875,0) rectangle (axis cs:2.125,4); 22 | \draw[draw=none,fill=red] (axis cs:0.125,0) rectangle (axis cs:0.375,5); 23 | \draw[draw=none,fill=red] (axis cs:1.125,0) rectangle (axis cs:1.375,3); 24 | \draw[draw=none,fill=red] (axis cs:2.125,0) rectangle (axis cs:2.375,1); 25 | \path [draw=black, line width=2pt] 26 | (axis cs:-0.25,0.9) 27 | --(axis cs:-0.25,1.1); 28 | 29 | \path [draw=black, line width=2pt] 30 | (axis cs:0.75,1.8) 31 | --(axis cs:0.75,2.2); 32 | 33 | \path [draw=black, line width=2pt] 34 | (axis cs:1.75,2.5) 35 | --(axis cs:1.75,3.5); 36 | 37 | \addplot [line width=2pt, black, mark=-, mark size=8, mark options={solid}, only marks] 38 | table {% 39 | -0.25 0.9 40 | 0.75 1.8 41 | 1.75 2.5 42 | }; 43 | \addplot [line width=2pt, black, mark=-, mark size=8, mark options={solid}, only marks] 44 | table {% 45 | -0.25 1.1 46 | 0.75 2.2 47 | 1.75 3.5 48 | }; 49 | \path [draw=black, line width=2pt] 50 | (axis cs:0,2.6) 51 | --(axis cs:0,3.4); 52 | 53 | \path [draw=black, line width=2pt] 54 | (axis cs:1,1.8) 55 | --(axis cs:1,2.2); 56 | 57 | \path [draw=black, line width=2pt] 58 | (axis cs:2,3.5) 59 | --(axis cs:2,4.5); 60 | 61 | \addplot [line width=2pt, black, mark=-, mark size=8, mark options={solid}, only marks] 62 | table {% 63 | 0 2.6 64 | 1 1.8 65 | 2 3.5 66 | }; 67 | \addplot [line width=2pt, black, mark=-, mark size=8, mark options={solid}, only marks] 68 | table {% 69 | 0 3.4 70 | 1 2.2 71 | 2 4.5 72 | }; 73 | \path [draw=black, line width=2pt] 74 | (axis cs:0.25,4.9) 75 | --(axis cs:0.25,5.1); 76 | 77 | \path [draw=black, line width=2pt] 78 | (axis cs:1.25,2.8) 79 | --(axis cs:1.25,3.2); 80 | 81 | \path [draw=black, line width=2pt] 82 | (axis cs:2.25,0.9) 83 | --(axis cs:2.25,1.1); 84 | 85 | \addplot [line width=2pt, black, mark=-, mark size=8, mark options={solid}, only marks] 86 | table {% 87 | 0.25 4.9 88 | 1.25 2.8 89 | 2.25 0.9 90 | }; 91 | \addplot [line width=2pt, black, mark=-, mark size=8, mark options={solid}, only marks] 92 | table {% 93 | 0.25 5.1 94 | 1.25 3.2 95 | 2.25 1.1 96 | }; 97 | \end{axis} 98 | 99 | \end{tikzpicture} 100 | -------------------------------------------------------------------------------- /tests/test_barchart_legend.py: -------------------------------------------------------------------------------- 1 | """ Bar Chart Legend test 2 | This tests plots a simple bar chart. Bar charts are plotted as 3 | rectangle patches witch are difficult to tell from other rectangle 4 | patches that should not be plotted in PGFPlots (e.g. axis, legend) 5 | 6 | This also tests legends on barcharts. Which are difficult because 7 | in PGFPlots, they have no \\addplot, and thus legend must be 8 | manually added. 9 | """ 10 | import matplotlib.pyplot as plt 11 | import numpy as np 12 | 13 | 14 | def plot(): 15 | # plot data 16 | fig = plt.figure() 17 | ax = fig.add_subplot(111) 18 | 19 | x = np.arange(3) 20 | y1 = [1, 2, 3] 21 | y2 = [3, 2, 4] 22 | y3 = [5, 3, 1] 23 | w = 0.25 24 | 25 | ax.bar(x - w, y1, w, color="b", align="center", label="Data 1") 26 | ax.bar(x, y2, w, color="g", align="center", label="Data 2") 27 | ax.bar(x + w, y3, w, color="r", align="center", label="Data 3") 28 | ax.legend() 29 | 30 | return fig 31 | 32 | 33 | def test(): 34 | from .helpers import assert_equality 35 | 36 | assert_equality(plot, "test_barchart_legend_reference.tex") 37 | -------------------------------------------------------------------------------- /tests/test_barchart_legend_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{green01270}{RGB}{0,127,0} 5 | \definecolor{lightgray204}{RGB}{204,204,204} 6 | 7 | \begin{axis}[ 8 | legend cell align={left}, 9 | legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=lightgray204}, 10 | tick align=outside, 11 | tick pos=left, 12 | x grid style={darkgray176}, 13 | xmin=-0.5125, xmax=2.5125, 14 | xtick style={color=black}, 15 | y grid style={darkgray176}, 16 | ymin=0, ymax=5.25, 17 | ytick style={color=black} 18 | ] 19 | \draw[draw=none,fill=blue] (axis cs:-0.375,0) rectangle (axis cs:-0.125,1); 20 | \addlegendimage{ybar,ybar legend,draw=none,fill=blue} 21 | \addlegendentry{Data 1} 22 | 23 | \draw[draw=none,fill=blue] (axis cs:0.625,0) rectangle (axis cs:0.875,2); 24 | \draw[draw=none,fill=blue] (axis cs:1.625,0) rectangle (axis cs:1.875,3); 25 | \draw[draw=none,fill=green01270] (axis cs:-0.125,0) rectangle (axis cs:0.125,3); 26 | \addlegendimage{ybar,ybar legend,draw=none,fill=green01270} 27 | \addlegendentry{Data 2} 28 | 29 | \draw[draw=none,fill=green01270] (axis cs:0.875,0) rectangle (axis cs:1.125,2); 30 | \draw[draw=none,fill=green01270] (axis cs:1.875,0) rectangle (axis cs:2.125,4); 31 | \draw[draw=none,fill=red] (axis cs:0.125,0) rectangle (axis cs:0.375,5); 32 | \addlegendimage{ybar,ybar legend,draw=none,fill=red} 33 | \addlegendentry{Data 3} 34 | 35 | \draw[draw=none,fill=red] (axis cs:1.125,0) rectangle (axis cs:1.375,3); 36 | \draw[draw=none,fill=red] (axis cs:2.125,0) rectangle (axis cs:2.375,1); 37 | \end{axis} 38 | 39 | \end{tikzpicture} 40 | -------------------------------------------------------------------------------- /tests/test_barchart_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{green01270}{RGB}{0,127,0} 5 | 6 | \begin{axis}[ 7 | tick align=outside, 8 | tick pos=left, 9 | x grid style={darkgray176}, 10 | xmin=-0.5125, xmax=2.5125, 11 | xtick style={color=black}, 12 | y grid style={darkgray176}, 13 | ymin=0, ymax=5.25, 14 | ytick style={color=black} 15 | ] 16 | \draw[draw=none,fill=blue] (axis cs:-0.375,0) rectangle (axis cs:-0.125,1); 17 | \draw[draw=none,fill=blue] (axis cs:0.625,0) rectangle (axis cs:0.875,2); 18 | \draw[draw=none,fill=blue] (axis cs:1.625,0) rectangle (axis cs:1.875,3); 19 | \draw[draw=none,fill=green01270] (axis cs:-0.125,0) rectangle (axis cs:0.125,3); 20 | \draw[draw=none,fill=green01270] (axis cs:0.875,0) rectangle (axis cs:1.125,2); 21 | \draw[draw=none,fill=green01270] (axis cs:1.875,0) rectangle (axis cs:2.125,4); 22 | \draw[draw=none,fill=red] (axis cs:0.125,0) rectangle (axis cs:0.375,5); 23 | \draw[draw=none,fill=red] (axis cs:1.125,0) rectangle (axis cs:1.375,3); 24 | \draw[draw=none,fill=red] (axis cs:2.125,0) rectangle (axis cs:2.375,1); 25 | \end{axis} 26 | 27 | \end{tikzpicture} 28 | -------------------------------------------------------------------------------- /tests/test_basic_sin.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | fig = plt.figure() 7 | with plt.style.context("ggplot"): 8 | t = np.arange(0.0, 2.0, 0.1) 9 | s = np.sin(2 * np.pi * t) 10 | s2 = np.cos(2 * np.pi * t) 11 | A = plt.cm.jet(np.linspace(0, 1, 10)) 12 | plt.plot(t, s, "o-", lw=1.5, color=A[5], label="sin") 13 | plt.plot(t, s2, "o-", lw=3, alpha=0.3, label="cos") 14 | plt.xlabel("time(s)") 15 | # plt.xlabel('time(s) _ % $ \\') 16 | plt.ylabel("Voltage (mV)") 17 | plt.title("Simple plot $\\frac{\\alpha}{2}$") 18 | plt.legend() 19 | plt.grid(True) 20 | return fig 21 | 22 | 23 | def test(): 24 | from .helpers import assert_equality 25 | 26 | assert_equality(plot, "test_basic_sin_reference.tex") 27 | -------------------------------------------------------------------------------- /tests/test_basic_sin_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{chocolate2267451}{RGB}{226,74,51} 4 | \definecolor{dimgray85}{RGB}{85,85,85} 5 | \definecolor{gainsboro229}{RGB}{229,229,229} 6 | \definecolor{greenyellow17025576}{RGB}{170,255,76} 7 | \definecolor{lightgray204}{RGB}{204,204,204} 8 | 9 | \begin{axis}[ 10 | axis background/.style={fill=gainsboro229}, 11 | axis line style={white}, 12 | legend cell align={left}, 13 | legend style={ 14 | fill opacity=0.8, 15 | draw opacity=1, 16 | text opacity=1, 17 | at={(0.03,0.03)}, 18 | anchor=south west, 19 | draw=lightgray204, 20 | fill=gainsboro229 21 | }, 22 | tick align=outside, 23 | tick pos=left, 24 | title={Simple plot \(\displaystyle \frac{\alpha}{2}\)}, 25 | x grid style={white}, 26 | xlabel=\textcolor{dimgray85}{time(s)}, 27 | xmajorgrids, 28 | xmin=-0.095, xmax=1.995, 29 | xtick style={color=dimgray85}, 30 | y grid style={white}, 31 | ylabel=\textcolor{dimgray85}{Voltage (mV)}, 32 | ymajorgrids, 33 | ymin=-1.1, ymax=1.1, 34 | ytick style={color=dimgray85} 35 | ] 36 | \addplot [semithick, greenyellow17025576, mark=*, mark size=3, mark options={solid}] 37 | table {% 38 | 0 0 39 | 0.1 0.58778525 40 | 0.2 0.95105652 41 | 0.3 0.95105652 42 | 0.4 0.58778525 43 | 0.5 1.2246468e-16 44 | 0.6 -0.58778525 45 | 0.7 -0.95105652 46 | 0.8 -0.95105652 47 | 0.9 -0.58778525 48 | 1 -2.4492936e-16 49 | 1.1 0.58778525 50 | 1.2 0.95105652 51 | 1.3 0.95105652 52 | 1.4 0.58778525 53 | 1.5 3.6739404e-16 54 | 1.6 -0.58778525 55 | 1.7 -0.95105652 56 | 1.8 -0.95105652 57 | 1.9 -0.58778525 58 | }; 59 | \addlegendentry{sin} 60 | \addplot [very thick, chocolate2267451, opacity=0.3, mark=*, mark size=3, mark options={solid}] 61 | table {% 62 | 0 1 63 | 0.1 0.80901699 64 | 0.2 0.30901699 65 | 0.3 -0.30901699 66 | 0.4 -0.80901699 67 | 0.5 -1 68 | 0.6 -0.80901699 69 | 0.7 -0.30901699 70 | 0.8 0.30901699 71 | 0.9 0.80901699 72 | 1 1 73 | 1.1 0.80901699 74 | 1.2 0.30901699 75 | 1.3 -0.30901699 76 | 1.4 -0.80901699 77 | 1.5 -1 78 | 1.6 -0.80901699 79 | 1.7 -0.30901699 80 | 1.8 0.30901699 81 | 1.9 0.80901699 82 | }; 83 | \addlegendentry{cos} 84 | \end{axis} 85 | 86 | \end{tikzpicture} 87 | -------------------------------------------------------------------------------- /tests/test_boxplot.py: -------------------------------------------------------------------------------- 1 | """ Box Plot test 2 | This test plots a box plot with three data series. The causes an empty Line2D 3 | to be plotted. Without care, this can turn into an empty table in PGFPlot 4 | which crashes latex (due to it treating an empty table as a table with 5 | external data in the file '' or '.tex') 6 | See: https://github.com/nschloe/tikzplotlib/pull/134 7 | """ 8 | import matplotlib.pyplot as plt 9 | 10 | 11 | def plot(): 12 | # plot data 13 | fig = plt.figure() 14 | ax = fig.add_subplot(111) 15 | 16 | data = [ 17 | [ 18 | 0.8792419963142024, 19 | 0.8842648555256405, 20 | 0.8830545971510088, 21 | 0.8831310510125482, 22 | 0.8839926059865629, 23 | 0.8795815040451961, 24 | 0.8780455489941472, 25 | 0.8785436398314896, 26 | 0.8830947020953477, 27 | 0.8853267660041949, 28 | 0.8888678711018956, 29 | 0.8852975957910832, 30 | 0.8806832729996307, 31 | 0.8757157004574541, 32 | 0.8767001155960863, 33 | 0.8840806038864472, 34 | 0.8817619814119265, 35 | 0.8888364252374024, 36 | 0.8812448127688732, 37 | 0.8831027782255365, 38 | ], 39 | [ 40 | 0.8977874209274417, 41 | 0.8941751386130553, 42 | 0.8896779411432865, 43 | 0.8971274869048325, 44 | 0.8974081692527065, 45 | 0.8942767272739647, 46 | 0.8875248054826029, 47 | 0.8777267389916926, 48 | 0.8950411839136605, 49 | 0.8927553406630346, 50 | 0.8950822278376636, 51 | 0.8987940094730611, 52 | 0.8921713177345106, 53 | 0.8875512496817447, 54 | 0.8897284821652239, 55 | 0.8910385725900226, 56 | 0.8879321741542129, 57 | 0.889056167587369, 58 | 0.884905350828982, 59 | 0.89214934207348, 60 | ], 61 | [ 62 | 0.8841888415170959, 63 | 0.8922931655807687, 64 | 0.8896153674950393, 65 | 0.8875992162118492, 66 | 0.890776178375901, 67 | 0.8889109386518265, 68 | 0.8879119743598638, 69 | 0.8912870099488378, 70 | 0.8981046527087161, 71 | 0.8920725720963792, 72 | 0.8841683225315845, 73 | 0.8857539590587772, 74 | 0.8945156112818913, 75 | 0.8894879283167035, 76 | 0.8912651966639861, 77 | 0.8929190818922158, 78 | 0.8943297597492411, 79 | 0.8888594626359189, 80 | 0.8912494597675972, 81 | 0.8917524004164856, 82 | ], 83 | ] 84 | 85 | ax.boxplot(data) 86 | 87 | return fig 88 | 89 | 90 | def test(): 91 | from .helpers import assert_equality 92 | 93 | assert_equality(plot, "test_boxplot_reference.tex") 94 | -------------------------------------------------------------------------------- /tests/test_boxplot_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{darkorange25512714}{RGB}{255,127,14} 5 | 6 | \begin{axis}[ 7 | tick align=outside, 8 | tick pos=left, 9 | x grid style={darkgray176}, 10 | xmin=0.5, xmax=3.5, 11 | xtick style={color=black}, 12 | y grid style={darkgray176}, 13 | ymin=0.87456179, ymax=0.89994792, 14 | ytick style={color=black} 15 | ] 16 | \addplot [black] 17 | table {% 18 | 0.85 0.87949663 19 | 1.15 0.87949663 20 | 1.15 0.88412667 21 | 0.85 0.88412667 22 | 0.85 0.87949663 23 | }; 24 | \addplot [black] 25 | table {% 26 | 1 0.87949663 27 | 1 0.8757157 28 | }; 29 | \addplot [black] 30 | table {% 31 | 1 0.88412667 32 | 1 0.88886787 33 | }; 34 | \addplot [black] 35 | table {% 36 | 0.925 0.8757157 37 | 1.075 0.8757157 38 | }; 39 | \addplot [black] 40 | table {% 41 | 0.925 0.88886787 42 | 1.075 0.88886787 43 | }; 44 | \addplot [black] 45 | table {% 46 | 1.85 0.88877517 47 | 2.15 0.88877517 48 | 2.15 0.89505144 49 | 1.85 0.89505144 50 | 1.85 0.88877517 51 | }; 52 | \addplot [black] 53 | table {% 54 | 2 0.88877517 55 | 2 0.88490535 56 | }; 57 | \addplot [black] 58 | table {% 59 | 2 0.89505144 60 | 2 0.89879401 61 | }; 62 | \addplot [black] 63 | table {% 64 | 1.925 0.88490535 65 | 2.075 0.88490535 66 | }; 67 | \addplot [black] 68 | table {% 69 | 1.925 0.89879401 70 | 2.075 0.89879401 71 | }; 72 | \addplot [black, mark=o, mark size=3, mark options={solid,fill opacity=0}, only marks] 73 | table {% 74 | 2 0.87772674 75 | }; 76 | \addplot [black] 77 | table {% 78 | 2.85 0.88862259 79 | 3.15 0.88862259 80 | 3.15 0.89212772 81 | 2.85 0.89212772 82 | 2.85 0.88862259 83 | }; 84 | \addplot [black] 85 | table {% 86 | 3 0.88862259 87 | 3 0.88416832 88 | }; 89 | \addplot [black] 90 | table {% 91 | 3 0.89212772 92 | 3 0.89451561 93 | }; 94 | \addplot [black] 95 | table {% 96 | 2.925 0.88416832 97 | 3.075 0.88416832 98 | }; 99 | \addplot [black] 100 | table {% 101 | 2.925 0.89451561 102 | 3.075 0.89451561 103 | }; 104 | \addplot [black, mark=o, mark size=3, mark options={solid,fill opacity=0}, only marks] 105 | table {% 106 | 3 0.89810465 107 | }; 108 | \addplot [darkorange25512714] 109 | table {% 110 | 0.85 0.88307465 111 | 1.15 0.88307465 112 | }; 113 | \addplot [darkorange25512714] 114 | table {% 115 | 1.85 0.89216033 116 | 2.15 0.89216033 117 | }; 118 | \addplot [darkorange25512714] 119 | table {% 120 | 2.85 0.89101282 121 | 3.15 0.89101282 122 | }; 123 | \end{axis} 124 | 125 | \end{tikzpicture} 126 | -------------------------------------------------------------------------------- /tests/test_colorbars.py: -------------------------------------------------------------------------------- 1 | import matplotlib as mpl 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def plot(): 6 | # Make a figure and axes with dimensions as desired. 7 | fig, ax = plt.subplots(3) 8 | 9 | # Set the colormap and norm to correspond to the data for which the colorbar will be 10 | # used. 11 | cmap = mpl.cm.cool 12 | norm = mpl.colors.Normalize(vmin=-5, vmax=10) 13 | 14 | # ColorbarBase derives from ScalarMappable and puts a colorbar in a specified axes, 15 | # so it has everything needed for a standalone colorbar. There are many more 16 | # kwargs, but the following gives a basic continuous colorbar with ticks and labels. 17 | cb1 = mpl.colorbar.ColorbarBase( 18 | ax[0], cmap=cmap, norm=norm, orientation="horizontal" 19 | ) 20 | cb1.set_label("Some Units") 21 | 22 | # The second example illustrates the use of a ListedColormap, a BoundaryNorm, and 23 | # extended ends to show the "over" and "under" value colors. 24 | cmap = mpl.colors.ListedColormap(["r", "g", "b", "c"]) 25 | cmap.set_over("0.25") 26 | cmap.set_under("0.75") 27 | 28 | # If a ListedColormap is used, the length of the bounds array must be one greater 29 | # than the length of the color list. The bounds must be monotonically increasing. 30 | bounds = [1, 2, 4, 7, 8] 31 | norm = mpl.colors.BoundaryNorm(bounds, cmap.N) 32 | cb2 = mpl.colorbar.ColorbarBase( 33 | ax[1], 34 | cmap=cmap, 35 | norm=norm, 36 | # to use 'extend', you must 37 | # specify two extra boundaries: 38 | boundaries=[0] + bounds + [13], 39 | extend="both", 40 | ticks=bounds, # optional 41 | spacing="proportional", 42 | orientation="horizontal", 43 | ) 44 | cb2.set_label("Discrete intervals, some other units") 45 | 46 | # The third example illustrates the use of custom length colorbar extensions, used 47 | # on a colorbar with discrete intervals. 48 | cmap = mpl.colors.ListedColormap( 49 | [[0.0, 0.4, 1.0], [0.0, 0.8, 1.0], [1.0, 0.8, 0.0], [1.0, 0.4, 0.0]] 50 | ) 51 | cmap.set_over((1.0, 0.0, 0.0)) 52 | cmap.set_under((0.0, 0.0, 1.0)) 53 | 54 | bounds = [-1.0, -0.5, 0.0, 0.5, 1.0] 55 | norm = mpl.colors.BoundaryNorm(bounds, cmap.N) 56 | cb3 = mpl.colorbar.ColorbarBase( 57 | ax[2], 58 | cmap=cmap, 59 | norm=norm, 60 | boundaries=[-10] + bounds + [10], 61 | extend="both", 62 | # Make the length of each extension 63 | # the same as the length of the 64 | # interior colors: 65 | extendfrac="auto", 66 | ticks=bounds, 67 | spacing="uniform", 68 | orientation="horizontal", 69 | ) 70 | cb3.set_label("Custom extension lengths, some other units") 71 | 72 | return fig 73 | 74 | 75 | def test(): 76 | from .helpers import assert_equality 77 | 78 | assert_equality(plot, "test_colorbars_reference.tex", assert_compilation=False) 79 | -------------------------------------------------------------------------------- /tests/test_colorbars_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \end{tikzpicture} 4 | -------------------------------------------------------------------------------- /tests/test_context.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import matplotlib.cm as cm 3 | import matplotlib.pyplot as plt 4 | import numpy as np 5 | 6 | x, y = np.meshgrid(np.linspace(0, 1), np.linspace(0, 1)) 7 | z = x**2 - y**2 8 | 9 | fig = plt.figure() 10 | plt.pcolormesh(x, y, z, cmap=cm.viridis, shading="gouraud") 11 | 12 | return fig 13 | 14 | 15 | def test(): 16 | from .helpers import assert_equality 17 | 18 | assert_equality(plot, __file__[:-3] + "_reference.tex", flavor="context") 19 | -------------------------------------------------------------------------------- /tests/test_context_reference.tex: -------------------------------------------------------------------------------- 1 | \starttikzpicture 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \startaxis[ 6 | tick align=outside, 7 | tick pos=left, 8 | x grid style={darkgray176}, 9 | xmin=0, xmax=1, 10 | xtick style={color=black}, 11 | y grid style={darkgray176}, 12 | ymin=0, ymax=1, 13 | ytick style={color=black} 14 | ] 15 | \addplot graphics [includegraphics cmd=\pgfimage,xmin=0, xmax=1, ymin=0, ymax=1] {tmp-000.png}; 16 | \stopaxis 17 | 18 | \stoptikzpicture 19 | -------------------------------------------------------------------------------- /tests/test_contourf.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | nbins = 5 7 | 8 | fig = plt.figure() 9 | ax = plt.gca() 10 | 11 | x_max = 2 12 | x_min = 0 13 | y_max = 2 14 | y_min = 0 15 | 16 | xi, yi = np.mgrid[x_min : x_max : nbins * 1j, y_min : y_max : nbins * 1j] 17 | pos = np.empty(xi.shape + (2,)) 18 | pos[:, :, 0] = xi 19 | pos[:, :, 1] = yi 20 | zi = 2 - (xi - 1) ** 2 - (yi - 1) ** 2 21 | ax.contourf(xi, yi, zi, levels=5) 22 | 23 | ax.set_xlim(x_min, x_max) 24 | ax.set_ylim(y_min, y_max) 25 | return fig 26 | 27 | 28 | def test(): 29 | from .helpers import assert_equality 30 | 31 | assert_equality(plot, __file__[:-3] + "_reference.tex") 32 | -------------------------------------------------------------------------------- /tests/test_contourf_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkcyan32144140}{RGB}{32,144,140} 4 | \definecolor{darkgray176}{RGB}{176,176,176} 5 | \definecolor{darkslateblue5294141}{RGB}{52,94,141} 6 | \definecolor{darkslateblue7235116}{RGB}{72,35,116} 7 | \definecolor{greenyellow18922238}{RGB}{189,222,38} 8 | \definecolor{mediumseagreen68190112}{RGB}{68,190,112} 9 | 10 | \begin{axis}[ 11 | tick align=outside, 12 | tick pos=left, 13 | x grid style={darkgray176}, 14 | xmin=0, xmax=2, 15 | xtick style={color=black}, 16 | y grid style={darkgray176}, 17 | ymin=0, ymax=2, 18 | ytick style={color=black} 19 | ] 20 | \addplot [draw=none, fill=darkslateblue7235116] 21 | table{% 22 | x y 23 | 0.26666667 0 24 | 0 0 25 | 0 0.26666667 26 | 0.26666667 0 27 | }; 28 | \addplot [draw=none, fill=darkslateblue7235116] 29 | table{% 30 | x y 31 | 0 2 32 | 0.26666667 2 33 | 0 1.7333333 34 | 0 2 35 | }; 36 | \addplot [draw=none, fill=darkslateblue7235116] 37 | table{% 38 | x y 39 | 2 0.26666667 40 | 2 0 41 | 1.7333333 0 42 | 2 0.26666667 43 | }; 44 | \addplot [draw=none, fill=darkslateblue7235116] 45 | table{% 46 | x y 47 | 1.7333333 2 48 | 2 2 49 | 2 1.7333333 50 | 1.7333333 2 51 | }; 52 | \addplot [draw=none, fill=darkslateblue5294141] 53 | table{% 54 | x y 55 | 0 0.5 56 | 0 0.6 57 | 0.033333333 0.5 58 | 0.5 0.033333333 59 | 0.6 0 60 | 0.5 0 61 | 0.26666667 0 62 | 0 0.26666667 63 | 0 0.5 64 | }; 65 | \addplot [draw=none, fill=darkslateblue5294141] 66 | table{% 67 | x y 68 | 0 1.5 69 | 0 1.7333333 70 | 0.26666667 2 71 | 0.5 2 72 | 0.6 2 73 | 0.5 1.9666667 74 | 0.033333333 1.5 75 | 0 1.4 76 | 0 1.5 77 | }; 78 | \addplot [draw=none, fill=darkslateblue5294141] 79 | table{% 80 | x y 81 | 1.5 0.033333333 82 | 1.9666667 0.5 83 | 2 0.6 84 | 2 0.5 85 | 2 0.26666667 86 | 1.7333333 0 87 | 1.5 0 88 | 1.4 0 89 | 1.5 0.033333333 90 | }; 91 | \addplot [draw=none, fill=darkslateblue5294141] 92 | table{% 93 | x y 94 | 1.4 2 95 | 1.5 2 96 | 1.7333333 2 97 | 2 1.7333333 98 | 2 1.5 99 | 2 1.4 100 | 1.9666667 1.5 101 | 1.5 1.9666667 102 | 1.4 2 103 | }; 104 | \addplot [draw=none, fill=darkcyan32144140] 105 | table{% 106 | x y 107 | 0.033333333 0.5 108 | 0 0.6 109 | 0 1 110 | 0 1.4 111 | 0.033333333 1.5 112 | 0.5 1.9666667 113 | 0.6 2 114 | 1 2 115 | 1.4 2 116 | 1.5 1.9666667 117 | 1.9666667 1.5 118 | 2 1.4 119 | 2 1 120 | 2 0.6 121 | 1.9666667 0.5 122 | 1.5 0.033333333 123 | 1.4 0 124 | 1 0 125 | 0.6 0 126 | 0.5 0.033333333 127 | 0.033333333 0.5 128 | 129 | 0.5 0.3 130 | 1 0.13333333 131 | 1.5 0.3 132 | 1.7 0.5 133 | 1.8666667 1 134 | 1.7 1.5 135 | 1.5 1.7 136 | 1 1.8666667 137 | 0.5 1.7 138 | 0.3 1.5 139 | 0.13333333 1 140 | 0.3 0.5 141 | 0.5 0.3 142 | }; 143 | \addplot [draw=none, fill=mediumseagreen68190112] 144 | table{% 145 | x y 146 | 0.3 0.5 147 | 0.13333333 1 148 | 0.3 1.5 149 | 0.5 1.7 150 | 1 1.8666667 151 | 1.5 1.7 152 | 1.7 1.5 153 | 1.8666667 1 154 | 1.7 0.5 155 | 1.5 0.3 156 | 1 0.13333333 157 | 0.5 0.3 158 | 0.3 0.5 159 | 160 | 0.5 0.7 161 | 0.7 0.5 162 | 1 0.4 163 | 1.3 0.5 164 | 1.5 0.7 165 | 1.6 1 166 | 1.5 1.3 167 | 1.3 1.5 168 | 1 1.6 169 | 0.7 1.5 170 | 0.5 1.3 171 | 0.4 1 172 | 0.5 0.7 173 | }; 174 | \addplot [draw=none, fill=greenyellow18922238] 175 | table{% 176 | x y 177 | 0.4 1 178 | 0.5 1.3 179 | 0.7 1.5 180 | 1 1.6 181 | 1.3 1.5 182 | 1.5 1.3 183 | 1.6 1 184 | 1.5 0.7 185 | 1.3 0.5 186 | 1 0.4 187 | 0.7 0.5 188 | 0.5 0.7 189 | 0.4 1 190 | }; 191 | \end{axis} 192 | 193 | \end{tikzpicture} 194 | -------------------------------------------------------------------------------- /tests/test_custom_collection.py: -------------------------------------------------------------------------------- 1 | """Custom collection test 2 | 3 | This tests plots a subclass of Collection, which contains enough information 4 | as a base class to be rendered. 5 | """ 6 | import matplotlib 7 | import matplotlib.pyplot as plt 8 | import numpy as np 9 | 10 | 11 | class TransformedEllipseCollection(matplotlib.collections.Collection): 12 | """ 13 | A gutted version of matplotlib.collections.EllipseCollection that lets us 14 | pass the transformation matrix directly. 15 | 16 | This is useful for plotting cholesky factors of covariance matrices. 17 | """ 18 | 19 | def __init__(self, matrices, **kwargs): 20 | super().__init__(**kwargs) 21 | self.set_transform(matplotlib.transforms.IdentityTransform()) 22 | self._transforms = np.zeros(matrices.shape[:-2] + (3, 3)) 23 | self._transforms[..., :2, :2] = matrices 24 | self._transforms[..., 2, 2] = 1 25 | self._paths = [matplotlib.path.Path.unit_circle()] 26 | 27 | def _set_transforms(self): 28 | """Calculate transforms immediately before drawing.""" 29 | m = self.axes.transData.get_affine().get_matrix().copy() 30 | m[:2, 2:] = 0 31 | self.set_transform(matplotlib.transforms.Affine2D(m)) 32 | 33 | @matplotlib.artist.allow_rasterization 34 | def draw(self, renderer): 35 | self._set_transforms() 36 | super().draw(renderer) 37 | 38 | 39 | def rot(theta): 40 | """Get a stack of rotation matrices""" 41 | return np.stack( 42 | [ 43 | np.stack([np.cos(theta), -np.sin(theta)], axis=-1), 44 | np.stack([np.sin(theta), np.cos(theta)], axis=-1), 45 | ], 46 | axis=-2, 47 | ) 48 | 49 | 50 | def plot(): 51 | # plot data 52 | fig = plt.figure() 53 | ax = fig.add_subplot(111) 54 | 55 | theta = np.linspace(0, 2 * np.pi, 6, endpoint=False) 56 | mats = rot(theta) @ np.diag([0.1, 0.2]) 57 | x = np.cos(theta) 58 | y = np.sin(theta) 59 | 60 | c = TransformedEllipseCollection( 61 | mats, 62 | offsets=np.stack((x, y), axis=-1), 63 | edgecolor="tab:red", 64 | alpha=0.5, 65 | facecolor="tab:blue", 66 | transOffset=ax.transData, 67 | ) 68 | ax.add_collection(c) 69 | ax.set(xlim=[-1.5, 1.5], ylim=[-1.5, 1.5]) 70 | 71 | return fig 72 | 73 | 74 | def test(): 75 | from .helpers import assert_equality 76 | 77 | assert_equality(plot, "test_custom_collection_reference.tex") 78 | -------------------------------------------------------------------------------- /tests/test_custom_collection_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{crimson2143940}{RGB}{214,39,40} 4 | \definecolor{darkgray176}{RGB}{176,176,176} 5 | \definecolor{steelblue31119180}{RGB}{31,119,180} 6 | 7 | \begin{axis}[ 8 | tick align=outside, 9 | tick pos=left, 10 | x grid style={darkgray176}, 11 | xmin=-1.5, xmax=1.5, 12 | xtick style={color=black}, 13 | y grid style={darkgray176}, 14 | ymin=-1.5, ymax=1.5, 15 | ytick style={color=black} 16 | ] 17 | \path [draw=crimson2143940, fill=steelblue31119180, opacity=0.5] 18 | (axis cs:1,-0.2) 19 | .. controls (axis cs:1.0265203,-0.2) and (axis cs:1.051958,-0.17892674) .. (axis cs:1.0707107,-0.14142136) 20 | .. controls (axis cs:1.0894634,-0.10391597) and (axis cs:1.1,-0.05304062) .. (axis cs:1.1,0) 21 | .. controls (axis cs:1.1,0.05304062) and (axis cs:1.0894634,0.10391597) .. (axis cs:1.0707107,0.14142136) 22 | .. controls (axis cs:1.051958,0.17892674) and (axis cs:1.0265203,0.2) .. (axis cs:1,0.2) 23 | .. controls (axis cs:0.97347969,0.2) and (axis cs:0.94804201,0.17892674) .. (axis cs:0.92928932,0.14142136) 24 | .. controls (axis cs:0.91053663,0.10391597) and (axis cs:0.9,0.05304062) .. (axis cs:0.9,0) 25 | .. controls (axis cs:0.9,-0.05304062) and (axis cs:0.91053663,-0.10391597) .. (axis cs:0.92928932,-0.14142136) 26 | .. controls (axis cs:0.94804201,-0.17892674) and (axis cs:0.97347969,-0.2) .. (axis cs:1,-0.2) 27 | --cycle; 28 | \path [draw=crimson2143940, fill=steelblue31119180, opacity=0.5] 29 | (axis cs:0.67320508,0.7660254) 30 | .. controls (axis cs:0.68646524,0.78899267) and (axis cs:0.68093409,0.82155897) .. (axis cs:0.65782983,0.85655197) 31 | .. controls (axis cs:0.63472556,0.89154497) and (axis cs:0.59593452,0.92610763) .. (axis cs:0.55,0.95262794) 32 | .. controls (axis cs:0.50406548,0.97914825) and (axis cs:0.45473781,0.99546094) .. (axis cs:0.41288085,0.99797333) 33 | .. controls (axis cs:0.37102389,1.0004857) and (axis cs:0.34005507,0.98899267) .. (axis cs:0.32679492,0.9660254) 34 | .. controls (axis cs:0.31353476,0.94305814) and (axis cs:0.31906591,0.91049184) .. (axis cs:0.34217017,0.87549884) 35 | .. controls (axis cs:0.36527444,0.84050584) and (axis cs:0.40406548,0.80594317) .. (axis cs:0.45,0.77942286) 36 | .. controls (axis cs:0.49593452,0.75290255) and (axis cs:0.54526219,0.73658987) .. (axis cs:0.58711915,0.73407748) 37 | .. controls (axis cs:0.62897611,0.7315651) and (axis cs:0.65994493,0.74305814) .. (axis cs:0.67320508,0.7660254) 38 | --cycle; 39 | \path [draw=crimson2143940, fill=steelblue31119180, opacity=0.5] 40 | (axis cs:-0.32679492,0.9660254) 41 | .. controls (axis cs:-0.34005507,0.98899267) and (axis cs:-0.37102389,1.0004857) .. (axis cs:-0.41288085,0.99797333) 42 | .. controls (axis cs:-0.45473781,0.99546094) and (axis cs:-0.50406548,0.97914825) .. (axis cs:-0.55,0.95262794) 43 | .. controls (axis cs:-0.59593452,0.92610763) and (axis cs:-0.63472556,0.89154497) .. (axis cs:-0.65782983,0.85655197) 44 | .. controls (axis cs:-0.68093409,0.82155897) and (axis cs:-0.68646524,0.78899267) .. (axis cs:-0.67320508,0.7660254) 45 | .. controls (axis cs:-0.65994493,0.74305814) and (axis cs:-0.62897611,0.7315651) .. (axis cs:-0.58711915,0.73407748) 46 | .. controls (axis cs:-0.54526219,0.73658987) and (axis cs:-0.49593452,0.75290255) .. (axis cs:-0.45,0.77942286) 47 | .. controls (axis cs:-0.40406548,0.80594317) and (axis cs:-0.36527444,0.84050584) .. (axis cs:-0.34217017,0.87549884) 48 | .. controls (axis cs:-0.31906591,0.91049184) and (axis cs:-0.31353476,0.94305814) .. (axis cs:-0.32679492,0.9660254) 49 | --cycle; 50 | \path [draw=crimson2143940, fill=steelblue31119180, opacity=0.5] 51 | (axis cs:-1,0.2) 52 | .. controls (axis cs:-1.0265203,0.2) and (axis cs:-1.051958,0.17892674) .. (axis cs:-1.0707107,0.14142136) 53 | .. controls (axis cs:-1.0894634,0.10391597) and (axis cs:-1.1,0.05304062) .. (axis cs:-1.1,1.3471115e-16) 54 | .. controls (axis cs:-1.1,-0.05304062) and (axis cs:-1.0894634,-0.10391597) .. (axis cs:-1.0707107,-0.14142136) 55 | .. controls (axis cs:-1.051958,-0.17892674) and (axis cs:-1.0265203,-0.2) .. (axis cs:-1,-0.2) 56 | .. controls (axis cs:-0.97347969,-0.2) and (axis cs:-0.94804201,-0.17892674) .. (axis cs:-0.92928932,-0.14142136) 57 | .. controls (axis cs:-0.91053663,-0.10391597) and (axis cs:-0.9,-0.05304062) .. (axis cs:-0.9,1.1021821e-16) 58 | .. controls (axis cs:-0.9,0.05304062) and (axis cs:-0.91053663,0.10391597) .. (axis cs:-0.92928932,0.14142136) 59 | .. controls (axis cs:-0.94804201,0.17892674) and (axis cs:-0.97347969,0.2) .. (axis cs:-1,0.2) 60 | --cycle; 61 | \path [draw=crimson2143940, fill=steelblue31119180, opacity=0.5] 62 | (axis cs:-0.67320508,-0.7660254) 63 | .. controls (axis cs:-0.68646524,-0.78899267) and (axis cs:-0.68093409,-0.82155897) .. (axis cs:-0.65782983,-0.85655197) 64 | .. controls (axis cs:-0.63472556,-0.89154497) and (axis cs:-0.59593452,-0.92610763) .. (axis cs:-0.55,-0.95262794) 65 | .. controls (axis cs:-0.50406548,-0.97914825) and (axis cs:-0.45473781,-0.99546094) .. (axis cs:-0.41288085,-0.99797333) 66 | .. controls (axis cs:-0.37102389,-1.0004857) and (axis cs:-0.34005507,-0.98899267) .. (axis cs:-0.32679492,-0.9660254) 67 | .. controls (axis cs:-0.31353476,-0.94305814) and (axis cs:-0.31906591,-0.91049184) .. (axis cs:-0.34217017,-0.87549884) 68 | .. controls (axis cs:-0.36527444,-0.84050584) and (axis cs:-0.40406548,-0.80594317) .. (axis cs:-0.45,-0.77942286) 69 | .. controls (axis cs:-0.49593452,-0.75290255) and (axis cs:-0.54526219,-0.73658987) .. (axis cs:-0.58711915,-0.73407748) 70 | .. controls (axis cs:-0.62897611,-0.7315651) and (axis cs:-0.65994493,-0.74305814) .. (axis cs:-0.67320508,-0.7660254) 71 | --cycle; 72 | \path [draw=crimson2143940, fill=steelblue31119180, opacity=0.5] 73 | (axis cs:0.32679492,-0.9660254) 74 | .. controls (axis cs:0.34005507,-0.98899267) and (axis cs:0.37102389,-1.0004857) .. (axis cs:0.41288085,-0.99797333) 75 | .. controls (axis cs:0.45473781,-0.99546094) and (axis cs:0.50406548,-0.97914825) .. (axis cs:0.55,-0.95262794) 76 | .. controls (axis cs:0.59593452,-0.92610763) and (axis cs:0.63472556,-0.89154497) .. (axis cs:0.65782983,-0.85655197) 77 | .. controls (axis cs:0.68093409,-0.82155897) and (axis cs:0.68646524,-0.78899267) .. (axis cs:0.67320508,-0.7660254) 78 | .. controls (axis cs:0.65994493,-0.74305814) and (axis cs:0.62897611,-0.7315651) .. (axis cs:0.58711915,-0.73407748) 79 | .. controls (axis cs:0.54526219,-0.73658987) and (axis cs:0.49593452,-0.75290255) .. (axis cs:0.45,-0.77942286) 80 | .. controls (axis cs:0.40406548,-0.80594317) and (axis cs:0.36527444,-0.84050584) .. (axis cs:0.34217017,-0.87549884) 81 | .. controls (axis cs:0.31906591,-0.91049184) and (axis cs:0.31353476,-0.94305814) .. (axis cs:0.32679492,-0.9660254) 82 | --cycle; 83 | 84 | \end{axis} 85 | 86 | \end{tikzpicture} 87 | -------------------------------------------------------------------------------- /tests/test_datetime.py: -------------------------------------------------------------------------------- 1 | import datetime as date 2 | 3 | import matplotlib.pyplot as plt 4 | from matplotlib import dates 5 | 6 | 7 | def plot(): 8 | fig = plt.figure() 9 | 10 | values = [50, 50.02] 11 | time = [date.datetime(2016, 10, 10, 18, 00), date.datetime(2016, 10, 10, 18, 15)] 12 | plt.plot(time, values) 13 | hfmt = dates.DateFormatter("%H:%M") 14 | ax = plt.gca() 15 | ax.xaxis.set_major_formatter(hfmt) 16 | return fig 17 | 18 | 19 | def test(): 20 | from .helpers import assert_equality 21 | 22 | assert_equality(plot, __file__[:-3] + "_reference.tex") 23 | -------------------------------------------------------------------------------- /tests/test_datetime_paths.py: -------------------------------------------------------------------------------- 1 | import datetime as date 2 | 3 | import matplotlib.pyplot as plt 4 | from matplotlib import dates 5 | 6 | 7 | def plot(): 8 | fig = plt.figure() 9 | 10 | times = [date.datetime(2020, 1, 1, 12, 00), date.datetime(2020, 1, 2, 12, 00)] 11 | line = [2, 2] 12 | upper = [3, 4] 13 | lower = [1, 0] 14 | 15 | plt.plot(times, line) 16 | plt.fill_between(times, lower, upper) 17 | ax = plt.gca() 18 | ax.fmt_xdata = dates.DateFormatter("%d %b %Y %H:%M:%S") 19 | 20 | return fig 21 | 22 | 23 | def test(): 24 | from .helpers import assert_equality 25 | 26 | assert_equality(plot, __file__[:-3] + "_reference.tex") 27 | -------------------------------------------------------------------------------- /tests/test_datetime_paths_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{steelblue31119180}{RGB}{31,119,180} 5 | 6 | \begin{axis}[ 7 | date coordinates in=x, 8 | tick align=outside, 9 | tick pos=left, 10 | x grid style={darkgray176}, 11 | xmin=2020-01-01 10:48, xmax=2020-01-02 13:12, 12 | xtick style={color=black}, 13 | y grid style={darkgray176}, 14 | ymin=-0.2, ymax=4.2, 15 | ytick style={color=black} 16 | ] 17 | \path [fill=steelblue31119180] 18 | (axis cs:18262.5,3) 19 | --(axis cs:18262.5,1) 20 | --(axis cs:18263.5,0) 21 | --(axis cs:18263.5,4) 22 | --(axis cs:18263.5,4) 23 | --(axis cs:18262.5,3) 24 | --cycle; 25 | 26 | \addplot [semithick, steelblue31119180] 27 | table [header=false,col sep=comma] {% 28 | 2020-01-01 12:00,2 29 | 2020-01-02 12:00,2 30 | }; 31 | \end{axis} 32 | 33 | \end{tikzpicture} 34 | -------------------------------------------------------------------------------- /tests/test_datetime_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{steelblue31119180}{RGB}{31,119,180} 5 | 6 | \begin{axis}[ 7 | date coordinates in=x, 8 | tick align=outside, 9 | tick pos=left, 10 | x grid style={darkgray176}, 11 | xmin=2016-10-10 17:59, xmax=2016-10-10 18:15, 12 | xtick style={color=black}, 13 | y grid style={darkgray176}, 14 | ymin=49.999, ymax=50.021, 15 | ytick style={color=black} 16 | ] 17 | \addplot [semithick, steelblue31119180] 18 | table [header=false,col sep=comma] {% 19 | 2016-10-10 18:00,50 20 | 2016-10-10 18:15,50.02 21 | }; 22 | \end{axis} 23 | 24 | \end{tikzpicture} 25 | -------------------------------------------------------------------------------- /tests/test_deterministic_output.py: -------------------------------------------------------------------------------- 1 | # assert repeated exports of the same plot produce the same output file 2 | 3 | import subprocess 4 | import sys 5 | import tempfile 6 | 7 | # We create the tikz files in separate subprocesses, as when producing those in 8 | # the same process, the order of axis parameters is deterministic. 9 | plot_code = """ 10 | import sys 11 | import numpy as np 12 | from matplotlib import pyplot as plt 13 | import tikzplotlib 14 | 15 | t = np.arange(0.0, 2.0, 0.1) 16 | s = np.sin(2 * np.pi * t) 17 | plt.plot(t, s, label="a") 18 | plt.legend() 19 | tikzplotlib.save(sys.argv[1]) 20 | """ 21 | 22 | 23 | def test(): 24 | _, tmp_base = tempfile.mkstemp() 25 | # trade-off between test duration and probability of false negative 26 | n_tests = 4 27 | tikzs = [] 28 | for _ in range(n_tests): 29 | tikz_file = tmp_base + "_tikz.tex" 30 | try: 31 | _ = subprocess.check_output( 32 | [sys.executable, "-", tikz_file], 33 | input=plot_code.encode(), 34 | stderr=subprocess.STDOUT, 35 | ) 36 | sp = subprocess.Popen( 37 | ["python3", "-", tikz_file], 38 | stdin=subprocess.PIPE, 39 | stdout=subprocess.PIPE, 40 | stderr=subprocess.STDOUT, 41 | ) 42 | _, _ = sp.communicate(plot_code.encode()) 43 | except subprocess.CalledProcessError as e: 44 | print("Command output:") 45 | print("=" * 70) 46 | print(e.output) 47 | print("=" * 70) 48 | raise 49 | with open(tikz_file) as f: 50 | tikzs.append(f.read()) 51 | for t in tikzs[1:]: 52 | assert t == tikzs[0] 53 | -------------------------------------------------------------------------------- /tests/test_dual_axis.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | fig = plt.figure() 7 | ax = fig.add_subplot(111) 8 | dat = np.linspace(0, 10, 20) 9 | ax.plot(dat, dat**2) 10 | ax2 = ax.twinx() 11 | ax2.plot(20 * dat, 20 * dat**2) 12 | ax.xaxis.tick_top() 13 | return fig 14 | 15 | 16 | def test(): 17 | from .helpers import assert_equality 18 | 19 | assert_equality(plot, "test_dual_axis_reference.tex") 20 | -------------------------------------------------------------------------------- /tests/test_dual_axis_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{steelblue31119180}{RGB}{31,119,180} 5 | 6 | \begin{axis}[ 7 | tick align=outside, 8 | x grid style={darkgray176}, 9 | xmin=-10, xmax=210, 10 | xtick pos=right, 11 | xtick style={color=black}, 12 | y grid style={darkgray176}, 13 | ymin=-5, ymax=105, 14 | ytick pos=left, 15 | ytick style={color=black} 16 | ] 17 | \addplot [semithick, steelblue31119180] 18 | table {% 19 | 0 0 20 | 0.52631579 0.27700831 21 | 1.0526316 1.1080332 22 | 1.5789474 2.4930748 23 | 2.1052632 4.432133 24 | 2.6315789 6.9252078 25 | 3.1578947 9.9722992 26 | 3.6842105 13.573407 27 | 4.2105263 17.728532 28 | 4.7368421 22.437673 29 | 5.2631579 27.700831 30 | 5.7894737 33.518006 31 | 6.3157895 39.889197 32 | 6.8421053 46.814404 33 | 7.3684211 54.293629 34 | 7.8947368 62.32687 35 | 8.4210526 70.914127 36 | 8.9473684 80.055402 37 | 9.4736842 89.750693 38 | 10 100 39 | }; 40 | \end{axis} 41 | 42 | \begin{axis}[ 43 | axis y line=right, 44 | tick align=outside, 45 | x grid style={darkgray176}, 46 | xmin=-10, xmax=210, 47 | xtick pos=left, 48 | xtick style={color=black}, 49 | y grid style={darkgray176}, 50 | ymin=-100, ymax=2100, 51 | ytick pos=right, 52 | ytick style={color=black}, 53 | yticklabel style={anchor=west} 54 | ] 55 | \addplot [semithick, steelblue31119180] 56 | table {% 57 | 0 0 58 | 10.526316 5.5401662 59 | 21.052632 22.160665 60 | 31.578947 49.861496 61 | 42.105263 88.642659 62 | 52.631579 138.50416 63 | 63.157895 199.44598 64 | 73.684211 271.46814 65 | 84.210526 354.57064 66 | 94.736842 448.75346 67 | 105.26316 554.01662 68 | 115.78947 670.36011 69 | 126.31579 797.78393 70 | 136.84211 936.28809 71 | 147.36842 1085.8726 72 | 157.89474 1246.5374 73 | 168.42105 1418.2825 74 | 178.94737 1601.108 75 | 189.47368 1795.0139 76 | 200 2000 77 | }; 78 | \end{axis} 79 | 80 | \end{tikzpicture} 81 | -------------------------------------------------------------------------------- /tests/test_errorband.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | fig, ax = plt.subplots() 7 | with plt.style.context("ggplot"): 8 | t = np.linspace(0, 2 * np.pi, 11) 9 | s = np.sin(t) 10 | ax.plot(t, s, "k-") 11 | ax.fill_between(t, s + 0.1, s - 0.1, facecolor="k", alpha=0.2) 12 | ax.set_xlim(t[0], t[-1]) 13 | ax.set_xlabel("t") 14 | ax.set_ylabel("sin(t)") 15 | ax.set_title("Simple plot") 16 | ax.grid(True) 17 | return fig 18 | 19 | 20 | def test(): 21 | from .helpers import assert_equality 22 | 23 | assert_equality(plot, "test_errorband_reference.tex") 24 | -------------------------------------------------------------------------------- /tests/test_errorband_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | tick align=outside, 7 | tick pos=left, 8 | title={Simple plot}, 9 | x grid style={darkgray176}, 10 | xlabel={t}, 11 | xmajorgrids, 12 | xmin=0, xmax=6.2831853, 13 | xtick style={color=black}, 14 | y grid style={darkgray176}, 15 | ylabel={sin(t)}, 16 | ymajorgrids, 17 | ymin=-1.1561622, ymax=1.1561622, 18 | ytick style={color=black} 19 | ] 20 | \path [fill=black, fill opacity=0.2, very thin] 21 | (axis cs:0,-0.1) 22 | --(axis cs:0,0.1) 23 | --(axis cs:0.62831853,0.68778525) 24 | --(axis cs:1.2566371,1.0510565) 25 | --(axis cs:1.8849556,1.0510565) 26 | --(axis cs:2.5132741,0.68778525) 27 | --(axis cs:3.1415927,0.1) 28 | --(axis cs:3.7699112,-0.48778525) 29 | --(axis cs:4.3982297,-0.85105652) 30 | --(axis cs:5.0265482,-0.85105652) 31 | --(axis cs:5.6548668,-0.48778525) 32 | --(axis cs:6.2831853,0.1) 33 | --(axis cs:6.2831853,-0.1) 34 | --(axis cs:6.2831853,-0.1) 35 | --(axis cs:5.6548668,-0.68778525) 36 | --(axis cs:5.0265482,-1.0510565) 37 | --(axis cs:4.3982297,-1.0510565) 38 | --(axis cs:3.7699112,-0.68778525) 39 | --(axis cs:3.1415927,-0.1) 40 | --(axis cs:2.5132741,0.48778525) 41 | --(axis cs:1.8849556,0.85105652) 42 | --(axis cs:1.2566371,0.85105652) 43 | --(axis cs:0.62831853,0.48778525) 44 | --(axis cs:0,-0.1) 45 | --cycle; 46 | 47 | \addplot [semithick, black] 48 | table {% 49 | 0 0 50 | 0.62831853 0.58778525 51 | 1.2566371 0.95105652 52 | 1.8849556 0.95105652 53 | 2.5132741 0.58778525 54 | 3.1415927 1.2246468e-16 55 | 3.7699112 -0.58778525 56 | 4.3982297 -0.95105652 57 | 5.0265482 -0.95105652 58 | 5.6548668 -0.58778525 59 | 6.2831853 -2.4492936e-16 60 | }; 61 | \end{axis} 62 | 63 | \end{tikzpicture} 64 | -------------------------------------------------------------------------------- /tests/test_errorbar.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | 3 | 4 | def plot(): 5 | # plot data 6 | fig = plt.figure() 7 | ax = fig.add_subplot(111) 8 | 9 | x = [7.14, 7.36, 7.47, 7.52] 10 | y = [3.3, 4.4, 8.8, 5.5] 11 | ystd = [0.1, 0.5, 0.8, 0.3] 12 | 13 | ax.errorbar(x, y, yerr=ystd) 14 | return fig 15 | 16 | 17 | def test(): 18 | from .helpers import assert_equality 19 | 20 | assert_equality(plot, "test_errorbar_reference.tex") 21 | -------------------------------------------------------------------------------- /tests/test_errorbar_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{steelblue31119180}{RGB}{31,119,180} 5 | 6 | \begin{axis}[ 7 | tick align=outside, 8 | tick pos=left, 9 | x grid style={darkgray176}, 10 | xmin=7.121, xmax=7.539, 11 | xtick style={color=black}, 12 | y grid style={darkgray176}, 13 | ymin=2.88, ymax=9.92, 14 | ytick style={color=black} 15 | ] 16 | \path [draw=steelblue31119180, semithick] 17 | (axis cs:7.14,3.2) 18 | --(axis cs:7.14,3.4); 19 | 20 | \path [draw=steelblue31119180, semithick] 21 | (axis cs:7.36,3.9) 22 | --(axis cs:7.36,4.9); 23 | 24 | \path [draw=steelblue31119180, semithick] 25 | (axis cs:7.47,8) 26 | --(axis cs:7.47,9.6); 27 | 28 | \path [draw=steelblue31119180, semithick] 29 | (axis cs:7.52,5.2) 30 | --(axis cs:7.52,5.8); 31 | 32 | \addplot [semithick, steelblue31119180] 33 | table {% 34 | 7.14 3.3 35 | 7.36 4.4 36 | 7.47 8.8 37 | 7.52 5.5 38 | }; 39 | \end{axis} 40 | 41 | \end{tikzpicture} 42 | -------------------------------------------------------------------------------- /tests/test_escape_chars.py: -------------------------------------------------------------------------------- 1 | # https://github.com/nschloe/tikzplotlib/issues/332 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def plot(): 6 | fig = plt.figure() 7 | plt.plot(0, 0, "kx") 8 | plt.title("Foo & Bar Dogs_N_Cats") 9 | plt.xlabel("Foo & Bar Dogs_N_Cats") 10 | plt.ylabel("Foo & Bar Dogs_N_Cats") 11 | return fig 12 | 13 | 14 | def test(): 15 | from .helpers import assert_equality 16 | 17 | assert_equality(plot, "test_escape_chars_reference.tex") 18 | -------------------------------------------------------------------------------- /tests/test_escape_chars_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | tick align=outside, 7 | tick pos=left, 8 | title={Foo \& Bar Dogs\_N\_Cats}, 9 | x grid style={darkgray176}, 10 | xlabel={Foo \& Bar Dogs\_N\_Cats}, 11 | xmin=-0.055, xmax=0.055, 12 | xtick style={color=black}, 13 | y grid style={darkgray176}, 14 | ylabel={Foo \& Bar Dogs\_N\_Cats}, 15 | ymin=-0.055, ymax=0.055, 16 | ytick style={color=black} 17 | ] 18 | \addplot [semithick, black, mark=x, mark size=3, mark options={solid}, only marks] 19 | table {% 20 | 0 0 21 | }; 22 | \end{axis} 23 | 24 | \end{tikzpicture} 25 | -------------------------------------------------------------------------------- /tests/test_externalize_tables.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | fig = plt.figure() 6 | with plt.style.context("ggplot"): 7 | t = np.arange(0.0, 2.0, 0.1) 8 | s = np.sin(2 * np.pi * t) 9 | s2 = np.cos(2 * np.pi * t) 10 | A = plt.cm.jet(np.linspace(0, 1, 10)) 11 | plt.plot(t, s, "o-", lw=1.5, color=A[5]) 12 | plt.plot(t, s2, "o-", lw=3, alpha=0.3) 13 | plt.xlabel("time(s)") 14 | # plt.xlabel('time(s) _ % $ \\') 15 | plt.ylabel("Voltage (mV)") 16 | plt.title("Simple plot $\\frac{\\alpha}{2}$") 17 | plt.grid(True) 18 | return fig 19 | 20 | 21 | def test(): 22 | from .helpers import assert_equality 23 | 24 | assert_equality(plot, "test_externalize_tables_reference.tex") 25 | -------------------------------------------------------------------------------- /tests/test_externalize_tables_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{chocolate2267451}{RGB}{226,74,51} 4 | \definecolor{dimgray85}{RGB}{85,85,85} 5 | \definecolor{gainsboro229}{RGB}{229,229,229} 6 | \definecolor{greenyellow17025576}{RGB}{170,255,76} 7 | 8 | \begin{axis}[ 9 | axis background/.style={fill=gainsboro229}, 10 | axis line style={white}, 11 | tick align=outside, 12 | tick pos=left, 13 | title={Simple plot \(\displaystyle \frac{\alpha}{2}\)}, 14 | x grid style={white}, 15 | xlabel=\textcolor{dimgray85}{time(s)}, 16 | xmajorgrids, 17 | xmin=-0.095, xmax=1.995, 18 | xtick style={color=dimgray85}, 19 | y grid style={white}, 20 | ylabel=\textcolor{dimgray85}{Voltage (mV)}, 21 | ymajorgrids, 22 | ymin=-1.1, ymax=1.1, 23 | ytick style={color=dimgray85} 24 | ] 25 | \addplot [semithick, greenyellow17025576, mark=*, mark size=3, mark options={solid}] 26 | table {% 27 | 0 0 28 | 0.1 0.58778525 29 | 0.2 0.95105652 30 | 0.3 0.95105652 31 | 0.4 0.58778525 32 | 0.5 1.2246468e-16 33 | 0.6 -0.58778525 34 | 0.7 -0.95105652 35 | 0.8 -0.95105652 36 | 0.9 -0.58778525 37 | 1 -2.4492936e-16 38 | 1.1 0.58778525 39 | 1.2 0.95105652 40 | 1.3 0.95105652 41 | 1.4 0.58778525 42 | 1.5 3.6739404e-16 43 | 1.6 -0.58778525 44 | 1.7 -0.95105652 45 | 1.8 -0.95105652 46 | 1.9 -0.58778525 47 | }; 48 | \addplot [very thick, chocolate2267451, opacity=0.3, mark=*, mark size=3, mark options={solid}] 49 | table {% 50 | 0 1 51 | 0.1 0.80901699 52 | 0.2 0.30901699 53 | 0.3 -0.30901699 54 | 0.4 -0.80901699 55 | 0.5 -1 56 | 0.6 -0.80901699 57 | 0.7 -0.30901699 58 | 0.8 0.30901699 59 | 0.9 0.80901699 60 | 1 1 61 | 1.1 0.80901699 62 | 1.2 0.30901699 63 | 1.3 -0.30901699 64 | 1.4 -0.80901699 65 | 1.5 -1 66 | 1.6 -0.80901699 67 | 1.7 -0.30901699 68 | 1.8 0.30901699 69 | 1.9 0.80901699 70 | }; 71 | \end{axis} 72 | 73 | \end{tikzpicture} 74 | -------------------------------------------------------------------------------- /tests/test_fancy_colorbar.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | da = np.zeros((3, 3)) 7 | da[:2, :2] = 1.0 8 | 9 | fig = plt.figure() 10 | ax = plt.gca() 11 | 12 | im = ax.imshow(da, cmap="viridis") 13 | plt.colorbar(im, aspect=5, shrink=0.5) 14 | return fig 15 | 16 | 17 | def test(): 18 | from .helpers import assert_equality 19 | 20 | assert_equality(plot, "test_fancy_colorbar_reference.tex") 21 | -------------------------------------------------------------------------------- /tests/test_fancy_colorbar_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | colorbar, 7 | colorbar style={ylabel={}}, 8 | colormap/viridis, 9 | point meta max=1, 10 | point meta min=0, 11 | tick align=outside, 12 | tick pos=left, 13 | x grid style={darkgray176}, 14 | xmin=-0.5, xmax=2.5, 15 | xtick style={color=black}, 16 | y dir=reverse, 17 | y grid style={darkgray176}, 18 | ymin=-0.5, ymax=2.5, 19 | ytick style={color=black} 20 | ] 21 | \addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=2.5, ymin=2.5, ymax=-0.5] {tmp-000.png}; 22 | \end{axis} 23 | 24 | \end{tikzpicture} 25 | -------------------------------------------------------------------------------- /tests/test_fancybox.py: -------------------------------------------------------------------------------- 1 | # Taken from http://matplotlib.org/examples/pylab_examples/fancybox_demo.html 2 | import matplotlib.pyplot as plt 3 | import matplotlib.transforms as mtransforms 4 | from matplotlib.patches import FancyBboxPatch 5 | 6 | # Bbox object around which the fancy box will be drawn. 7 | bb = mtransforms.Bbox([[0.3, 0.4], [0.7, 0.6]]) 8 | 9 | 10 | def draw_bbox(ax, bb_obj): 11 | # boxstyle=square with pad=0, i.e. bbox itself. 12 | p_bbox = FancyBboxPatch( 13 | (bb_obj.xmin, bb_obj.ymin), 14 | abs(bb_obj.width), 15 | abs(bb_obj.height), 16 | boxstyle="square,pad=0.", 17 | ec="k", 18 | fc="none", 19 | zorder=10.0, 20 | ) 21 | ax.add_patch(p_bbox) 22 | return 23 | 24 | 25 | def box1(ax): 26 | # a fancy box with round corners. pad=0.1 27 | p_fancy = FancyBboxPatch( 28 | (bb.xmin, bb.ymin), 29 | abs(bb.width), 30 | abs(bb.height), 31 | boxstyle="round,pad=0.1", 32 | fc=(1.0, 0.8, 1.0), 33 | ec=(1.0, 0.5, 1.0), 34 | ) 35 | 36 | ax.add_patch(p_fancy) 37 | 38 | ax.text(0.1, 0.8, "boxstyle='round, pad=0.1'", size=10, transform=ax.transAxes) 39 | 40 | # # draws control points for the fancy box. 41 | # l = p_fancy.get_path().vertices 42 | # ax.plot(l[:,0], l[:,1], '.') 43 | 44 | # draw the original bbox in black 45 | draw_bbox(ax, bb) 46 | return 47 | 48 | 49 | def box2(ax): 50 | # bbox=round has two optional argument. pad and rounding_size. 51 | # They can be set during the initialization. 52 | p_fancy = FancyBboxPatch( 53 | (bb.xmin, bb.ymin), 54 | abs(bb.width), 55 | abs(bb.height), 56 | boxstyle="round,pad=0.1", 57 | fc=(1.0, 0.8, 1.0), 58 | ec=(1.0, 0.5, 1.0), 59 | ) 60 | 61 | ax.add_patch(p_fancy) 62 | 63 | # boxstyle and its argument can be later modified with 64 | # set_boxstyle method. Note that the old attributes are simply 65 | # forgotten even if the boxstyle name is same. 66 | 67 | p_fancy.set_boxstyle("round,pad=0.1, rounding_size=0.2") 68 | # or 69 | # p_fancy.set_boxstyle('round', pad=0.1, rounding_size=0.2) 70 | 71 | ax.text( 72 | 0.1, 73 | 0.8, 74 | "boxstyle='round,pad=0.1\n rounding\\_size=0.2'", 75 | size=10, 76 | transform=ax.transAxes, 77 | ) 78 | 79 | # # draws control points for the fancy box. 80 | # l = p_fancy.get_path().vertices 81 | # ax.plot(l[:,0], l[:,1], '.') 82 | 83 | draw_bbox(ax, bb) 84 | return 85 | 86 | 87 | def box3(ax): 88 | 89 | # mutation_scale determine overall scale of the mutation, 90 | # i.e. both pad and rounding_size is scaled according to this 91 | # value. 92 | p_fancy = FancyBboxPatch( 93 | (bb.xmin, bb.ymin), 94 | abs(bb.width), 95 | abs(bb.height), 96 | boxstyle="round,pad=0.1", 97 | mutation_scale=2.0, 98 | fc=(1.0, 0.8, 1.0), 99 | ec=(1.0, 0.5, 1.0), 100 | ) 101 | 102 | ax.add_patch(p_fancy) 103 | 104 | ax.text( 105 | 0.1, 106 | 0.8, 107 | "boxstyle='round,pad=0.1'\n mutation\\_scale=2", 108 | size=10, 109 | transform=ax.transAxes, 110 | ) 111 | 112 | # # draws control points for the fancy box. 113 | # l = p_fancy.get_path().vertices 114 | # ax.plot(l[:,0], l[:,1], '.') 115 | 116 | draw_bbox(ax, bb) 117 | return 118 | 119 | 120 | def box4(ax): 121 | 122 | # When the aspect ratio of the axes is not 1, the fancy box may 123 | # not be what you expected (green) 124 | 125 | p_fancy = FancyBboxPatch( 126 | (bb.xmin, bb.ymin), 127 | abs(bb.width), 128 | abs(bb.height), 129 | boxstyle="round,pad=0.2", 130 | fc="none", 131 | ec=(0.0, 0.5, 0.0), 132 | zorder=4, 133 | ) 134 | 135 | ax.add_patch(p_fancy) 136 | 137 | # You can compensate this by setting the mutation_aspect (pink). 138 | p_fancy = FancyBboxPatch( 139 | (bb.xmin, bb.ymin), 140 | abs(bb.width), 141 | abs(bb.height), 142 | boxstyle="round,pad=0.3", 143 | mutation_aspect=0.5, 144 | fc=(1.0, 0.8, 1.0), 145 | ec=(1.0, 0.5, 1.0), 146 | ) 147 | 148 | ax.add_patch(p_fancy) 149 | 150 | ax.text( 151 | 0.1, 152 | 0.8, 153 | "boxstyle='round, pad=0.3'\n mutation\\_aspect=.5", 154 | size=10, 155 | transform=ax.transAxes, 156 | ) 157 | 158 | draw_bbox(ax, bb) 159 | return 160 | 161 | 162 | def plot(): 163 | fig = plt.figure() 164 | plt.clf() 165 | 166 | ax = plt.subplot(2, 2, 1) 167 | box1(ax) 168 | ax.set_xlim(0.0, 1.0) 169 | ax.set_ylim(0.0, 1.0) 170 | ax.set_title("box1") 171 | ax.set_aspect(1.0) 172 | 173 | ax = plt.subplot(2, 2, 2) 174 | ax.set_title("box2") 175 | box2(ax) 176 | ax.set_xlim(0.0, 1.0) 177 | ax.set_ylim(0.0, 1.0) 178 | ax.set_aspect(1.0) 179 | 180 | ax = plt.subplot(2, 2, 3) 181 | ax.set_title("box3") 182 | box3(ax) 183 | ax.set_xlim(0.0, 1.0) 184 | ax.set_ylim(0.0, 1.0) 185 | ax.set_aspect(1) 186 | 187 | ax = plt.subplot(2, 2, 4) 188 | ax.set_title("box4") 189 | box4(ax) 190 | ax.set_xlim(-0.5, 1.5) 191 | ax.set_ylim(0.0, 1.0) 192 | ax.set_aspect(2.0) 193 | 194 | return fig 195 | 196 | 197 | def test(): 198 | from .helpers import assert_equality 199 | 200 | assert_equality(plot, "test_fancybox_reference.tex") 201 | -------------------------------------------------------------------------------- /tests/test_fancybox_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{green01270}{RGB}{0,127,0} 5 | \definecolor{lavender255204255}{RGB}{255,204,255} 6 | \definecolor{violet255127255}{RGB}{255,127,255} 7 | 8 | \begin{groupplot}[group style={group size=2 by 2}] 9 | \nextgroupplot[ 10 | tick align=outside, 11 | tick pos=left, 12 | title={box1}, 13 | x grid style={darkgray176}, 14 | xmin=0, xmax=1, 15 | xtick style={color=black}, 16 | y grid style={darkgray176}, 17 | ymin=0, ymax=1, 18 | ytick style={color=black} 19 | ] 20 | \path [draw=violet255127255, fill=lavender255204255] 21 | (axis cs:0.3,0.3) 22 | --(axis cs:0.7,0.3) 23 | .. controls (axis cs:0.76666667,0.3) and (axis cs:0.8,0.33333333) .. (axis cs:0.8,0.4) 24 | --(axis cs:0.8,0.6) 25 | .. controls (axis cs:0.8,0.66666667) and (axis cs:0.76666667,0.7) .. (axis cs:0.7,0.7) 26 | --(axis cs:0.3,0.7) 27 | .. controls (axis cs:0.23333333,0.7) and (axis cs:0.2,0.66666667) .. (axis cs:0.2,0.6) 28 | --(axis cs:0.2,0.4) 29 | .. controls (axis cs:0.2,0.33333333) and (axis cs:0.23333333,0.3) .. (axis cs:0.3,0.3) 30 | --cycle; 31 | \draw (axis cs:0.1,0.8) node[ 32 | scale=0.5, 33 | anchor=base west, 34 | text=black, 35 | rotate=0.0 36 | ]{boxstyle='round, pad=0.1'}; 37 | \path [draw=black] 38 | (axis cs:0.3,0.4) 39 | --(axis cs:0.7,0.4) 40 | --(axis cs:0.7,0.6) 41 | --(axis cs:0.3,0.6) 42 | --cycle; 43 | 44 | \nextgroupplot[ 45 | tick align=outside, 46 | tick pos=left, 47 | title={box2}, 48 | x grid style={darkgray176}, 49 | xmin=0, xmax=1, 50 | xtick style={color=black}, 51 | y grid style={darkgray176}, 52 | ymin=0, ymax=1, 53 | ytick style={color=black} 54 | ] 55 | \path [draw=violet255127255, fill=lavender255204255] 56 | (axis cs:0.4,0.3) 57 | --(axis cs:0.6,0.3) 58 | .. controls (axis cs:0.73333333,0.3) and (axis cs:0.8,0.36666667) .. (axis cs:0.8,0.5) 59 | --(axis cs:0.8,0.5) 60 | .. controls (axis cs:0.8,0.63333333) and (axis cs:0.73333333,0.7) .. (axis cs:0.6,0.7) 61 | --(axis cs:0.4,0.7) 62 | .. controls (axis cs:0.26666667,0.7) and (axis cs:0.2,0.63333333) .. (axis cs:0.2,0.5) 63 | --(axis cs:0.2,0.5) 64 | .. controls (axis cs:0.2,0.36666667) and (axis cs:0.26666667,0.3) .. (axis cs:0.4,0.3) 65 | --cycle; 66 | \draw (axis cs:0.1,0.8) node[ 67 | scale=0.5, 68 | anchor=base west, 69 | text=black, 70 | rotate=0.0, 71 | align=left 72 | ]{boxstyle='round,pad=0.1\\rounding\_size=0.2'}; 73 | \path [draw=black] 74 | (axis cs:0.3,0.4) 75 | --(axis cs:0.7,0.4) 76 | --(axis cs:0.7,0.6) 77 | --(axis cs:0.3,0.6) 78 | --cycle; 79 | 80 | \nextgroupplot[ 81 | tick align=outside, 82 | tick pos=left, 83 | title={box3}, 84 | x grid style={darkgray176}, 85 | xmin=0, xmax=1, 86 | xtick style={color=black}, 87 | y grid style={darkgray176}, 88 | ymin=0, ymax=1, 89 | ytick style={color=black} 90 | ] 91 | \path [draw=violet255127255, fill=lavender255204255] 92 | (axis cs:0.3,0.2) 93 | --(axis cs:0.7,0.2) 94 | .. controls (axis cs:0.83333333,0.2) and (axis cs:0.9,0.26666667) .. (axis cs:0.9,0.4) 95 | --(axis cs:0.9,0.6) 96 | .. controls (axis cs:0.9,0.73333333) and (axis cs:0.83333333,0.8) .. (axis cs:0.7,0.8) 97 | --(axis cs:0.3,0.8) 98 | .. controls (axis cs:0.16666667,0.8) and (axis cs:0.1,0.73333333) .. (axis cs:0.1,0.6) 99 | --(axis cs:0.1,0.4) 100 | .. controls (axis cs:0.1,0.26666667) and (axis cs:0.16666667,0.2) .. (axis cs:0.3,0.2) 101 | --cycle; 102 | \draw (axis cs:0.1,0.8) node[ 103 | scale=0.5, 104 | anchor=base west, 105 | text=black, 106 | rotate=0.0, 107 | align=left 108 | ]{boxstyle='round,pad=0.1'\\mutation\_scale=2}; 109 | \path [draw=black] 110 | (axis cs:0.3,0.4) 111 | --(axis cs:0.7,0.4) 112 | --(axis cs:0.7,0.6) 113 | --(axis cs:0.3,0.6) 114 | --cycle; 115 | 116 | \nextgroupplot[ 117 | tick align=outside, 118 | tick pos=left, 119 | title={box4}, 120 | x grid style={darkgray176}, 121 | xmin=-0.5, xmax=1.5, 122 | xtick style={color=black}, 123 | y grid style={darkgray176}, 124 | ymin=0, ymax=1, 125 | ytick style={color=black} 126 | ] 127 | \path [draw=violet255127255, fill=lavender255204255] 128 | (axis cs:0.3,0.25) 129 | --(axis cs:0.7,0.25) 130 | .. controls (axis cs:0.9,0.25) and (axis cs:1,0.3) .. (axis cs:1,0.4) 131 | --(axis cs:1,0.6) 132 | .. controls (axis cs:1,0.7) and (axis cs:0.9,0.75) .. (axis cs:0.7,0.75) 133 | --(axis cs:0.3,0.75) 134 | .. controls (axis cs:0.1,0.75) and (axis cs:0,0.7) .. (axis cs:0,0.6) 135 | --(axis cs:0,0.4) 136 | .. controls (axis cs:0,0.3) and (axis cs:0.1,0.25) .. (axis cs:0.3,0.25) 137 | --cycle; 138 | \draw (axis cs:0.1,0.8) node[ 139 | scale=0.5, 140 | anchor=base west, 141 | text=black, 142 | rotate=0.0, 143 | align=left 144 | ]{boxstyle='round, pad=0.3'\\mutation\_aspect=.5}; 145 | \path [draw=green01270] 146 | (axis cs:0.3,0.2) 147 | --(axis cs:0.7,0.2) 148 | .. controls (axis cs:0.83333333,0.2) and (axis cs:0.9,0.26666667) .. (axis cs:0.9,0.4) 149 | --(axis cs:0.9,0.6) 150 | .. controls (axis cs:0.9,0.73333333) and (axis cs:0.83333333,0.8) .. (axis cs:0.7,0.8) 151 | --(axis cs:0.3,0.8) 152 | .. controls (axis cs:0.16666667,0.8) and (axis cs:0.1,0.73333333) .. (axis cs:0.1,0.6) 153 | --(axis cs:0.1,0.4) 154 | .. controls (axis cs:0.1,0.26666667) and (axis cs:0.16666667,0.2) .. (axis cs:0.3,0.2) 155 | --cycle; 156 | \path [draw=black] 157 | (axis cs:0.3,0.4) 158 | --(axis cs:0.7,0.4) 159 | --(axis cs:0.7,0.6) 160 | --(axis cs:0.3,0.6) 161 | --cycle; 162 | \end{groupplot} 163 | 164 | \end{tikzpicture} 165 | -------------------------------------------------------------------------------- /tests/test_fillstyle.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | fig = plt.figure() 7 | 8 | N = 10 9 | t = np.linspace(0, 1, N) 10 | x = np.arange(N) 11 | plt.plot(t, x, "-o", fillstyle="none") 12 | plt.tight_layout() 13 | return fig 14 | 15 | 16 | def test(): 17 | from .helpers import assert_equality 18 | 19 | assert_equality(plot, "test_fillstyle_reference.tex") 20 | -------------------------------------------------------------------------------- /tests/test_fillstyle_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{steelblue31119180}{RGB}{31,119,180} 5 | 6 | \begin{axis}[ 7 | tick align=outside, 8 | tick pos=left, 9 | x grid style={darkgray176}, 10 | xmin=-0.05, xmax=1.05, 11 | xtick style={color=black}, 12 | y grid style={darkgray176}, 13 | ymin=-0.45, ymax=9.45, 14 | ytick style={color=black} 15 | ] 16 | \addplot [semithick, steelblue31119180, mark=o, mark size=3, mark options={solid,fill opacity=0}] 17 | table {% 18 | 0 0 19 | 0.11111111 1 20 | 0.22222222 2 21 | 0.33333333 3 22 | 0.44444444 4 23 | 0.55555556 5 24 | 0.66666667 6 25 | 0.77777778 7 26 | 0.88888889 8 27 | 1 9 28 | }; 29 | \end{axis} 30 | 31 | \end{tikzpicture} 32 | -------------------------------------------------------------------------------- /tests/test_hatch.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | 4 | def plot(): 5 | """ 6 | Hatch demo code from 7 | https://matplotlib.org/3.1.1/gallery/shapes_and_collections/hatch_demo.html 8 | 9 | Slightly modified to test more aspects of the hatch implementation 10 | """ 11 | import matplotlib.pyplot as plt 12 | from matplotlib.patches import Ellipse, Polygon 13 | 14 | fig, (ax1, ax2, ax3) = plt.subplots(3) 15 | 16 | ax1.bar(range(1, 5), range(1, 5), color="red", edgecolor="black", hatch="/") 17 | ax1.bar( 18 | range(1, 5), 19 | [6] * 4, 20 | bottom=range(1, 5), 21 | color="blue", 22 | edgecolor="black", 23 | hatch="//", 24 | ) 25 | ax1.set_xticks([1.5, 2.5, 3.5, 4.5]) 26 | 27 | bars = ax2.bar( 28 | range(1, 5), range(1, 5), color="yellow", edgecolor="black" 29 | ) + ax2.bar(range(1, 5), [6] * 4, bottom=range(1, 5), color="green") 30 | ax2.set_xticks([1.5, 2.5, 3.5, 4.5]) 31 | 32 | patterns = ("-", "+", "x", "\\", "*", "o", "O", ".") 33 | for bar, pattern in zip(bars, patterns): 34 | bar.set_hatch(pattern) 35 | 36 | ax3.fill( 37 | [1, 3, 3, 1], [1, 1, 2, 2], fill=False, hatch="\\", zorder=1, label="Square" 38 | ) 39 | ax3.add_patch( 40 | Ellipse((4, 1.5), 4, 0.5, fill="green", hatch="*", zorder=3, label="Ellipse") 41 | ) 42 | p = Polygon( 43 | [[0, 0], [4, 1.1], [6, 2.5], [2, 1.4]], 44 | closed=True, 45 | fill=False, 46 | hatch="/", 47 | zorder=2, 48 | label="Polygon", 49 | ) 50 | p._hatch_color = (0.5, 0.3, 0.8, 0.7) 51 | ax3.add_patch(p) 52 | ax3.set_xlim((0, 6)) 53 | ax3.set_ylim((0, 2.5)) 54 | ax3.legend() 55 | 56 | return fig 57 | 58 | 59 | def test(): 60 | from .helpers import assert_equality 61 | 62 | with pytest.warns(UserWarning): 63 | assert_equality(plot, "test_hatch_reference.tex") 64 | -------------------------------------------------------------------------------- /tests/test_hatch_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{green}{RGB}{0,128,0} 5 | \definecolor{lightgray204}{RGB}{204,204,204} 6 | \definecolor{slateblue12776204}{RGB}{127,76,204} 7 | \definecolor{steelblue31119180}{RGB}{31,119,180} 8 | \definecolor{yellow}{RGB}{255,255,0} 9 | 10 | \begin{groupplot}[group style={group size=1 by 3}] 11 | \nextgroupplot[ 12 | tick align=outside, 13 | tick pos=left, 14 | x grid style={darkgray176}, 15 | xmin=0.41, xmax=4.59, 16 | xtick style={color=black}, 17 | y grid style={darkgray176}, 18 | ymin=0, ymax=10.5, 19 | ytick style={color=black} 20 | ] 21 | \draw[draw=black,fill=red,postaction={pattern=north east lines}] (axis cs:0.6,0) rectangle (axis cs:1.4,1); 22 | \draw[draw=black,fill=red,postaction={pattern=north east lines}] (axis cs:1.6,0) rectangle (axis cs:2.4,2); 23 | \draw[draw=black,fill=red,postaction={pattern=north east lines}] (axis cs:2.6,0) rectangle (axis cs:3.4,3); 24 | \draw[draw=black,fill=red,postaction={pattern=north east lines}] (axis cs:3.6,0) rectangle (axis cs:4.4,4); 25 | \draw[draw=black,fill=blue,postaction={pattern=north east lines}] (axis cs:0.6,1) rectangle (axis cs:1.4,7); 26 | \draw[draw=black,fill=blue,postaction={pattern=north east lines}] (axis cs:1.6,2) rectangle (axis cs:2.4,8); 27 | \draw[draw=black,fill=blue,postaction={pattern=north east lines}] (axis cs:2.6,3) rectangle (axis cs:3.4,9); 28 | \draw[draw=black,fill=blue,postaction={pattern=north east lines}] (axis cs:3.6,4) rectangle (axis cs:4.4,10); 29 | 30 | \nextgroupplot[ 31 | tick align=outside, 32 | tick pos=left, 33 | x grid style={darkgray176}, 34 | xmin=0.41, xmax=4.59, 35 | xtick style={color=black}, 36 | y grid style={darkgray176}, 37 | ymin=0, ymax=10.5, 38 | ytick style={color=black} 39 | ] 40 | \draw[draw=black,fill=yellow,postaction={pattern=horizontal lines}] (axis cs:0.6,0) rectangle (axis cs:1.4,1); 41 | \draw[draw=black,fill=yellow,postaction={pattern=grid}] (axis cs:1.6,0) rectangle (axis cs:2.4,2); 42 | \draw[draw=black,fill=yellow,postaction={pattern=crosshatch}] (axis cs:2.6,0) rectangle (axis cs:3.4,3); 43 | \draw[draw=black,fill=yellow,postaction={pattern=north west lines}] (axis cs:3.6,0) rectangle (axis cs:4.4,4); 44 | \draw[draw=none,fill=green,postaction={pattern=fivepointed stars}] (axis cs:0.6,1) rectangle (axis cs:1.4,7); 45 | \draw[draw=none,fill=green,postaction={pattern=sixpointed stars}] (axis cs:1.6,2) rectangle (axis cs:2.4,8); 46 | \draw[draw=none,fill=green,postaction={pattern=bricks}] (axis cs:2.6,3) rectangle (axis cs:3.4,9); 47 | \draw[draw=none,fill=green,postaction={pattern=crosshatch dots}] (axis cs:3.6,4) rectangle (axis cs:4.4,10); 48 | 49 | \nextgroupplot[ 50 | legend cell align={left}, 51 | legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=lightgray204}, 52 | tick align=outside, 53 | tick pos=left, 54 | x grid style={darkgray176}, 55 | xmin=0, xmax=6, 56 | xtick style={color=black}, 57 | y grid style={darkgray176}, 58 | ymin=0, ymax=2.5, 59 | ytick style={color=black} 60 | ] 61 | \path [draw=black, postaction={pattern=north west lines}] 62 | (axis cs:1,1) 63 | --(axis cs:3,1) 64 | --(axis cs:3,2) 65 | --(axis cs:1,2) 66 | --cycle; 67 | \addlegendimage{area legend, draw=black, postaction={pattern=north west lines}} 68 | \addlegendentry{Square} 69 | 70 | \path [draw=black, postaction={pattern=north east lines, pattern color=slateblue12776204, fill opacity=0.7}] 71 | (axis cs:0,0) 72 | --(axis cs:4,1.1) 73 | --(axis cs:6,2.5) 74 | --(axis cs:2,1.4) 75 | --cycle; 76 | \addlegendimage{area legend, draw=black, postaction={pattern=north east lines, pattern color=slateblue12776204, fill opacity=0.7}} 77 | \addlegendentry{Polygon} 78 | 79 | \draw[draw=none,fill=steelblue31119180,postaction={pattern=fivepointed stars}] (axis cs:4,1.5) ellipse (2 and 0.25); 80 | \addlegendimage{area legend, draw=none, fill=steelblue31119180, postaction={pattern=fivepointed stars}} 81 | \addlegendentry{Ellipse} 82 | 83 | \end{groupplot} 84 | 85 | \end{tikzpicture} 86 | -------------------------------------------------------------------------------- /tests/test_heat.py: -------------------------------------------------------------------------------- 1 | import matplotlib 2 | import matplotlib.pyplot as plt 3 | import numpy as np 4 | 5 | 6 | def plot(): 7 | fig = plt.figure() 8 | x, y = np.ogrid[-10:10:100j, -10:10:100j] 9 | extent = (x.min(), x.max(), y.min(), y.max()) 10 | cmap = matplotlib.cm.get_cmap("gray") 11 | plt.imshow(x * y, extent=extent, cmap=cmap) 12 | plt.colorbar() 13 | return fig 14 | 15 | 16 | def test(): 17 | from .helpers import assert_equality 18 | 19 | assert_equality(plot, "test_heat_reference.tex") 20 | -------------------------------------------------------------------------------- /tests/test_heat_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | colorbar, 7 | colorbar style={ylabel={}}, 8 | colormap/blackwhite, 9 | point meta max=100, 10 | point meta min=-100, 11 | tick align=outside, 12 | tick pos=left, 13 | x grid style={darkgray176}, 14 | xmin=-10, xmax=10, 15 | xtick style={color=black}, 16 | y grid style={darkgray176}, 17 | ymin=-10, ymax=10, 18 | ytick style={color=black} 19 | ] 20 | \addplot graphics [includegraphics cmd=\pgfimage,xmin=-10, xmax=10, ymin=-10, ymax=10] {tmp-000.png}; 21 | \end{axis} 22 | 23 | \end{tikzpicture} 24 | -------------------------------------------------------------------------------- /tests/test_histogram.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | # Make plot with vertical (default) colorbar 7 | fig = plt.figure() 8 | ax = fig.add_subplot(111) 9 | 10 | np.random.seed(123) 11 | ax.hist(10 + 2 * np.random.randn(1000), label="men") 12 | ax.hist(12 + 3 * np.random.randn(1000), label="women", alpha=0.5) 13 | ax.legend() 14 | return fig 15 | 16 | 17 | def test(): 18 | from .helpers import assert_equality 19 | 20 | assert_equality(plot, "test_histogram_reference.tex") 21 | -------------------------------------------------------------------------------- /tests/test_histogram_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{darkorange25512714}{RGB}{255,127,14} 5 | \definecolor{lightgray204}{RGB}{204,204,204} 6 | \definecolor{steelblue31119180}{RGB}{31,119,180} 7 | 8 | \begin{axis}[ 9 | legend cell align={left}, 10 | legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=lightgray204}, 11 | tick align=outside, 12 | tick pos=left, 13 | x grid style={darkgray176}, 14 | xmin=-0.40194748, xmax=21.549936, 15 | xtick style={color=black}, 16 | y grid style={darkgray176}, 17 | ymin=0, ymax=300.3, 18 | ytick style={color=black} 19 | ] 20 | \draw[draw=none,fill=steelblue31119180] (axis cs:3.53789,0) rectangle (axis cs:4.8984168,7); 21 | \addlegendimage{ybar,ybar legend,draw=none,fill=steelblue31119180} 22 | \addlegendentry{men} 23 | 24 | \draw[draw=none,fill=steelblue31119180] (axis cs:4.8984168,0) rectangle (axis cs:6.2589437,27); 25 | \draw[draw=none,fill=steelblue31119180] (axis cs:6.2589437,0) rectangle (axis cs:7.6194705,95); 26 | \draw[draw=none,fill=steelblue31119180] (axis cs:7.6194705,0) rectangle (axis cs:8.9799974,183); 27 | \draw[draw=none,fill=steelblue31119180] (axis cs:8.9799974,0) rectangle (axis cs:10.340524,286); 28 | \draw[draw=none,fill=steelblue31119180] (axis cs:10.340524,0) rectangle (axis cs:11.701051,202); 29 | \draw[draw=none,fill=steelblue31119180] (axis cs:11.701051,0) rectangle (axis cs:13.061578,142); 30 | \draw[draw=none,fill=steelblue31119180] (axis cs:13.061578,0) rectangle (axis cs:14.422105,49); 31 | \draw[draw=none,fill=steelblue31119180] (axis cs:14.422105,0) rectangle (axis cs:15.782632,7); 32 | \draw[draw=none,fill=steelblue31119180] (axis cs:15.782632,0) rectangle (axis cs:17.143158,2); 33 | \draw[draw=none,fill=darkorange25512714,fill opacity=0.5] (axis cs:0.59586541,0) rectangle (axis cs:2.5914912,2); 34 | \addlegendimage{ybar,ybar legend,draw=none,fill=darkorange25512714,fill opacity=0.5} 35 | \addlegendentry{women} 36 | 37 | \draw[draw=none,fill=darkorange25512714,fill opacity=0.5] (axis cs:2.5914912,0) rectangle (axis cs:4.587117,6); 38 | \draw[draw=none,fill=darkorange25512714,fill opacity=0.5] (axis cs:4.587117,0) rectangle (axis cs:6.5827427,20); 39 | \draw[draw=none,fill=darkorange25512714,fill opacity=0.5] (axis cs:6.5827427,0) rectangle (axis cs:8.5783685,81); 40 | \draw[draw=none,fill=darkorange25512714,fill opacity=0.5] (axis cs:8.5783685,0) rectangle (axis cs:10.573994,191); 41 | \draw[draw=none,fill=darkorange25512714,fill opacity=0.5] (axis cs:10.573994,0) rectangle (axis cs:12.56962,252); 42 | \draw[draw=none,fill=darkorange25512714,fill opacity=0.5] (axis cs:12.56962,0) rectangle (axis cs:14.565246,264); 43 | \draw[draw=none,fill=darkorange25512714,fill opacity=0.5] (axis cs:14.565246,0) rectangle (axis cs:16.560872,132); 44 | \draw[draw=none,fill=darkorange25512714,fill opacity=0.5] (axis cs:16.560872,0) rectangle (axis cs:18.556497,44); 45 | \draw[draw=none,fill=darkorange25512714,fill opacity=0.5] (axis cs:18.556497,0) rectangle (axis cs:20.552123,8); 46 | \end{axis} 47 | 48 | \end{tikzpicture} 49 | -------------------------------------------------------------------------------- /tests/test_horizontal_alignment.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | labels = ["lab1", "label 2", "another super label"] 7 | n = len(labels) 8 | x = np.arange(n) 9 | y = 1 / (x + 1) 10 | 11 | ax = plt.gca() 12 | ax.bar(x, y, 0.5) 13 | 14 | ax.set_xticks(x) 15 | ax.set_xticklabels(labels) 16 | plt.xticks(rotation=45, ha="right") 17 | 18 | 19 | def test(): 20 | from .helpers import assert_equality 21 | 22 | assert_equality(plot, "test_horizontal_alignment_reference.tex") 23 | -------------------------------------------------------------------------------- /tests/test_horizontal_alignment_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{steelblue31119180}{RGB}{31,119,180} 5 | 6 | \begin{axis}[ 7 | tick align=outside, 8 | tick pos=left, 9 | x grid style={darkgray176}, 10 | xmin=-0.375, xmax=2.375, 11 | xtick style={color=black}, 12 | xtick={0,1,2}, 13 | xticklabel style={rotate=45.0,anchor=east}, 14 | xticklabels={lab1,label 2,another super label}, 15 | y grid style={darkgray176}, 16 | ymin=0, ymax=1.05, 17 | ytick style={color=black} 18 | ] 19 | \draw[draw=none,fill=steelblue31119180] (axis cs:-0.25,0) rectangle (axis cs:0.25,1); 20 | \draw[draw=none,fill=steelblue31119180] (axis cs:0.75,0) rectangle (axis cs:1.25,0.5); 21 | \draw[draw=none,fill=steelblue31119180] (axis cs:1.75,0) rectangle (axis cs:2.25,0.33333333); 22 | \end{axis} 23 | 24 | \end{tikzpicture} 25 | -------------------------------------------------------------------------------- /tests/test_image_plot_lower.py: -------------------------------------------------------------------------------- 1 | import pathlib 2 | 3 | import matplotlib.pyplot as plt 4 | 5 | # the picture 'lena.png' with origin='lower' is flipped upside-down. 6 | # So it has to be upside-down in the pdf-file as well. 7 | 8 | 9 | def plot(): 10 | import matplotlib.image as mpimg 11 | from matplotlib import rcParams 12 | 13 | this_dir = pathlib.Path(__file__).resolve().parent 14 | img = mpimg.imread(this_dir / "lena.png") 15 | 16 | dpi = rcParams["figure.dpi"] 17 | figsize = img.shape[0] / dpi, img.shape[1] / dpi 18 | 19 | fig = plt.figure(figsize=figsize) 20 | ax = plt.axes([0, 0, 1, 1], frameon=False) 21 | ax.set_axis_off() 22 | plt.imshow(img, cmap="viridis", origin="lower") 23 | # Setting the current color map to HSV messes up other plots 24 | # plt.hsv() 25 | plt.colorbar() 26 | return fig 27 | 28 | 29 | def test(): 30 | from .helpers import assert_equality 31 | 32 | assert_equality(plot, "test_image_plot_lower_reference.tex") 33 | -------------------------------------------------------------------------------- /tests/test_image_plot_lower_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | colorbar, 7 | colorbar style={ylabel={}}, 8 | colormap/viridis, 9 | hide x axis, 10 | hide y axis, 11 | point meta max=0.93333334, 12 | point meta min=0.10196079, 13 | tick align=outside, 14 | tick pos=left, 15 | x grid style={darkgray176}, 16 | xmin=-0.5, xmax=15.5, 17 | xtick style={color=black}, 18 | y grid style={darkgray176}, 19 | ymin=-0.5, ymax=15.5, 20 | ytick style={color=black} 21 | ] 22 | \addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=15.5, ymin=-0.5, ymax=15.5] {tmp-000.png}; 23 | \end{axis} 24 | 25 | \end{tikzpicture} 26 | -------------------------------------------------------------------------------- /tests/test_image_plot_upper.py: -------------------------------------------------------------------------------- 1 | import pathlib 2 | 3 | import matplotlib.pyplot as plt 4 | import pytest 5 | 6 | # the picture 'lena.png' with origin='lower' is flipped upside-down. 7 | # So it has to be upside-down in the pdf-file as well. 8 | 9 | 10 | def plot(): 11 | import matplotlib.image as mpimg 12 | from matplotlib import rcParams 13 | 14 | this_dir = pathlib.Path(__file__).resolve().parent 15 | img = mpimg.imread(this_dir / "lena.png") 16 | 17 | dpi = rcParams["figure.dpi"] 18 | figsize = img.shape[0] / dpi, img.shape[1] / dpi 19 | fig = plt.figure(figsize=figsize) 20 | ax = plt.axes([0, 0, 1, 1], frameon=False) 21 | ax.set_axis_off() 22 | 23 | plt.imshow(img, cmap="viridis", origin="upper") 24 | 25 | # Setting the current color map to HSV messes up other plots 26 | # plt.hsv() 27 | plt.colorbar() 28 | return fig 29 | 30 | 31 | # TODO reintroduce 32 | @pytest.mark.skip("Fails?") 33 | def test(): 34 | from .helpers import assert_equality 35 | 36 | assert_equality(plot, __file__[:-3] + "_reference.tex") 37 | -------------------------------------------------------------------------------- /tests/test_image_plot_upper_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | colorbar, 7 | colorbar style={ylabel={}}, 8 | colormap/viridis, 9 | hide x axis, 10 | hide y axis, 11 | point meta max=0.93333334, 12 | point meta min=0.10196079, 13 | tick align=outside, 14 | tick pos=left, 15 | x grid style={darkgray176}, 16 | xmin=-0.5, xmax=15.5, 17 | xtick style={color=black}, 18 | y dir=reverse, 19 | y grid style={darkgray176}, 20 | ymin=-0.5, ymax=15.5, 21 | ytick style={color=black} 22 | ] 23 | \addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=15.5, ymin=15.5, ymax=-0.5] {tmp-000.png}; 24 | \end{axis} 25 | 26 | \end{tikzpicture} 27 | -------------------------------------------------------------------------------- /tests/test_legend3.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | 3 | 4 | def plot(): 5 | # mpl.style.use("seaborn-colorblind") 6 | fig = plt.figure(figsize=(7, 4)) 7 | ax = fig.add_subplot(111) 8 | ax.fill_between([1, 2], [2, 2], [3, 3], color="red", alpha=0.2, label="roh") 9 | ax.fill_between([1, 2], [4, 4], [5, 5], color="blue", alpha=0.2, label="kal") 10 | ax.plot([1, 2], [2, 5], "k", label="ref") 11 | ax.grid() 12 | plt.legend() 13 | return fig 14 | -------------------------------------------------------------------------------- /tests/test_legend3_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{lightgray204}{RGB}{204,204,204} 5 | 6 | \begin{axis}[ 7 | legend cell align={left}, 8 | legend style={ 9 | fill opacity=0.8, 10 | draw opacity=1, 11 | text opacity=1, 12 | at={(0.03,0.97)}, 13 | anchor=north west, 14 | draw=lightgray204 15 | }, 16 | tick align=outside, 17 | tick pos=left, 18 | x grid style={darkgray176}, 19 | xmajorgrids, 20 | xmin=0.95, xmax=2.05, 21 | xtick style={color=black}, 22 | y grid style={darkgray176}, 23 | ymajorgrids, 24 | ymin=1.85, ymax=5.15, 25 | ytick style={color=black} 26 | ] 27 | \path [draw=red, fill=red, opacity=0.2] 28 | (axis cs:1,3) 29 | --(axis cs:1,2) 30 | --(axis cs:2,2) 31 | --(axis cs:2,3) 32 | --(axis cs:2,3) 33 | --(axis cs:1,3) 34 | --cycle; 35 | \addlegendimage{area legend, draw=red, fill=red, opacity=0.2} 36 | \addlegendentry{roh} 37 | 38 | \path [draw=blue, fill=blue, opacity=0.2] 39 | (axis cs:1,5) 40 | --(axis cs:1,4) 41 | --(axis cs:2,4) 42 | --(axis cs:2,5) 43 | --(axis cs:2,5) 44 | --(axis cs:1,5) 45 | --cycle; 46 | \addlegendimage{area legend, draw=blue, fill=blue, opacity=0.2} 47 | \addlegendentry{kal} 48 | 49 | \addplot [semithick, black] 50 | table {% 51 | 1 2 52 | 2 5 53 | }; 54 | \addlegendentry{ref} 55 | \end{axis} 56 | 57 | \end{tikzpicture} 58 | -------------------------------------------------------------------------------- /tests/test_legend_best_location.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import pytest 4 | 5 | 6 | def plot(): 7 | fig, ax = plt.subplots(3, 3, sharex="col", sharey="row") 8 | axes = [ax[i][j] for i in range(len(ax)) for j in range(len(ax[i]))] 9 | t = np.arange(0.0, 2.0 * np.pi, 0.4) 10 | 11 | # Legend best location is "upper right" 12 | (l,) = axes[0].plot(t, np.cos(t) * np.exp(-t), linewidth=0.5) 13 | axes[0].legend((l,), ("UR",), loc=0) 14 | 15 | # Legend best location is "upper left" 16 | (l,) = axes[1].plot(t, np.cos(t) * np.exp(0.15 * t), linewidth=0.5) 17 | axes[1].legend((l,), ("UL",), loc=0) 18 | 19 | # Legend best location is "lower left" 20 | (l,) = axes[2].plot(t, np.cos(5.0 * t) + 1, linewidth=0.5) 21 | axes[2].legend((l,), ("LL",), loc=0) 22 | 23 | # Legend best location is "lower right" 24 | (l,) = axes[3].plot( 25 | t, 2 * np.cos(5.0 * t) * np.exp(-0.5 * t) + 0.2 * t, linewidth=0.5 26 | ) 27 | axes[3].legend((l,), ("LR",), loc=0) 28 | 29 | # Legend best location is "center left" 30 | (l,) = axes[4].plot(t[30:], 2 * np.cos(10 * t[30:]), linewidth=0.5) 31 | axes[4].plot(t, -1.5 * np.ones_like(t), t, 1.5 * np.ones_like(t)) 32 | axes[4].legend((l,), ("CL",), loc=0) 33 | 34 | # Legend best location is "center right" 35 | (l,) = axes[5].plot(t[:30], 2 * np.cos(10 * t[:30]), linewidth=0.5) 36 | axes[5].plot(t, -1.5 * np.ones_like(t), t, 1.5 * np.ones_like(t)) 37 | axes[5].legend((l,), ("CR",), loc=0) 38 | 39 | # Legend best location is "lower center" 40 | (l,) = axes[6].plot(t, -3 * np.cos(t) * np.exp(-0.1 * t), linewidth=0.5) 41 | axes[6].legend((l,), ("LC",), loc=0) 42 | 43 | # Legend best location is "upper center" 44 | (l,) = axes[7].plot(t, 3 * np.cos(t) * np.exp(-0.1 * t), linewidth=0.5) 45 | axes[7].legend((l,), ("UC",), loc=0) 46 | 47 | # Legend best location is "center" 48 | loc = axes[8].plot( 49 | t[:10], 50 | 2 * np.cos(10 * t[:10]), 51 | t[-10:], 52 | 2 * np.cos(10 * t[-10:]), 53 | linewidth=0.5, 54 | ) 55 | axes[8].plot(t, -2 * np.ones_like(t), t, 2 * np.ones_like(t)) 56 | axes[8].legend((loc,), ("C",), loc=0) 57 | 58 | return fig 59 | 60 | 61 | # TODO find a way to make this robust 62 | @pytest.mark.skip(reason="'Best' location depends on window size.") 63 | def test(): 64 | from .helpers import assert_equality 65 | 66 | assert_equality(plot, "test_legend_best_location_reference.tex") 67 | -------------------------------------------------------------------------------- /tests/test_legend_columns.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import matplotlib.pyplot as plt 3 | import numpy as np 4 | 5 | fig, ax = plt.subplots(figsize=(17, 6)) 6 | ax.plot(np.array([1, 5]), label="Test 1") 7 | ax.plot(np.array([5, 1]), label="Test 2") 8 | ax.legend(ncol=2, loc="upper center") 9 | return fig 10 | 11 | 12 | def test(): 13 | from .helpers import assert_equality 14 | 15 | assert_equality(plot, "test_legend_columns_reference.tex") 16 | -------------------------------------------------------------------------------- /tests/test_legend_columns_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{darkorange25512714}{RGB}{255,127,14} 5 | \definecolor{lightgray204}{RGB}{204,204,204} 6 | \definecolor{steelblue31119180}{RGB}{31,119,180} 7 | 8 | \begin{axis}[ 9 | legend cell align={left}, 10 | legend columns=2, 11 | legend style={ 12 | fill opacity=0.8, 13 | draw opacity=1, 14 | text opacity=1, 15 | at={(0.5,0.91)}, 16 | anchor=north, 17 | draw=lightgray204 18 | }, 19 | tick align=outside, 20 | tick pos=left, 21 | x grid style={darkgray176}, 22 | xmin=-0.05, xmax=1.05, 23 | xtick style={color=black}, 24 | y grid style={darkgray176}, 25 | ymin=0.8, ymax=5.2, 26 | ytick style={color=black} 27 | ] 28 | \addplot [semithick, steelblue31119180] 29 | table {% 30 | 0 1 31 | 1 5 32 | }; 33 | \addlegendentry{Test 1} 34 | \addplot [semithick, darkorange25512714] 35 | table {% 36 | 0 5 37 | 1 1 38 | }; 39 | \addlegendentry{Test 2} 40 | \end{axis} 41 | 42 | \end{tikzpicture} 43 | -------------------------------------------------------------------------------- /tests/test_legend_labels.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | fig = plt.figure() 6 | 7 | x = np.ma.arange(0, 2 * np.pi, 0.4) 8 | y1 = np.sin(1 * x) 9 | y2 = np.sin(2 * x) 10 | y3 = np.sin(3 * x) 11 | 12 | plt.plot(x, y1, label="y1") 13 | plt.plot(x, y2, label=None) 14 | plt.plot(x, y3, label="y4") 15 | plt.legend() 16 | 17 | return fig 18 | 19 | 20 | def test(): 21 | from .helpers import assert_equality 22 | 23 | assert_equality(plot, "test_legend_labels_reference.tex") 24 | -------------------------------------------------------------------------------- /tests/test_legend_labels_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{darkorange25512714}{RGB}{255,127,14} 5 | \definecolor{forestgreen4416044}{RGB}{44,160,44} 6 | \definecolor{lightgray204}{RGB}{204,204,204} 7 | \definecolor{steelblue31119180}{RGB}{31,119,180} 8 | 9 | \begin{axis}[ 10 | legend cell align={left}, 11 | legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=lightgray204}, 12 | tick align=outside, 13 | tick pos=left, 14 | x grid style={darkgray176}, 15 | xmin=-0.3, xmax=6.3, 16 | xtick style={color=black}, 17 | y grid style={darkgray176}, 18 | ymin=-1.0959515, ymax=1.0993605, 19 | ytick style={color=black} 20 | ] 21 | \addplot [semithick, steelblue31119180] 22 | table {% 23 | 0 0 24 | 0.4 0.38941834 25 | 0.8 0.71735609 26 | 1.2 0.93203909 27 | 1.6 0.9995736 28 | 2 0.90929743 29 | 2.4 0.67546318 30 | 2.8 0.33498815 31 | 3.2 -0.058374143 32 | 3.6 -0.44252044 33 | 4 -0.7568025 34 | 4.4 -0.95160207 35 | 4.8 -0.99616461 36 | 5.2 -0.88345466 37 | 5.6 -0.63126664 38 | 6 -0.2794155 39 | }; 40 | \addlegendentry{y1} 41 | \addplot [semithick, darkorange25512714, forget plot] 42 | table {% 43 | 0 0 44 | 0.4 0.71735609 45 | 0.8 0.9995736 46 | 1.2 0.67546318 47 | 1.6 -0.058374143 48 | 2 -0.7568025 49 | 2.4 -0.99616461 50 | 2.8 -0.63126664 51 | 3.2 0.1165492 52 | 3.6 0.79366786 53 | 4 0.98935825 54 | 4.4 0.58491719 55 | 4.8 -0.17432678 56 | 5.2 -0.82782647 57 | 5.6 -0.97917773 58 | 6 -0.53657292 59 | }; 60 | \addplot [semithick, forestgreen4416044] 61 | table {% 62 | 0 0 63 | 0.4 0.93203909 64 | 0.8 0.67546318 65 | 1.2 -0.44252044 66 | 1.6 -0.99616461 67 | 2 -0.2794155 68 | 2.4 0.79366786 69 | 2.8 0.85459891 70 | 3.2 -0.17432678 71 | 3.6 -0.98093623 72 | 4 -0.53657292 73 | 4.4 0.59207351 74 | 4.8 0.96565778 75 | 5.2 0.10775365 76 | 5.6 -0.88756703 77 | 6 -0.75098725 78 | }; 79 | \addlegendentry{y4} 80 | \end{axis} 81 | 82 | \end{tikzpicture} 83 | -------------------------------------------------------------------------------- /tests/test_legend_line_scatter.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | fig = plt.figure() 7 | t = np.arange(5) 8 | np.random.seed(123) 9 | x = t 10 | plt.plot(t, x, label="line") 11 | plt.scatter(t, x, label="scatter") 12 | plt.legend() 13 | return fig 14 | 15 | 16 | def test(): 17 | from .helpers import assert_equality 18 | 19 | assert_equality(plot, __file__[:-3] + "_reference.tex") 20 | -------------------------------------------------------------------------------- /tests/test_legend_line_scatter_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{lightgray204}{RGB}{204,204,204} 5 | \definecolor{steelblue31119180}{RGB}{31,119,180} 6 | 7 | \begin{axis}[ 8 | legend cell align={left}, 9 | legend style={ 10 | fill opacity=0.8, 11 | draw opacity=1, 12 | text opacity=1, 13 | at={(0.03,0.97)}, 14 | anchor=north west, 15 | draw=lightgray204 16 | }, 17 | tick align=outside, 18 | tick pos=left, 19 | x grid style={darkgray176}, 20 | xmin=-0.2, xmax=4.2, 21 | xtick style={color=black}, 22 | y grid style={darkgray176}, 23 | ymin=-0.2, ymax=4.2, 24 | ytick style={color=black} 25 | ] 26 | \addplot [draw=steelblue31119180, fill=steelblue31119180, mark=*, only marks] 27 | table{% 28 | x y 29 | 0 0 30 | 1 1 31 | 2 2 32 | 3 3 33 | 4 4 34 | }; 35 | \addlegendentry{scatter} 36 | \addplot [semithick, steelblue31119180] 37 | table {% 38 | 0 0 39 | 1 1 40 | 2 2 41 | 3 3 42 | 4 4 43 | }; 44 | \addlegendentry{line} 45 | \end{axis} 46 | 47 | \end{tikzpicture} 48 | -------------------------------------------------------------------------------- /tests/test_legends.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | fig = plt.figure() 6 | 7 | x = np.ma.arange(0, 2 * np.pi, 0.4) 8 | y = np.ma.sin(x) 9 | y1 = np.sin(2 * x) 10 | y2 = np.sin(3 * x) 11 | ym1 = np.ma.masked_where(y1 > 0.5, y1) 12 | ym2 = np.ma.masked_where(y2 < -0.5, y2) 13 | 14 | lines = plt.plot(x, y, "r", x, ym1, "g", x, ym2, "bo") 15 | plt.setp(lines[0], linewidth=4) 16 | plt.setp(lines[1], linewidth=2) 17 | plt.setp(lines[2], markersize=10) 18 | 19 | plt.legend(("No mask", "Masked if > 0.5", "Masked if < -0.5"), loc="upper right") 20 | plt.title("Masked line demo") 21 | return fig 22 | 23 | 24 | def test(): 25 | from .helpers import assert_equality 26 | 27 | assert_equality(plot, __file__[:-3] + "_reference.tex") 28 | -------------------------------------------------------------------------------- /tests/test_legends2.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | fig, ax = plt.subplots(3, 3, sharex="col", sharey="row") 6 | axes = [ax[i][j] for i in range(len(ax)) for j in range(len(ax[i]))] 7 | for k, loc in enumerate(range(2, 11)): 8 | t1 = np.arange(0.0, 2.0, 0.4) 9 | t2 = np.arange(0.0, 2.0, 0.4) 10 | 11 | # note that plot returns a list of lines. The 'l1, = plot' usage extracts the 12 | # first element of the list into l1 using tuple unpacking. So l1 is a Line2D 13 | # instance, not a sequence of lines 14 | (l1,) = axes[k].plot(t2, np.exp(-t2), linewidth=0.5) 15 | axes[k].plot(t2, np.sin(2 * np.pi * t2), "--go", t1, np.log(1 + t1), ".") 16 | axes[k].plot(t2, np.exp(-t2) * np.sin(2 * np.pi * t2), "rs-.") 17 | 18 | axes[k].legend((l1,), ("loc %d" % loc,), loc=loc) 19 | return fig 20 | 21 | 22 | def test(): 23 | from .helpers import assert_equality 24 | 25 | assert_equality(plot, __file__[:-3] + "_reference.tex") 26 | -------------------------------------------------------------------------------- /tests/test_legends_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{green01270}{RGB}{0,127,0} 5 | \definecolor{lightgray204}{RGB}{204,204,204} 6 | 7 | \begin{axis}[ 8 | legend cell align={left}, 9 | legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=lightgray204}, 10 | tick align=outside, 11 | tick pos=left, 12 | title={Masked line demo}, 13 | unbounded coords=jump, 14 | x grid style={darkgray176}, 15 | xmin=-0.3, xmax=6.3, 16 | xtick style={color=black}, 17 | y grid style={darkgray176}, 18 | ymin=-1.0959515, ymax=1.0993605, 19 | ytick style={color=black} 20 | ] 21 | \addplot [ultra thick, red] 22 | table {% 23 | 0 0 24 | 0.4 0.38941834 25 | 0.8 0.71735609 26 | 1.2 0.93203909 27 | 1.6 0.9995736 28 | 2 0.90929743 29 | 2.4 0.67546318 30 | 2.8 0.33498815 31 | 3.2 -0.058374143 32 | 3.6 -0.44252044 33 | 4 -0.7568025 34 | 4.4 -0.95160207 35 | 4.8 -0.99616461 36 | 5.2 -0.88345466 37 | 5.6 -0.63126664 38 | 6 -0.2794155 39 | }; 40 | \addlegendentry{No mask} 41 | \addplot [thick, green01270] 42 | table {% 43 | 0 0 44 | 0.4 nan 45 | 0.8 nan 46 | 1.2 nan 47 | 1.6 -0.058374143 48 | 2 -0.7568025 49 | 2.4 -0.99616461 50 | 2.8 -0.63126664 51 | 3.2 0.1165492 52 | 3.6 nan 53 | 4 nan 54 | 4.4 nan 55 | 4.8 -0.17432678 56 | 5.2 -0.82782647 57 | 5.6 -0.97917773 58 | 6 -0.53657292 59 | }; 60 | \addlegendentry{Masked if > 0.5} 61 | \addplot [semithick, blue, mark=*, mark size=5, mark options={solid}, only marks] 62 | table {% 63 | 0 0 64 | 0.4 0.93203909 65 | 0.8 0.67546318 66 | 1.2 -0.44252044 67 | 1.6 nan 68 | 2 -0.2794155 69 | 2.4 0.79366786 70 | 2.8 0.85459891 71 | 3.2 -0.17432678 72 | 3.6 nan 73 | 4 nan 74 | 4.4 0.59207351 75 | 4.8 0.96565778 76 | 5.2 0.10775365 77 | 5.6 nan 78 | 6 nan 79 | }; 80 | \addlegendentry{Masked if < -0.5} 81 | \end{axis} 82 | 83 | \end{tikzpicture} 84 | -------------------------------------------------------------------------------- /tests/test_line_collection.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | # Example from 3 | # 4 | import matplotlib.pyplot as plt 5 | import numpy as np 6 | from matplotlib.collections import LineCollection 7 | 8 | fig = plt.figure() 9 | 10 | # In order to efficiently plot many lines in a single set of axes, 11 | # Matplotlib has the ability to add the lines all at once. Here is a 12 | # simple example showing how it is done. 13 | 14 | N = 10 15 | x = np.arange(N) 16 | # Here are many sets of y to plot vs x 17 | ys = [x + i for i in x] 18 | 19 | # We need to set the plot limits, they will not autoscale 20 | ax = plt.axes() 21 | ax.set_xlim((np.amin(x), np.amax(x))) 22 | ax.set_ylim((np.amin(np.amin(ys)), np.amax(np.amax(ys)))) 23 | 24 | # colors is sequence of rgba tuples 25 | # linestyle is a string or dash tuple. Legal string values are 26 | # solid|dashed|dashdot|dotted. The dash tuple is (offset, 27 | # onoffseq) 28 | # where onoffseq is an even length tuple of on and off ink in 29 | # points. 30 | # If linestyle is omitted, 'solid' is used 31 | # See matplotlib.collections.LineCollection for more information 32 | 33 | # Make a sequence of x,y pairs 34 | line_segments = LineCollection( 35 | [list(zip(x, y)) for y in ys], linewidths=(0.5, 1, 1.5, 2), linestyles="dashdot" 36 | ) 37 | line_segments.set_array(x) 38 | ax.add_collection(line_segments) 39 | fig = plt.gcf() 40 | axcb = fig.colorbar(line_segments) 41 | axcb.set_label("Line Number") 42 | ax.set_title("Line Collection with mapped colors") 43 | plt.sci(line_segments) # This allows interactive changing of the colormap. 44 | 45 | return fig 46 | 47 | 48 | def test(): 49 | from .helpers import assert_equality 50 | 51 | assert_equality(plot, __file__[:-3] + "_reference.tex") 52 | -------------------------------------------------------------------------------- /tests/test_line_collection_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{steelblue31119180}{RGB}{31,119,180} 5 | 6 | \begin{axis}[ 7 | colorbar, 8 | colorbar style={ylabel={Line Number}}, 9 | colormap/viridis, 10 | point meta max=9, 11 | point meta min=0, 12 | tick align=outside, 13 | tick pos=left, 14 | title={Line Collection with mapped colors}, 15 | x grid style={darkgray176}, 16 | xmin=0, xmax=9, 17 | xtick style={color=black}, 18 | y grid style={darkgray176}, 19 | ymin=0, ymax=18, 20 | ytick style={color=black} 21 | ] 22 | \path [draw=steelblue31119180, very thin, dash pattern=on 3.2pt off 0.8pt on 0.5pt off 0.8pt] 23 | (axis cs:0,0) 24 | --(axis cs:1,1) 25 | --(axis cs:2,2) 26 | --(axis cs:3,3) 27 | --(axis cs:4,4) 28 | --(axis cs:5,5) 29 | --(axis cs:6,6) 30 | --(axis cs:7,7) 31 | --(axis cs:8,8) 32 | --(axis cs:9,9); 33 | 34 | \path [draw=steelblue31119180, dash pattern=on 6.4pt off 1.6pt on 1pt off 1.6pt] 35 | (axis cs:0,1) 36 | --(axis cs:1,2) 37 | --(axis cs:2,3) 38 | --(axis cs:3,4) 39 | --(axis cs:4,5) 40 | --(axis cs:5,6) 41 | --(axis cs:6,7) 42 | --(axis cs:7,8) 43 | --(axis cs:8,9) 44 | --(axis cs:9,10); 45 | 46 | \path [draw=steelblue31119180, semithick, dash pattern=on 9.6pt off 2.4pt on 1.5pt off 2.4pt] 47 | (axis cs:0,2) 48 | --(axis cs:1,3) 49 | --(axis cs:2,4) 50 | --(axis cs:3,5) 51 | --(axis cs:4,6) 52 | --(axis cs:5,7) 53 | --(axis cs:6,8) 54 | --(axis cs:7,9) 55 | --(axis cs:8,10) 56 | --(axis cs:9,11); 57 | 58 | \path [draw=steelblue31119180, thick, dash pattern=on 12.8pt off 3.2pt on 2pt off 3.2pt] 59 | (axis cs:0,3) 60 | --(axis cs:1,4) 61 | --(axis cs:2,5) 62 | --(axis cs:3,6) 63 | --(axis cs:4,7) 64 | --(axis cs:5,8) 65 | --(axis cs:6,9) 66 | --(axis cs:7,10) 67 | --(axis cs:8,11) 68 | --(axis cs:9,12); 69 | 70 | \path [draw=steelblue31119180, very thin, dash pattern=on 3.2pt off 0.8pt on 0.5pt off 0.8pt] 71 | (axis cs:0,4) 72 | --(axis cs:1,5) 73 | --(axis cs:2,6) 74 | --(axis cs:3,7) 75 | --(axis cs:4,8) 76 | --(axis cs:5,9) 77 | --(axis cs:6,10) 78 | --(axis cs:7,11) 79 | --(axis cs:8,12) 80 | --(axis cs:9,13); 81 | 82 | \path [draw=steelblue31119180, very thin, dash pattern=on 3.2pt off 0.8pt on 0.5pt off 0.8pt] 83 | (axis cs:0,5) 84 | --(axis cs:1,6) 85 | --(axis cs:2,7) 86 | --(axis cs:3,8) 87 | --(axis cs:4,9) 88 | --(axis cs:5,10) 89 | --(axis cs:6,11) 90 | --(axis cs:7,12) 91 | --(axis cs:8,13) 92 | --(axis cs:9,14); 93 | 94 | \path [draw=steelblue31119180, very thin, dash pattern=on 3.2pt off 0.8pt on 0.5pt off 0.8pt] 95 | (axis cs:0,6) 96 | --(axis cs:1,7) 97 | --(axis cs:2,8) 98 | --(axis cs:3,9) 99 | --(axis cs:4,10) 100 | --(axis cs:5,11) 101 | --(axis cs:6,12) 102 | --(axis cs:7,13) 103 | --(axis cs:8,14) 104 | --(axis cs:9,15); 105 | 106 | \path [draw=steelblue31119180, very thin, dash pattern=on 3.2pt off 0.8pt on 0.5pt off 0.8pt] 107 | (axis cs:0,7) 108 | --(axis cs:1,8) 109 | --(axis cs:2,9) 110 | --(axis cs:3,10) 111 | --(axis cs:4,11) 112 | --(axis cs:5,12) 113 | --(axis cs:6,13) 114 | --(axis cs:7,14) 115 | --(axis cs:8,15) 116 | --(axis cs:9,16); 117 | 118 | \path [draw=steelblue31119180, very thin, dash pattern=on 3.2pt off 0.8pt on 0.5pt off 0.8pt] 119 | (axis cs:0,8) 120 | --(axis cs:1,9) 121 | --(axis cs:2,10) 122 | --(axis cs:3,11) 123 | --(axis cs:4,12) 124 | --(axis cs:5,13) 125 | --(axis cs:6,14) 126 | --(axis cs:7,15) 127 | --(axis cs:8,16) 128 | --(axis cs:9,17); 129 | 130 | \path [draw=steelblue31119180, very thin, dash pattern=on 3.2pt off 0.8pt on 0.5pt off 0.8pt] 131 | (axis cs:0,9) 132 | --(axis cs:1,10) 133 | --(axis cs:2,11) 134 | --(axis cs:3,12) 135 | --(axis cs:4,13) 136 | --(axis cs:5,14) 137 | --(axis cs:6,15) 138 | --(axis cs:7,16) 139 | --(axis cs:8,17) 140 | --(axis cs:9,18); 141 | 142 | \end{axis} 143 | 144 | \end{tikzpicture} 145 | -------------------------------------------------------------------------------- /tests/test_line_color_marker.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | fig = plt.figure() 6 | with plt.style.context("ggplot"): 7 | t = np.arange(0.0, 2.0, 0.1) 8 | s = np.sin(2 * np.pi * t) 9 | s2 = np.cos(2 * np.pi * t) 10 | plt.plot(t, s, ".-", lw=1.5, color="C0") 11 | plt.plot(t, s2, "^-", lw=3, color="C1") 12 | plt.xlabel("time(s)") 13 | # plt.xlabel('time(s) _ % $ \\') 14 | plt.ylabel("Voltage (mV)") 15 | plt.title("Simple plot $\\frac{\\alpha}{2}$") 16 | plt.grid(True) 17 | return fig 18 | 19 | 20 | def test(): 21 | from .helpers import assert_equality 22 | 23 | assert_equality(plot, __file__[:-3] + "_reference.tex") 24 | -------------------------------------------------------------------------------- /tests/test_line_color_marker_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkorange25512714}{RGB}{255,127,14} 4 | \definecolor{dimgray85}{RGB}{85,85,85} 5 | \definecolor{gainsboro229}{RGB}{229,229,229} 6 | \definecolor{steelblue31119180}{RGB}{31,119,180} 7 | 8 | \begin{axis}[ 9 | axis background/.style={fill=gainsboro229}, 10 | axis line style={white}, 11 | tick align=outside, 12 | tick pos=left, 13 | title={Simple plot \(\displaystyle \frac{\alpha}{2}\)}, 14 | x grid style={white}, 15 | xlabel=\textcolor{dimgray85}{time(s)}, 16 | xmajorgrids, 17 | xmin=-0.095, xmax=1.995, 18 | xtick style={color=dimgray85}, 19 | y grid style={white}, 20 | ylabel=\textcolor{dimgray85}{Voltage (mV)}, 21 | ymajorgrids, 22 | ymin=-1.1, ymax=1.1, 23 | ytick style={color=dimgray85} 24 | ] 25 | \addplot [semithick, steelblue31119180, mark=*, mark size=3, mark options={solid}] 26 | table {% 27 | 0 0 28 | 0.1 0.58778525 29 | 0.2 0.95105652 30 | 0.3 0.95105652 31 | 0.4 0.58778525 32 | 0.5 1.2246468e-16 33 | 0.6 -0.58778525 34 | 0.7 -0.95105652 35 | 0.8 -0.95105652 36 | 0.9 -0.58778525 37 | 1 -2.4492936e-16 38 | 1.1 0.58778525 39 | 1.2 0.95105652 40 | 1.3 0.95105652 41 | 1.4 0.58778525 42 | 1.5 3.6739404e-16 43 | 1.6 -0.58778525 44 | 1.7 -0.95105652 45 | 1.8 -0.95105652 46 | 1.9 -0.58778525 47 | }; 48 | \addplot [very thick, darkorange25512714, mark=triangle*, mark size=3, mark options={solid}] 49 | table {% 50 | 0 1 51 | 0.1 0.80901699 52 | 0.2 0.30901699 53 | 0.3 -0.30901699 54 | 0.4 -0.80901699 55 | 0.5 -1 56 | 0.6 -0.80901699 57 | 0.7 -0.30901699 58 | 0.8 0.30901699 59 | 0.9 0.80901699 60 | 1 1 61 | 1.1 0.80901699 62 | 1.2 0.30901699 63 | 1.3 -0.30901699 64 | 1.4 -0.80901699 65 | 1.5 -1 66 | 1.6 -0.80901699 67 | 1.7 -0.30901699 68 | 1.8 0.30901699 69 | 1.9 0.80901699 70 | }; 71 | \end{axis} 72 | 73 | \end{tikzpicture} 74 | -------------------------------------------------------------------------------- /tests/test_line_dashes.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | 3 | 4 | def plot(): 5 | fig = plt.figure() 6 | linestyles = ["-", "--", "-.", ":", (0, (10, 1)), (5, (10, 1)), (0, (1, 2, 3, 4))] 7 | for idx, ls in enumerate(linestyles): 8 | plt.plot([idx, idx + 1], linestyle=ls) 9 | return fig 10 | 11 | 12 | def test(): 13 | from .helpers import assert_equality 14 | 15 | assert_equality(plot, __file__[:-3] + "_reference.tex") 16 | -------------------------------------------------------------------------------- /tests/test_line_dashes_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{crimson2143940}{RGB}{214,39,40} 4 | \definecolor{darkgray176}{RGB}{176,176,176} 5 | \definecolor{darkorange25512714}{RGB}{255,127,14} 6 | \definecolor{forestgreen4416044}{RGB}{44,160,44} 7 | \definecolor{mediumpurple148103189}{RGB}{148,103,189} 8 | \definecolor{orchid227119194}{RGB}{227,119,194} 9 | \definecolor{sienna1408675}{RGB}{140,86,75} 10 | \definecolor{steelblue31119180}{RGB}{31,119,180} 11 | 12 | \begin{axis}[ 13 | tick align=outside, 14 | tick pos=left, 15 | x grid style={darkgray176}, 16 | xmin=-0.05, xmax=1.05, 17 | xtick style={color=black}, 18 | y grid style={darkgray176}, 19 | ymin=-0.35, ymax=7.35, 20 | ytick style={color=black} 21 | ] 22 | \addplot [semithick, steelblue31119180] 23 | table {% 24 | 0 0 25 | 1 1 26 | }; 27 | \addplot [semithick, darkorange25512714, dashed] 28 | table {% 29 | 0 1 30 | 1 2 31 | }; 32 | \addplot [semithick, forestgreen4416044, dash pattern=on 1pt off 3pt on 3pt off 3pt] 33 | table {% 34 | 0 2 35 | 1 3 36 | }; 37 | \addplot [semithick, crimson2143940, dotted] 38 | table {% 39 | 0 3 40 | 1 4 41 | }; 42 | \addplot [semithick, mediumpurple148103189, dash pattern=on 10pt off 1pt] 43 | table {% 44 | 0 4 45 | 1 5 46 | }; 47 | \addplot [semithick, sienna1408675, dash pattern=on 10pt off 1pt, dash phase=5pt] 48 | table {% 49 | 0 5 50 | 1 6 51 | }; 52 | \addplot [semithick, orchid227119194, dash pattern=on 1pt off 2pt on 3pt off 4pt] 53 | table {% 54 | 0 6 55 | 1 7 56 | }; 57 | \end{axis} 58 | 59 | \end{tikzpicture} 60 | -------------------------------------------------------------------------------- /tests/test_line_set_data.py: -------------------------------------------------------------------------------- 1 | # from 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def plot(): 6 | fig = plt.figure() 7 | line = plt.plot(0, 0, "kx")[0] 8 | line.set_data(0, 0) 9 | return fig 10 | 11 | 12 | def test(): 13 | from .helpers import assert_equality 14 | 15 | assert_equality(plot, "test_line_set_data_reference.tex") 16 | -------------------------------------------------------------------------------- /tests/test_line_set_data_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | tick align=outside, 7 | tick pos=left, 8 | x grid style={darkgray176}, 9 | xmin=-0.055, xmax=0.055, 10 | xtick style={color=black}, 11 | y grid style={darkgray176}, 12 | ymin=-0.055, ymax=0.055, 13 | ytick style={color=black} 14 | ] 15 | \addplot [semithick, black, mark=x, mark size=3, mark options={solid}, only marks] 16 | table {% 17 | 0 0 18 | }; 19 | \end{axis} 20 | 21 | \end{tikzpicture} 22 | -------------------------------------------------------------------------------- /tests/test_loglogplot.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | fig = plt.figure() 7 | x = np.logspace(0, 6, num=5) 8 | plt.loglog(x, x**2, lw=2.1) 9 | return fig 10 | 11 | 12 | def test(): 13 | from .helpers import assert_equality 14 | 15 | assert_equality(plot, __file__[:-3] + "_reference.tex") 16 | -------------------------------------------------------------------------------- /tests/test_loglogplot_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{steelblue31119180}{RGB}{31,119,180} 5 | 6 | \begin{axis}[ 7 | log basis x={10}, 8 | log basis y={10}, 9 | tick align=outside, 10 | tick pos=left, 11 | x grid style={darkgray176}, 12 | xmin=0.50118723, xmax=1995262.3, 13 | xmode=log, 14 | xtick style={color=black}, 15 | y grid style={darkgray176}, 16 | ymin=0.25118864, ymax=3.9810717e+12, 17 | ymode=log, 18 | ytick style={color=black} 19 | ] 20 | \addplot [line width=0.84pt, steelblue31119180] 21 | table {% 22 | 1 1 23 | 31.622777 1000 24 | 1000 1000000 25 | 31622.777 1e+09 26 | 1000000 1e+12 27 | }; 28 | \end{axis} 29 | 30 | \end{tikzpicture} 31 | -------------------------------------------------------------------------------- /tests/test_logplot.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | 3 | 4 | def plot(): 5 | a = [pow(10, i) for i in range(10)] 6 | fig = plt.figure() 7 | ax = fig.add_subplot(1, 1, 1) 8 | ax.semilogy(a, color="blue", lw=0.25) 9 | 10 | plt.grid(visible=True, which="major", color="g", linestyle="-", linewidth=0.25) 11 | plt.grid(visible=True, which="minor", color="r", linestyle="--", linewidth=0.5) 12 | return fig 13 | 14 | 15 | def test(): 16 | from .helpers import assert_equality 17 | 18 | assert_equality(plot, __file__[:-3] + "_reference.tex") 19 | -------------------------------------------------------------------------------- /tests/test_logplot_base.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | from matplotlib import pyplot as plt 3 | 4 | a = [pow(10, i) for i in range(10)] 5 | fig = plt.figure() 6 | ax = fig.add_subplot(1, 1, 1) 7 | ax.semilogy(a, color="blue", lw=0.25, base=2) 8 | 9 | plt.grid(visible=True, which="major", color="g", linestyle="-", linewidth=0.25) 10 | plt.grid(visible=True, which="minor", color="r", linestyle="--", linewidth=0.5) 11 | return fig 12 | 13 | 14 | def test(): 15 | from .helpers import assert_equality 16 | 17 | assert_equality(plot, __file__[:-3] + "_reference.tex") 18 | -------------------------------------------------------------------------------- /tests/test_logplot_base_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{green01270}{RGB}{0,127,0} 4 | 5 | \begin{axis}[ 6 | log basis y={2}, 7 | tick align=outside, 8 | tick pos=left, 9 | x grid style={green01270}, 10 | xmajorgrids, 11 | xmin=-0.45, xmax=9.45, 12 | xminorgrids, 13 | xtick style={color=black}, 14 | y grid style={green01270}, 15 | ymajorgrids, 16 | ymin=0.35481339, ymax=2.8183829e+09, 17 | yminorgrids, 18 | ymode=log, 19 | ytick style={color=black} 20 | ] 21 | \addplot [ultra thin, blue] 22 | table {% 23 | 0 1 24 | 1 10 25 | 2 100 26 | 3 1000 27 | 4 10000 28 | 5 100000 29 | 6 1000000 30 | 7 10000000 31 | 8 1e+08 32 | 9 1e+09 33 | }; 34 | \end{axis} 35 | 36 | \end{tikzpicture} 37 | -------------------------------------------------------------------------------- /tests/test_logplot_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{green01270}{RGB}{0,127,0} 4 | 5 | \begin{axis}[ 6 | log basis y={10}, 7 | tick align=outside, 8 | tick pos=left, 9 | x grid style={green01270}, 10 | xmajorgrids, 11 | xmin=-0.45, xmax=9.45, 12 | xminorgrids, 13 | xtick style={color=black}, 14 | y grid style={green01270}, 15 | ymajorgrids, 16 | ymin=0.35481339, ymax=2.8183829e+09, 17 | yminorgrids, 18 | ymode=log, 19 | ytick style={color=black} 20 | ] 21 | \addplot [ultra thin, blue] 22 | table {% 23 | 0 1 24 | 1 10 25 | 2 100 26 | 3 1000 27 | 4 10000 28 | 5 100000 29 | 6 1000000 30 | 7 10000000 31 | 8 1e+08 32 | 9 1e+09 33 | }; 34 | \end{axis} 35 | 36 | \end{tikzpicture} 37 | -------------------------------------------------------------------------------- /tests/test_marker.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | fig, ax = plt.subplots() 7 | with plt.style.context("ggplot"): 8 | t = np.linspace(0, 2 * np.pi, 11) 9 | s = np.sin(t) 10 | c = np.cos(t) 11 | s2 = np.sin(2 * t) 12 | ax.plot(t, s, "ko-", mec="r", markevery=2, markersize=3) 13 | ax.plot(t, c, "ks--", mec="r", markevery=3) 14 | ax.scatter(t, s2, s=100, c="black", marker="+") 15 | ax.set_xlim(t[0], t[-1]) 16 | ax.set_xlabel("t") 17 | ax.set_ylabel("y") 18 | ax.set_title("Simple plot") 19 | ax.grid(True) 20 | return fig 21 | 22 | 23 | def test(): 24 | from .helpers import assert_equality 25 | 26 | assert_equality(plot, __file__[:-3] + "_reference.tex") 27 | -------------------------------------------------------------------------------- /tests/test_marker_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | tick align=outside, 7 | tick pos=left, 8 | title={Simple plot}, 9 | x grid style={darkgray176}, 10 | xlabel={t}, 11 | xmajorgrids, 12 | xmin=0, xmax=6.2831853, 13 | xtick style={color=black}, 14 | y grid style={darkgray176}, 15 | ylabel={y}, 16 | ymajorgrids, 17 | ymin=-1.1, ymax=1.1, 18 | ytick style={color=black} 19 | ] 20 | \addplot [draw=black, fill=black, mark=+, only marks] 21 | table{% 22 | x y 23 | 0 0 24 | 0.62831853 0.95105652 25 | 1.2566371 0.58778525 26 | 1.8849556 -0.58778525 27 | 2.5132741 -0.95105652 28 | 3.1415927 -2.4492936e-16 29 | 3.7699112 0.95105652 30 | 4.3982297 0.58778525 31 | 5.0265482 -0.58778525 32 | 5.6548668 -0.95105652 33 | 6.2831853 -4.8985872e-16 34 | }; 35 | \addplot [semithick, black, mark=*, mark size=1.5, mark repeat=2, mark options={solid,draw=red}] 36 | table {% 37 | 0 0 38 | 0.62831853 0.58778525 39 | 1.2566371 0.95105652 40 | 1.8849556 0.95105652 41 | 2.5132741 0.58778525 42 | 3.1415927 1.2246468e-16 43 | 3.7699112 -0.58778525 44 | 4.3982297 -0.95105652 45 | 5.0265482 -0.95105652 46 | 5.6548668 -0.58778525 47 | 6.2831853 -2.4492936e-16 48 | }; 49 | \addplot [semithick, black, dashed, mark=square*, mark size=3, mark repeat=3, mark options={solid,draw=red}] 50 | table {% 51 | 0 1 52 | 0.62831853 0.80901699 53 | 1.2566371 0.30901699 54 | 1.8849556 -0.30901699 55 | 2.5132741 -0.80901699 56 | 3.1415927 -1 57 | 3.7699112 -0.80901699 58 | 4.3982297 -0.30901699 59 | 5.0265482 0.30901699 60 | 5.6548668 0.80901699 61 | 6.2831853 1 62 | }; 63 | \end{axis} 64 | 65 | \end{tikzpicture} 66 | -------------------------------------------------------------------------------- /tests/test_noise.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | # Make plot with vertical (default) colorbar 7 | fig = plt.figure() 8 | ax = fig.add_subplot(111) 9 | 10 | np.random.seed(123) 11 | data = np.clip(np.random.randn(250, 250), -1, 1) 12 | 13 | cax = ax.imshow(data, interpolation="nearest") 14 | ax.set_title("Gaussian noise with vertical colorbar") 15 | 16 | # Add colorbar, make sure to specify tick locations to match desired ticklabels. 17 | cbar = fig.colorbar(cax, ticks=[-1, 0, 1]) 18 | # vertically oriented colorbar 19 | cbar.ax.set_yticklabels(["< -1", "0", "> 1"]) 20 | return fig 21 | 22 | 23 | def test(): 24 | from .helpers import assert_equality 25 | 26 | assert_equality(plot, __file__[:-3] + "_reference.tex") 27 | -------------------------------------------------------------------------------- /tests/test_noise2.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | # Make plot with horizontal colorbar 7 | fig = plt.figure() 8 | ax = fig.add_subplot(111) 9 | 10 | np.random.seed(123) 11 | data = np.clip(np.random.randn(250, 250), -1, 1) 12 | 13 | cax = ax.imshow(data, interpolation="nearest") 14 | ax.set_title("Gaussian noise with horizontal colorbar") 15 | 16 | cbar = fig.colorbar(cax, ticks=[-1, 0, 1], orientation="horizontal") 17 | # horizontal colorbar 18 | # Use comma in label 19 | cbar.ax.set_xticklabels(["Low", "Medium", "High,Higher"]) 20 | return fig 21 | 22 | 23 | def test(): 24 | from .helpers import assert_equality 25 | 26 | assert_equality(plot, __file__[:-3] + "_reference.tex") 27 | -------------------------------------------------------------------------------- /tests/test_noise2_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | colorbar horizontal, 7 | colorbar style={xtick={-1,0,1},xticklabels={Low,Medium,{High,Higher}}}, 8 | colormap/viridis, 9 | point meta max=1, 10 | point meta min=-1, 11 | tick align=outside, 12 | tick pos=left, 13 | title={Gaussian noise with horizontal colorbar}, 14 | x grid style={darkgray176}, 15 | xmin=-0.5, xmax=249.5, 16 | xtick style={color=black}, 17 | y dir=reverse, 18 | y grid style={darkgray176}, 19 | ymin=-0.5, ymax=249.5, 20 | ytick style={color=black} 21 | ] 22 | \addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=249.5, ymin=249.5, ymax=-0.5] {tmp-000.png}; 23 | \end{axis} 24 | 25 | \end{tikzpicture} 26 | -------------------------------------------------------------------------------- /tests/test_noise_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | colorbar, 7 | colorbar style={ytick={-1,0,1},yticklabels={< -1,0,> 1},ylabel={}}, 8 | colormap/viridis, 9 | point meta max=1, 10 | point meta min=-1, 11 | tick align=outside, 12 | tick pos=left, 13 | title={Gaussian noise with vertical colorbar}, 14 | x grid style={darkgray176}, 15 | xmin=-0.5, xmax=249.5, 16 | xtick style={color=black}, 17 | y dir=reverse, 18 | y grid style={darkgray176}, 19 | ymin=-0.5, ymax=249.5, 20 | ytick style={color=black} 21 | ] 22 | \addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=249.5, ymin=249.5, ymax=-0.5] {tmp-000.png}; 23 | \end{axis} 24 | 25 | \end{tikzpicture} 26 | -------------------------------------------------------------------------------- /tests/test_pandas_dataframe.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import pandas as pd 3 | 4 | 5 | def plot(): 6 | fig = plt.figure(1, figsize=(8, 5)) 7 | df = pd.DataFrame(index=["one", "two", "three"], data={"data": [1, 2, 3]}) 8 | plt.plot(df, "o") 9 | return fig 10 | 11 | 12 | def test(): 13 | from .helpers import assert_equality 14 | 15 | assert_equality(plot, __file__[:-3] + "_reference.tex") 16 | 17 | 18 | if __name__ == "__main__": 19 | plot() 20 | plt.show() 21 | -------------------------------------------------------------------------------- /tests/test_pandas_dataframe_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{steelblue31119180}{RGB}{31,119,180} 5 | 6 | \begin{axis}[ 7 | tick align=outside, 8 | tick pos=left, 9 | x grid style={darkgray176}, 10 | xmin=-0.1, xmax=2.1, 11 | xtick style={color=black}, 12 | xtick={0,1,2}, 13 | xticklabels={one,two,three}, 14 | y grid style={darkgray176}, 15 | ymin=0.9, ymax=3.1, 16 | ytick style={color=black} 17 | ] 18 | \addplot [semithick, steelblue31119180, mark=*, mark size=3, mark options={solid}, only marks] 19 | table {% 20 | 0 1 21 | 1 2 22 | 2 3 23 | }; 24 | \end{axis} 25 | 26 | \end{tikzpicture} 27 | -------------------------------------------------------------------------------- /tests/test_patch_styles.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | from matplotlib.collections import PolyCollection 3 | 4 | 5 | def plot(): 6 | fig = plt.figure() 7 | axis = fig.add_subplot(1, 1, 1) 8 | 9 | col = PolyCollection( 10 | [], facecolors="none", edgecolors="red", linestyle="--", linewidth=1.2 11 | ) 12 | col.set_zorder(1) 13 | axis.add_collection(col) 14 | axis.set_xlim(0.5, 2.5) 15 | axis.set_ylim(0.5, 2.5) 16 | axis.collections[0].set_verts([[[1, 1], [1, 2], [2, 2], [2, 1]]]) 17 | return fig 18 | 19 | 20 | def test(): 21 | from .helpers import assert_equality 22 | 23 | assert_equality(plot, __file__[:-3] + "_reference.tex") 24 | -------------------------------------------------------------------------------- /tests/test_patch_styles_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | tick align=outside, 7 | tick pos=left, 8 | x grid style={darkgray176}, 9 | xmin=0.5, xmax=2.5, 10 | xtick style={color=black}, 11 | y grid style={darkgray176}, 12 | ymin=0.5, ymax=2.5, 13 | ytick style={color=black} 14 | ] 15 | \path [draw=red, line width=0.48pt, dash pattern=on 4.44pt off 1.92pt] 16 | (axis cs:1,1) 17 | --(axis cs:1,2) 18 | --(axis cs:2,2) 19 | --(axis cs:2,1) 20 | --cycle; 21 | 22 | \end{axis} 23 | 24 | \end{tikzpicture} 25 | -------------------------------------------------------------------------------- /tests/test_patches.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import matplotlib as mpl 3 | import numpy as np 4 | from matplotlib import pyplot as plt 5 | from matplotlib.collections import PatchCollection 6 | from matplotlib.patches import ( 7 | Circle, 8 | Ellipse, 9 | FancyArrowPatch, 10 | Polygon, 11 | Rectangle, 12 | Wedge, 13 | ) 14 | from matplotlib.path import Path 15 | 16 | np.random.seed(123) 17 | 18 | fig = plt.figure() 19 | ax = fig.add_subplot(111) 20 | 21 | N = 3 22 | x = np.random.rand(N) 23 | y = np.random.rand(N) 24 | radii = 0.1 * np.random.rand(N) 25 | patches = [] 26 | for x1, y1, r in zip(x, y, radii): 27 | circle = Circle((x1, y1), r) 28 | patches.append(circle) 29 | 30 | rect = Rectangle(xy=[0.0, 0.25], width=1.0, height=0.5, angle=-45.0) 31 | patches.append(rect) 32 | 33 | x = np.random.rand(N) 34 | y = np.random.rand(N) 35 | radii = 0.1 * np.random.rand(N) 36 | theta1 = 360.0 * np.random.rand(N) 37 | theta2 = 360.0 * np.random.rand(N) 38 | for x1, y1, r, t1, t2 in zip(x, y, radii, theta1, theta2): 39 | wedge = Wedge((x1, y1), r, t1, t2) 40 | patches.append(wedge) 41 | 42 | # Some limiting conditions on Wedge 43 | patches += [ 44 | Wedge((0.3, 0.7), 0.1, 0, 360), # Full circle 45 | Wedge((0.7, 0.8), 0.2, 0, 360, width=0.05), # Full ring 46 | Wedge((0.8, 0.3), 0.2, 0, 45), # Full sector 47 | Wedge((0.8, 0.3), 0.2, 45, 90, width=0.10), # Ring sector 48 | ] 49 | 50 | for _ in range(N): 51 | polygon = Polygon(np.random.rand(N, 2), True) 52 | patches.append(polygon) 53 | 54 | colors = 100 * np.random.rand(len(patches)) 55 | p = PatchCollection(patches, cmap=mpl.cm.viridis, alpha=0.4) 56 | p.set_array(np.array(colors)) 57 | ax.add_collection(p) 58 | 59 | ellipse = Ellipse(xy=[1.0, 0.5], width=1.0, height=0.5, angle=45.0, alpha=0.4) 60 | ax.add_patch(ellipse) 61 | 62 | circle = Circle(xy=[0.0, 1.0], radius=0.5, color="r", alpha=0.4) 63 | ax.add_patch(circle) 64 | 65 | arrow = FancyArrowPatch(posA=[0.25, 0.25], posB=[0.5, 0.25], arrowstyle="->") 66 | ax.add_patch(arrow) 67 | 68 | curved_arrow = FancyArrowPatch( 69 | path=Path( 70 | [(0.3, 0.3), (0.5, 1.0), (1.0, 0.8), (0.8, 0.3)], 71 | [Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4], 72 | ), 73 | arrowstyle="-|>", 74 | ) 75 | ax.add_patch(curved_arrow) 76 | 77 | plt.colorbar(p) 78 | 79 | return fig 80 | 81 | 82 | def test(): 83 | from .helpers import assert_equality 84 | 85 | assert_equality(plot, __file__[:-3] + "_reference.tex") 86 | -------------------------------------------------------------------------------- /tests/test_quadmesh.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | x = np.linspace(0 * np.pi, 2 * np.pi, 128) 6 | y = np.linspace(0 * np.pi, 2 * np.pi, 128) 7 | X, Y = np.meshgrid(x, y) 8 | nu = 1e-5 9 | 10 | def F(t): 11 | return np.exp(-2 * nu * t) 12 | 13 | def u(x, y, t): 14 | return np.sin(x) * np.cos(y) * F(t) 15 | 16 | def v(x, y, t): 17 | return -np.cos(x) * np.sin(y) * F(t) 18 | 19 | fig, axs = plt.subplots(2, figsize=(8, 12)) 20 | axs[0].pcolormesh(X, Y, u(X, Y, 0), shading="gouraud") 21 | axs[1].pcolormesh(X, Y, v(X, Y, 0), shading="gouraud") 22 | for ax in axs: 23 | ax.set_xlim(x[0], x[-1]) 24 | ax.set_ylim(y[0], y[-1]) 25 | ax.set_xlabel("x") 26 | ax.set_ylabel("y") 27 | axs[0].set_title("Taylor--Green Vortex") 28 | 29 | return fig 30 | 31 | 32 | def test(): 33 | from .helpers import assert_equality 34 | 35 | assert_equality(plot, __file__[:-3] + "_reference.tex") 36 | -------------------------------------------------------------------------------- /tests/test_quadmesh_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{groupplot}[group style={group size=1 by 2}] 6 | \nextgroupplot[ 7 | tick align=outside, 8 | tick pos=left, 9 | title={Taylor--Green Vortex}, 10 | x grid style={darkgray176}, 11 | xlabel={x}, 12 | xmin=0, xmax=6.2831853, 13 | xtick style={color=black}, 14 | y grid style={darkgray176}, 15 | ylabel={y}, 16 | ymin=0, ymax=6.2831853, 17 | ytick style={color=black} 18 | ] 19 | \addplot graphics [includegraphics cmd=\pgfimage,xmin=0, xmax=6.2831853, ymin=0, ymax=6.2831853] {tmp-000.png}; 20 | 21 | \nextgroupplot[ 22 | tick align=outside, 23 | tick pos=left, 24 | x grid style={darkgray176}, 25 | xlabel={x}, 26 | xmin=0, xmax=6.2831853, 27 | xtick style={color=black}, 28 | y grid style={darkgray176}, 29 | ylabel={y}, 30 | ymin=0, ymax=6.2831853, 31 | ytick style={color=black} 32 | ] 33 | \addplot graphics [includegraphics cmd=\pgfimage,xmin=0, xmax=6.2831853, ymin=0, ymax=6.2831853] {tmp-001.png}; 34 | \end{groupplot} 35 | 36 | \end{tikzpicture} 37 | -------------------------------------------------------------------------------- /tests/test_rotated_labels.py: -------------------------------------------------------------------------------- 1 | import os 2 | import tempfile 3 | 4 | import pytest 5 | from matplotlib import pyplot as plt 6 | 7 | import tikzplotlib 8 | 9 | 10 | def __plot(): 11 | fig, ax = plt.subplots() 12 | 13 | x = [1, 2, 3, 4] 14 | y = [1, 4, 9, 6] 15 | 16 | plt.plot(x, y, "ro") 17 | plt.xticks(x, rotation="horizontal") 18 | 19 | return fig, ax 20 | 21 | 22 | @pytest.mark.parametrize( 23 | "x_alignment, y_alignment, x_tick_label_width, y_tick_label_width, rotation", 24 | [ 25 | (None, None, "1rem", "3rem", 90), 26 | (None, "center", "1rem", "3rem", 90), 27 | ("center", None, "1rem", "3rem", 90), 28 | ("center", "center", None, "3rem", 90), 29 | ("left", "left", None, "3rem", 90), 30 | ("right", "right", None, "3rem", 90), 31 | ("center", "center", "1rem", None, 90), 32 | ("left", "left", "2rem", None, 90), 33 | ("right", "right", "3rem", None, 90), 34 | ("center", "center", "1rem", "3rem", 90), 35 | ("left", "left", "2rem", "3rem", 90), 36 | ("right", "right", "3rem", "3rem", 90), 37 | ("left", "right", "2rem", "3rem", 90), 38 | ("right", "left", "3rem", "3rem", 90), 39 | ], 40 | ) 41 | def test_rotated_labels_parameters( 42 | x_alignment, y_alignment, x_tick_label_width, y_tick_label_width, rotation 43 | ): 44 | fig, _ = __plot() 45 | 46 | if x_alignment: 47 | plt.xticks(ha=x_alignment, rotation=rotation) 48 | if y_alignment: 49 | plt.yticks(ha=y_alignment, rotation=rotation) 50 | 51 | # convert to tikz file 52 | _, tmp_base = tempfile.mkstemp() 53 | tikz_file = tmp_base + "_tikz.tex" 54 | 55 | extra_dict = {} 56 | 57 | if x_tick_label_width: 58 | extra_dict["x tick label text width"] = x_tick_label_width 59 | if y_tick_label_width: 60 | extra_dict["y tick label text width"] = y_tick_label_width 61 | 62 | tikzplotlib.save(tikz_file, axis_width="7.5cm", extra_axis_parameters=extra_dict) 63 | 64 | # close figure 65 | plt.close(fig) 66 | 67 | # delete file 68 | os.unlink(tikz_file) 69 | 70 | 71 | @pytest.mark.parametrize( 72 | "x_tick_label_width, y_tick_label_width", 73 | [(None, None), ("1rem", None), (None, "3rem"), ("2rem", "3rem")], 74 | ) 75 | def test_rotated_labels_parameters_different_values( 76 | x_tick_label_width, y_tick_label_width 77 | ): 78 | fig, ax = __plot() 79 | 80 | plt.xticks(ha="left", rotation=90) 81 | plt.yticks(ha="left", rotation=90) 82 | ax.xaxis.get_majorticklabels()[0].set_rotation(20) 83 | ax.yaxis.get_majorticklabels()[0].set_horizontalalignment("right") 84 | 85 | # convert to tikz file 86 | _, tmp_base = tempfile.mkstemp() 87 | tikz_file = tmp_base + "_tikz.tex" 88 | 89 | extra_dict = {} 90 | 91 | if x_tick_label_width: 92 | extra_dict["x tick label text width"] = x_tick_label_width 93 | if y_tick_label_width: 94 | extra_dict["y tick label text width"] = y_tick_label_width 95 | 96 | tikzplotlib.save(tikz_file, axis_width="7.5cm", extra_axis_parameters=extra_dict) 97 | 98 | # close figure 99 | plt.close(fig) 100 | 101 | # delete file 102 | os.unlink(tikz_file) 103 | 104 | 105 | def test_rotated_labels_parameters_no_ticks(): 106 | fig, ax = __plot() 107 | 108 | ax.xaxis.set_ticks([]) 109 | 110 | plt.tick_params(axis="x", which="both", bottom="off", top="off") 111 | plt.tick_params(axis="y", which="both", left="off", right="off") 112 | 113 | # convert to tikz file 114 | _, tmp_base = tempfile.mkstemp() 115 | tikz_file = tmp_base + "_tikz.tex" 116 | 117 | tikzplotlib.save(tikz_file, axis_width="7.5cm") 118 | 119 | # close figure 120 | plt.close(fig) 121 | 122 | # delete file 123 | os.unlink(tikz_file) 124 | -------------------------------------------------------------------------------- /tests/test_scatter.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | fig = plt.figure() 6 | with plt.style.context("fivethirtyeight"): 7 | np.random.seed(123) 8 | plt.scatter( 9 | np.linspace(0, 100, 101), 10 | np.linspace(0, 100, 101) + 15 * np.random.rand(101), 11 | ) 12 | return fig 13 | 14 | 15 | def test(): 16 | from .helpers import assert_equality 17 | 18 | assert_equality(plot, __file__[:-3] + "_reference.tex") 19 | -------------------------------------------------------------------------------- /tests/test_scatter_colormap.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | fig = plt.figure() 6 | np.random.seed(123) 7 | plt.scatter( 8 | np.random.randn(10), 9 | np.random.randn(10), 10 | np.random.rand(10) * 90 + 10, 11 | np.random.randn(10), 12 | ) 13 | return fig 14 | 15 | 16 | def test(): 17 | from .helpers import assert_equality 18 | 19 | assert_equality(plot, __file__[:-3] + "_reference.tex") 20 | -------------------------------------------------------------------------------- /tests/test_scatter_colormap_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | tick align=outside, 7 | tick pos=left, 8 | x grid style={darkgray176}, 9 | xmin=-2.630585, xmax=1.8553423, 10 | xtick style={color=black}, 11 | y grid style={darkgray176}, 12 | ymin=-0.82312696, ymax=2.3501709, 13 | ytick style={color=black} 14 | ] 15 | \addplot [ 16 | colormap/viridis, 17 | only marks, 18 | scatter, 19 | scatter src=explicit, 20 | scatter/@pre marker code/.append style={/tikz/mark size=\perpointmarksize}, 21 | visualization depends on={\thisrow{sizedata} \as\perpointmarksize} 22 | ] 23 | table [x=x, y=y, meta=colordata]{% 24 | x y colordata sizedata 25 | -1.0856306 -0.67888615 -0.2556193705305969 4.892567336479345 26 | 0.99734545 -0.094708969 -2.7985891054607244 4.548365974103673 27 | 0.2829785 1.4913896 -1.771533104509847 4.886673433027043 28 | -1.5062947 -0.638902 -0.6998772345979173 3.5263565080399952 29 | -0.57860025 -0.44398196 0.9274624317585825 3.6807037891241934 30 | 1.6514365 -0.43435128 -0.1736356827902158 3.118069713064499 31 | -2.4266792 2.2059301 0.0028459158968110196 3.405493574026048 32 | -0.42891263 2.1867861 0.688222711102285 4.61077361155706 33 | 1.2659363 1.0040539 -0.8795363430090519 2.412822212144373 34 | -0.8667404 0.3861864 0.283627323807291 3.9506609882453407 35 | }; 36 | \end{axis} 37 | 38 | \end{tikzpicture} 39 | -------------------------------------------------------------------------------- /tests/test_scatter_different_colors.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | fig = plt.figure() 7 | np.random.seed(123) 8 | n = 4 9 | plt.scatter( 10 | np.random.rand(n), 11 | np.random.rand(n), 12 | color=np.array( 13 | [ 14 | [1.0, 0.6, 0.0], 15 | [0.0, 1.0, 0.0], 16 | [0.0, 0.0, 1.0], 17 | [0.0, 1.0, 1.0], 18 | ] 19 | ), 20 | edgecolors=np.array( 21 | [ 22 | [0.0, 1.0, 0.0], 23 | [0.0, 0.0, 1.0], 24 | [0.0, 1.0, 1.0], 25 | [1.0, 0.0, 0.0], 26 | ] 27 | ), 28 | ) 29 | return fig 30 | 31 | 32 | def test(): 33 | from .helpers import assert_equality 34 | 35 | assert_equality(plot, __file__[:-3] + "_reference.tex") 36 | -------------------------------------------------------------------------------- /tests/test_scatter_different_colors_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | tick align=outside, 7 | tick pos=left, 8 | x grid style={darkgray176}, 9 | xmin=0.20337057, xmax=0.71995007, 10 | xtick style={color=black}, 11 | y grid style={darkgray176}, 12 | ymin=0.39522357, ymax=1.0086471, 13 | ytick style={color=black} 14 | ] 15 | \addplot [ 16 | mark=*, 17 | only marks, 18 | scatter, 19 | scatter/@post marker code/.code={% 20 | \endscope 21 | }, 22 | scatter/@pre marker code/.code={% 23 | \expanded{% 24 | \noexpand\definecolor{thispointdrawcolor}{RGB}{\drawcolor}% 25 | \noexpand\definecolor{thispointfillcolor}{RGB}{\fillcolor}% 26 | }% 27 | \scope[draw=thispointdrawcolor, fill=thispointfillcolor]% 28 | }, 29 | visualization depends on={value \thisrow{draw} \as \drawcolor}, 30 | visualization depends on={value \thisrow{fill} \as \fillcolor} 31 | ] 32 | table{% 33 | x y draw fill 34 | 0.69646919 0.71946897 0,255,0 255,153,0 35 | 0.28613933 0.42310646 0,0,255 0,255,0 36 | 0.22685145 0.9807642 0,255,255 0,0,255 37 | 0.55131477 0.68482974 255,0,0 0,255,255 38 | }; 39 | \end{axis} 40 | 41 | \end{tikzpicture} 42 | -------------------------------------------------------------------------------- /tests/test_scatter_different_sizes.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | 3 | 4 | # https://github.com/nschloe/tikzplotlib/issues/414 5 | def plot(): 6 | _, ax = plt.subplots() 7 | ax.scatter( 8 | [1, 2, 3], 9 | [5, 7, 1], 10 | s=[300, 300, 300], 11 | facecolors="none", 12 | edgecolors="black", 13 | linewidths=3.0, 14 | ) 15 | 16 | 17 | def test(): 18 | from .helpers import assert_equality 19 | 20 | assert_equality(plot, __file__[:-3] + "_reference.tex") 21 | -------------------------------------------------------------------------------- /tests/test_scatter_different_sizes_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | tick align=outside, 7 | tick pos=left, 8 | x grid style={darkgray176}, 9 | xmin=0.9, xmax=3.1, 10 | xtick style={color=black}, 11 | y grid style={darkgray176}, 12 | ymin=0.7, ymax=7.3, 13 | ytick style={color=black} 14 | ] 15 | \addplot [ 16 | draw=black, 17 | mark=o, 18 | only marks, 19 | scatter, 20 | scatter/@pre marker code/.append style={/tikz/mark size=\perpointmarksize}, 21 | visualization depends on={\thisrow{sizedata} \as\perpointmarksize} 22 | ] 23 | table{% 24 | x y sizedata 25 | 1 5 9.772050238058398 26 | 2 7 9.772050238058398 27 | 3 1 9.772050238058398 28 | }; 29 | \end{axis} 30 | 31 | \end{tikzpicture} 32 | -------------------------------------------------------------------------------- /tests/test_scatter_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{dodgerblue0143213}{RGB}{0,143,213} 4 | \definecolor{lightgray203}{RGB}{203,203,203} 5 | \definecolor{whitesmoke240}{RGB}{240,240,240} 6 | 7 | \begin{axis}[ 8 | axis background/.style={fill=whitesmoke240}, 9 | axis line style={whitesmoke240}, 10 | tick align=outside, 11 | tick pos=left, 12 | x grid style={lightgray203}, 13 | xmajorgrids, 14 | xmin=-5, xmax=105, 15 | xtick style={color=black}, 16 | y grid style={lightgray203}, 17 | ymajorgrids, 18 | ymin=0.17184841, ymax=112.81716, 19 | ytick style={color=black} 20 | ] 21 | \addplot [draw=dodgerblue0143213, fill=dodgerblue0143213, mark=*, only marks] 22 | table{% 23 | x y 24 | 0 10.447038 25 | 1 5.29209 26 | 2 5.4027718 27 | 3 11.269722 28 | 4 14.792035 29 | 5 11.346597 30 | 6 20.711463 31 | 7 17.272446 32 | 8 15.213979 33 | 9 14.881763 34 | 10 15.14767 35 | 11 21.935746 36 | 12 18.578584 37 | 13 13.895168 38 | 14 19.970664 39 | 15 26.069931 40 | 16 18.737376 41 | 17 19.631776 42 | 18 25.973271 43 | 19 26.977414 44 | 20 29.516014 45 | 21 33.741477 46 | 22 32.86683 47 | 23 32.165353 48 | 24 34.836651 49 | 25 29.844384 50 | 26 31.42683 51 | 27 30.423948 52 | 28 32.405711 53 | 29 38.464642 54 | 30 31.381574 55 | 31 37.505518 56 | 32 38.462941 57 | 33 40.405276 58 | 34 40.387454 59 | 35 39.683918 60 | 36 42.39527 61 | 37 50.400837 62 | 38 52.1624 63 | 39 46.52755 64 | 40 49.359294 65 | 41 42.734276 66 | 42 46.759282 67 | 43 49.222393 68 | 44 56.994637 69 | 45 48.75683 70 | 46 53.245514 71 | 47 61.783397 72 | 48 55.792277 73 | 49 58.193418 74 | 50 51.80943 75 | 51 63.395112 76 | 52 61.045902 77 | 53 61.17602 78 | 54 59.141458 79 | 55 59.561812 80 | 56 62.255333 81 | 57 67.219511 82 | 58 71.131853 83 | 59 66.656335 84 | 60 70.039707 85 | 61 69.789048 86 | 62 71.373553 87 | 63 73.120336 88 | 64 76.635137 89 | 65 66.247925 90 | 66 77.455243 91 | 67 70.654996 92 | 68 70.913344 93 | 69 77.586854 94 | 70 71.435688 95 | 71 84.279902 96 | 72 81.408735 97 | 73 83.851245 98 | 74 74.241938 99 | 75 83.916478 100 | 76 84.351778 101 | 77 79.384395 102 | 78 80.296058 103 | 79 89.432943 104 | 80 84.781496 105 | 81 91.379554 106 | 82 90.315749 107 | 83 88.834259 108 | 84 97.876987 109 | 85 97.62505 110 | 86 91.360964 111 | 87 87.653872 112 | 88 92.571521 113 | 89 94.972785 114 | 90 100.57438 115 | 91 105.93038 116 | 92 97.338723 117 | 93 104.43822 118 | 94 102.89765 119 | 95 105.37553 120 | 96 98.266912 121 | 97 102.98314 122 | 98 101.61284 123 | 99 104.15184 124 | 100 107.69692 125 | }; 126 | \end{axis} 127 | 128 | \end{tikzpicture} 129 | -------------------------------------------------------------------------------- /tests/test_sharex_and_y.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | fig, axes = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(8, 5)) 7 | t = np.arange(0.0, 5.0, 0.1) 8 | s = np.cos(2 * np.pi * t) 9 | axes[0][0].plot(t, s, color="blue") 10 | axes[0][1].plot(t, s, color="red") 11 | axes[1][0].plot(t, s, color="green") 12 | axes[1][1].plot(t, s, color="black") 13 | return fig 14 | 15 | 16 | def test(): 17 | from .helpers import assert_equality 18 | 19 | assert_equality(plot, __file__[:-3] + "_reference.tex") 20 | -------------------------------------------------------------------------------- /tests/test_sharex_and_y_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{green}{RGB}{0,128,0} 5 | 6 | \begin{groupplot}[group style={group size=2 by 2}] 7 | \nextgroupplot[ 8 | scaled x ticks=manual:{}{\pgfmathparse{#1}}, 9 | tick align=outside, 10 | tick pos=left, 11 | x grid style={darkgray176}, 12 | xmin=-0.245, xmax=5.145, 13 | xtick style={color=black}, 14 | xticklabels={}, 15 | y grid style={darkgray176}, 16 | ymin=-1.1, ymax=1.1, 17 | ytick style={color=black} 18 | ] 19 | \addplot [semithick, blue] 20 | table {% 21 | 0 1 22 | 0.1 0.80901699 23 | 0.2 0.30901699 24 | 0.3 -0.30901699 25 | 0.4 -0.80901699 26 | 0.5 -1 27 | 0.6 -0.80901699 28 | 0.7 -0.30901699 29 | 0.8 0.30901699 30 | 0.9 0.80901699 31 | 1 1 32 | 1.1 0.80901699 33 | 1.2 0.30901699 34 | 1.3 -0.30901699 35 | 1.4 -0.80901699 36 | 1.5 -1 37 | 1.6 -0.80901699 38 | 1.7 -0.30901699 39 | 1.8 0.30901699 40 | 1.9 0.80901699 41 | 2 1 42 | 2.1 0.80901699 43 | 2.2 0.30901699 44 | 2.3 -0.30901699 45 | 2.4 -0.80901699 46 | 2.5 -1 47 | 2.6 -0.80901699 48 | 2.7 -0.30901699 49 | 2.8 0.30901699 50 | 2.9 0.80901699 51 | 3 1 52 | 3.1 0.80901699 53 | 3.2 0.30901699 54 | 3.3 -0.30901699 55 | 3.4 -0.80901699 56 | 3.5 -1 57 | 3.6 -0.80901699 58 | 3.7 -0.30901699 59 | 3.8 0.30901699 60 | 3.9 0.80901699 61 | 4 1 62 | 4.1 0.80901699 63 | 4.2 0.30901699 64 | 4.3 -0.30901699 65 | 4.4 -0.80901699 66 | 4.5 -1 67 | 4.6 -0.80901699 68 | 4.7 -0.30901699 69 | 4.8 0.30901699 70 | 4.9 0.80901699 71 | }; 72 | 73 | \nextgroupplot[ 74 | scaled x ticks=manual:{}{\pgfmathparse{#1}}, 75 | scaled y ticks=manual:{}{\pgfmathparse{#1}}, 76 | tick align=outside, 77 | tick pos=left, 78 | x grid style={darkgray176}, 79 | xmin=-0.245, xmax=5.145, 80 | xtick style={color=black}, 81 | xticklabels={}, 82 | y grid style={darkgray176}, 83 | ymin=-1.1, ymax=1.1, 84 | ytick style={color=black}, 85 | yticklabels={} 86 | ] 87 | \addplot [semithick, red] 88 | table {% 89 | 0 1 90 | 0.1 0.80901699 91 | 0.2 0.30901699 92 | 0.3 -0.30901699 93 | 0.4 -0.80901699 94 | 0.5 -1 95 | 0.6 -0.80901699 96 | 0.7 -0.30901699 97 | 0.8 0.30901699 98 | 0.9 0.80901699 99 | 1 1 100 | 1.1 0.80901699 101 | 1.2 0.30901699 102 | 1.3 -0.30901699 103 | 1.4 -0.80901699 104 | 1.5 -1 105 | 1.6 -0.80901699 106 | 1.7 -0.30901699 107 | 1.8 0.30901699 108 | 1.9 0.80901699 109 | 2 1 110 | 2.1 0.80901699 111 | 2.2 0.30901699 112 | 2.3 -0.30901699 113 | 2.4 -0.80901699 114 | 2.5 -1 115 | 2.6 -0.80901699 116 | 2.7 -0.30901699 117 | 2.8 0.30901699 118 | 2.9 0.80901699 119 | 3 1 120 | 3.1 0.80901699 121 | 3.2 0.30901699 122 | 3.3 -0.30901699 123 | 3.4 -0.80901699 124 | 3.5 -1 125 | 3.6 -0.80901699 126 | 3.7 -0.30901699 127 | 3.8 0.30901699 128 | 3.9 0.80901699 129 | 4 1 130 | 4.1 0.80901699 131 | 4.2 0.30901699 132 | 4.3 -0.30901699 133 | 4.4 -0.80901699 134 | 4.5 -1 135 | 4.6 -0.80901699 136 | 4.7 -0.30901699 137 | 4.8 0.30901699 138 | 4.9 0.80901699 139 | }; 140 | 141 | \nextgroupplot[ 142 | tick align=outside, 143 | tick pos=left, 144 | x grid style={darkgray176}, 145 | xmin=-0.245, xmax=5.145, 146 | xtick style={color=black}, 147 | y grid style={darkgray176}, 148 | ymin=-1.1, ymax=1.1, 149 | ytick style={color=black} 150 | ] 151 | \addplot [semithick, green] 152 | table {% 153 | 0 1 154 | 0.1 0.80901699 155 | 0.2 0.30901699 156 | 0.3 -0.30901699 157 | 0.4 -0.80901699 158 | 0.5 -1 159 | 0.6 -0.80901699 160 | 0.7 -0.30901699 161 | 0.8 0.30901699 162 | 0.9 0.80901699 163 | 1 1 164 | 1.1 0.80901699 165 | 1.2 0.30901699 166 | 1.3 -0.30901699 167 | 1.4 -0.80901699 168 | 1.5 -1 169 | 1.6 -0.80901699 170 | 1.7 -0.30901699 171 | 1.8 0.30901699 172 | 1.9 0.80901699 173 | 2 1 174 | 2.1 0.80901699 175 | 2.2 0.30901699 176 | 2.3 -0.30901699 177 | 2.4 -0.80901699 178 | 2.5 -1 179 | 2.6 -0.80901699 180 | 2.7 -0.30901699 181 | 2.8 0.30901699 182 | 2.9 0.80901699 183 | 3 1 184 | 3.1 0.80901699 185 | 3.2 0.30901699 186 | 3.3 -0.30901699 187 | 3.4 -0.80901699 188 | 3.5 -1 189 | 3.6 -0.80901699 190 | 3.7 -0.30901699 191 | 3.8 0.30901699 192 | 3.9 0.80901699 193 | 4 1 194 | 4.1 0.80901699 195 | 4.2 0.30901699 196 | 4.3 -0.30901699 197 | 4.4 -0.80901699 198 | 4.5 -1 199 | 4.6 -0.80901699 200 | 4.7 -0.30901699 201 | 4.8 0.30901699 202 | 4.9 0.80901699 203 | }; 204 | 205 | \nextgroupplot[ 206 | scaled y ticks=manual:{}{\pgfmathparse{#1}}, 207 | tick align=outside, 208 | tick pos=left, 209 | x grid style={darkgray176}, 210 | xmin=-0.245, xmax=5.145, 211 | xtick style={color=black}, 212 | y grid style={darkgray176}, 213 | ymin=-1.1, ymax=1.1, 214 | ytick style={color=black}, 215 | yticklabels={} 216 | ] 217 | \addplot [semithick, black] 218 | table {% 219 | 0 1 220 | 0.1 0.80901699 221 | 0.2 0.30901699 222 | 0.3 -0.30901699 223 | 0.4 -0.80901699 224 | 0.5 -1 225 | 0.6 -0.80901699 226 | 0.7 -0.30901699 227 | 0.8 0.30901699 228 | 0.9 0.80901699 229 | 1 1 230 | 1.1 0.80901699 231 | 1.2 0.30901699 232 | 1.3 -0.30901699 233 | 1.4 -0.80901699 234 | 1.5 -1 235 | 1.6 -0.80901699 236 | 1.7 -0.30901699 237 | 1.8 0.30901699 238 | 1.9 0.80901699 239 | 2 1 240 | 2.1 0.80901699 241 | 2.2 0.30901699 242 | 2.3 -0.30901699 243 | 2.4 -0.80901699 244 | 2.5 -1 245 | 2.6 -0.80901699 246 | 2.7 -0.30901699 247 | 2.8 0.30901699 248 | 2.9 0.80901699 249 | 3 1 250 | 3.1 0.80901699 251 | 3.2 0.30901699 252 | 3.3 -0.30901699 253 | 3.4 -0.80901699 254 | 3.5 -1 255 | 3.6 -0.80901699 256 | 3.7 -0.30901699 257 | 3.8 0.30901699 258 | 3.9 0.80901699 259 | 4 1 260 | 4.1 0.80901699 261 | 4.2 0.30901699 262 | 4.3 -0.30901699 263 | 4.4 -0.80901699 264 | 4.5 -1 265 | 4.6 -0.80901699 266 | 4.7 -0.30901699 267 | 4.8 0.30901699 268 | 4.9 0.80901699 269 | }; 270 | \end{groupplot} 271 | 272 | \end{tikzpicture} 273 | -------------------------------------------------------------------------------- /tests/test_steps.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import matplotlib.pyplot as plt 3 | import numpy as np 4 | 5 | fig = plt.figure() 6 | x = np.arange(5) 7 | y1 = np.array([1, 2, 1, 4, 2]) 8 | y2 = np.array([1, 2, 1, 4, 2]) 9 | y3 = np.array([1, 2, 1, 4, 2]) 10 | y4 = np.array([1, 2, 1, 4, 2]) 11 | y5 = np.array([2, 3, 2, 5, 3]) 12 | 13 | plt.step(x, y1, "r-") 14 | plt.step(x, y2, "b--", where="pre") 15 | plt.step(x, y3, "g-.", where="post") 16 | plt.step(x, y4, "y:", where="mid") 17 | plt.plot(x, y5, "c.") 18 | plt.legend(["default", "pre", "post", "mid", "default"]) 19 | 20 | return fig 21 | 22 | 23 | def test(): 24 | from .helpers import assert_equality 25 | 26 | assert_equality(plot, __file__[:-3] + "_reference.tex") 27 | -------------------------------------------------------------------------------- /tests/test_steps_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{darkturquoise0191191}{RGB}{0,191,191} 5 | \definecolor{goldenrod1911910}{RGB}{191,191,0} 6 | \definecolor{green01270}{RGB}{0,127,0} 7 | \definecolor{lightgray204}{RGB}{204,204,204} 8 | 9 | \begin{axis}[ 10 | legend cell align={left}, 11 | legend style={ 12 | fill opacity=0.8, 13 | draw opacity=1, 14 | text opacity=1, 15 | at={(0.03,0.97)}, 16 | anchor=north west, 17 | draw=lightgray204 18 | }, 19 | tick align=outside, 20 | tick pos=left, 21 | x grid style={darkgray176}, 22 | xmin=-0.2, xmax=4.2, 23 | xtick style={color=black}, 24 | y grid style={darkgray176}, 25 | ymin=0.8, ymax=5.2, 26 | ytick style={color=black} 27 | ] 28 | \addplot [semithick, red, const plot mark right] 29 | table {% 30 | 0 1 31 | 1 2 32 | 2 1 33 | 3 4 34 | 4 2 35 | }; 36 | \addlegendentry{default} 37 | \addplot [semithick, blue, const plot mark right, dashed] 38 | table {% 39 | 0 1 40 | 1 2 41 | 2 1 42 | 3 4 43 | 4 2 44 | }; 45 | \addlegendentry{pre} 46 | \addplot [semithick, green01270, const plot mark left, dash pattern=on 1pt off 3pt on 3pt off 3pt] 47 | table {% 48 | 0 1 49 | 1 2 50 | 2 1 51 | 3 4 52 | 4 2 53 | }; 54 | \addlegendentry{post} 55 | \addplot [semithick, goldenrod1911910, const plot mark mid, dotted] 56 | table {% 57 | 0 1 58 | 1 2 59 | 2 1 60 | 3 4 61 | 4 2 62 | }; 63 | \addlegendentry{mid} 64 | \addplot [semithick, darkturquoise0191191, mark=*, mark size=3, mark options={solid}, only marks] 65 | table {% 66 | 0 2 67 | 1 3 68 | 2 2 69 | 3 5 70 | 4 3 71 | }; 72 | \addlegendentry{default} 73 | \end{axis} 74 | 75 | \end{tikzpicture} 76 | -------------------------------------------------------------------------------- /tests/test_subplot4x4.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | fig = plt.figure() 6 | 7 | an = np.linspace(0, 2 * np.pi, 10) 8 | 9 | plt.subplot(221) 10 | plt.plot(3 * np.cos(an), 3 * np.sin(an)) 11 | plt.title("not equal, looks like ellipse", fontsize=10) 12 | 13 | plt.subplot(222) 14 | plt.plot(3 * np.cos(an), 3 * np.sin(an)) 15 | plt.axis("equal") 16 | plt.title("equal, looks like circle", fontsize=10) 17 | 18 | plt.subplot(223) 19 | plt.plot(3 * np.cos(an), 3 * np.sin(an)) 20 | plt.axis("equal") 21 | plt.axis([-3, 3, -3, 3]) 22 | plt.title("looks like circle, even after changing limits", fontsize=10) 23 | 24 | plt.subplot(224) 25 | plt.plot(3 * np.cos(an), 3 * np.sin(an)) 26 | plt.axis("equal") 27 | plt.axis([-3, 3, -3, 3]) 28 | plt.plot([0, 4], [0, 4]) 29 | plt.title("still equal after adding line", fontsize=10) 30 | 31 | return fig 32 | 33 | 34 | def test(): 35 | from .helpers import assert_equality 36 | 37 | assert_equality(plot, __file__[:-3] + "_reference.tex") 38 | -------------------------------------------------------------------------------- /tests/test_subplot4x4_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{darkorange25512714}{RGB}{255,127,14} 5 | \definecolor{steelblue31119180}{RGB}{31,119,180} 6 | 7 | \begin{groupplot}[group style={group size=2 by 2}] 8 | \nextgroupplot[ 9 | tick align=outside, 10 | tick pos=left, 11 | title={not equal, looks like ellipse}, 12 | x grid style={darkgray176}, 13 | xmin=-3.1100318, xmax=3.2909539, 14 | xtick style={color=black}, 15 | y grid style={darkgray176}, 16 | ymin=-3.2498656, ymax=3.2498656, 17 | ytick style={color=black} 18 | ] 19 | \addplot [semithick, steelblue31119180] 20 | table {% 21 | 3 0 22 | 2.2981333 1.9283628 23 | 0.52094453 2.9544233 24 | -1.5 2.5980762 25 | -2.8190779 1.0260604 26 | -2.8190779 -1.0260604 27 | -1.5 -2.5980762 28 | 0.52094453 -2.9544233 29 | 2.2981333 -1.9283628 30 | 3 -7.3478808e-16 31 | }; 32 | 33 | \nextgroupplot[ 34 | tick align=outside, 35 | tick pos=left, 36 | title={equal, looks like circle}, 37 | x grid style={darkgray176}, 38 | xmin=-3.1100318, xmax=3.2909539, 39 | xtick style={color=black}, 40 | y grid style={darkgray176}, 41 | ymin=-3.2498656, ymax=3.2498656, 42 | ytick style={color=black} 43 | ] 44 | \addplot [semithick, steelblue31119180] 45 | table {% 46 | 3 0 47 | 2.2981333 1.9283628 48 | 0.52094453 2.9544233 49 | -1.5 2.5980762 50 | -2.8190779 1.0260604 51 | -2.8190779 -1.0260604 52 | -1.5 -2.5980762 53 | 0.52094453 -2.9544233 54 | 2.2981333 -1.9283628 55 | 3 -7.3478808e-16 56 | }; 57 | 58 | \nextgroupplot[ 59 | tick align=outside, 60 | tick pos=left, 61 | title={looks like circle, even after changing limits}, 62 | x grid style={darkgray176}, 63 | xmin=-3, xmax=3, 64 | xtick style={color=black}, 65 | y grid style={darkgray176}, 66 | ymin=-3, ymax=3, 67 | ytick style={color=black} 68 | ] 69 | \addplot [semithick, steelblue31119180] 70 | table {% 71 | 3 0 72 | 2.2981333 1.9283628 73 | 0.52094453 2.9544233 74 | -1.5 2.5980762 75 | -2.8190779 1.0260604 76 | -2.8190779 -1.0260604 77 | -1.5 -2.5980762 78 | 0.52094453 -2.9544233 79 | 2.2981333 -1.9283628 80 | 3 -7.3478808e-16 81 | }; 82 | 83 | \nextgroupplot[ 84 | tick align=outside, 85 | tick pos=left, 86 | title={still equal after adding line}, 87 | x grid style={darkgray176}, 88 | xmin=-3, xmax=3, 89 | xtick style={color=black}, 90 | y grid style={darkgray176}, 91 | ymin=-3, ymax=3, 92 | ytick style={color=black} 93 | ] 94 | \addplot [semithick, steelblue31119180] 95 | table {% 96 | 3 0 97 | 2.2981333 1.9283628 98 | 0.52094453 2.9544233 99 | -1.5 2.5980762 100 | -2.8190779 1.0260604 101 | -2.8190779 -1.0260604 102 | -1.5 -2.5980762 103 | 0.52094453 -2.9544233 104 | 2.2981333 -1.9283628 105 | 3 -7.3478808e-16 106 | }; 107 | \addplot [semithick, darkorange25512714] 108 | table {% 109 | 0 0 110 | 4 4 111 | }; 112 | \end{groupplot} 113 | 114 | \end{tikzpicture} 115 | -------------------------------------------------------------------------------- /tests/test_subplots.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | def f(t): 6 | s1 = np.cos(2 * np.pi * t) 7 | e1 = np.exp(-t) 8 | return np.multiply(s1, e1) 9 | 10 | fig = plt.figure() 11 | 12 | t1 = np.arange(0.0, 5.0, 0.4) 13 | t2 = np.arange(0.0, 5.0, 0.1) 14 | t3 = np.arange(0.0, 2.0, 0.1) 15 | 16 | plt.subplot(211) 17 | plt.plot(t1, f(t1), "bo", t2, f(t2), "k--", markerfacecolor="green") 18 | plt.grid(True) 19 | plt.title("A tale of 2 subplots") 20 | plt.ylabel("Damped oscillation") 21 | 22 | plt.subplot(212) 23 | plt.plot(t3, np.cos(2 * np.pi * t3), "r.") 24 | plt.grid(True) 25 | plt.xlabel("time (s)") 26 | plt.ylabel("Undamped") 27 | 28 | fig.suptitle("PLOT TITLE", fontsize=18, fontweight="bold") 29 | 30 | return fig 31 | 32 | 33 | def test(): 34 | from .helpers import assert_equality 35 | 36 | assert_equality(plot, __file__[:-3] + "_reference.tex") 37 | -------------------------------------------------------------------------------- /tests/test_subplots_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{green}{RGB}{0,128,0} 5 | 6 | \begin{groupplot}[group style={group size=1 by 2}] 7 | \nextgroupplot[ 8 | tick align=outside, 9 | tick pos=left, 10 | title={A tale of 2 subplots}, 11 | x grid style={darkgray176}, 12 | xmajorgrids, 13 | xmin=-0.245, xmax=5.145, 14 | xtick style={color=black}, 15 | y grid style={darkgray176}, 16 | ylabel={Damped oscillation}, 17 | ymajorgrids, 18 | ymin=-0.68685719, ymax=1.0803265, 19 | ytick style={color=black} 20 | ] 21 | \addplot [semithick, blue, mark=*, mark size=3, mark options={solid,fill=green}, only marks] 22 | table {% 23 | 0 1 24 | 0.4 -0.54230031 25 | 0.8 0.13885029 26 | 1.2 0.09307413 27 | 1.6 -0.16333771 28 | 2 0.13533528 29 | 2.4 -0.073392366 30 | 2.8 0.018791343 31 | 3.2 0.012596214 32 | 3.6 -0.022105356 33 | 4 0.018315639 34 | 4.4 -0.0099325766 35 | 4.8 0.0025431317 36 | }; 37 | \addplot [semithick, black, dashed] 38 | table {% 39 | 0 1 40 | 0.1 0.73202885 41 | 0.2 0.25300172 42 | 0.3 -0.22892542 43 | 0.4 -0.54230031 44 | 0.5 -0.60653066 45 | 0.6 -0.44399794 46 | 0.7 -0.1534533 47 | 0.8 0.13885029 48 | 0.9 0.32892176 49 | 1 0.36787944 50 | 1.1 0.26929836 51 | 1.2 0.09307413 52 | 1.3 -0.084216956 53 | 1.4 -0.19950113 54 | 1.5 -0.22313016 55 | 1.6 -0.16333771 56 | 1.7 -0.056452314 57 | 1.8 0.051080166 58 | 1.9 0.12100355 59 | 2 0.13533528 60 | 2.1 0.099069332 61 | 2.2 0.034240059 62 | 2.3 -0.030981687 63 | 2.4 -0.073392366 64 | 2.5 -0.082084999 65 | 2.6 -0.060088587 66 | 2.7 -0.020767646 67 | 2.8 0.018791343 68 | 2.9 0.04451472 69 | 3 0.049787068 70 | 3.1 0.03644557 71 | 3.2 0.012596214 72 | 3.3 -0.011397526 73 | 3.4 -0.026999543 74 | 3.5 -0.030197383 75 | 3.6 -0.022105356 76 | 3.7 -0.0076399898 77 | 3.8 0.0069129487 78 | 3.9 0.01637605 79 | 4 0.018315639 80 | 4.1 0.013407576 81 | 4.2 0.0046338881 82 | 4.3 -0.0041929153 83 | 4.4 -0.0099325766 84 | 4.5 -0.011108997 85 | 4.6 -0.0081321059 86 | 4.7 -0.0028105952 87 | 4.8 0.0025431317 88 | 4.9 0.0060244123 89 | }; 90 | 91 | \nextgroupplot[ 92 | tick align=outside, 93 | tick pos=left, 94 | x grid style={darkgray176}, 95 | xlabel={time (s)}, 96 | xmajorgrids, 97 | xmin=-0.095, xmax=1.995, 98 | xtick style={color=black}, 99 | y grid style={darkgray176}, 100 | ylabel={Undamped}, 101 | ymajorgrids, 102 | ymin=-1.1, ymax=1.1, 103 | ytick style={color=black} 104 | ] 105 | \addplot [semithick, red, mark=*, mark size=3, mark options={solid}, only marks] 106 | table {% 107 | 0 1 108 | 0.1 0.80901699 109 | 0.2 0.30901699 110 | 0.3 -0.30901699 111 | 0.4 -0.80901699 112 | 0.5 -1 113 | 0.6 -0.80901699 114 | 0.7 -0.30901699 115 | 0.8 0.30901699 116 | 0.9 0.80901699 117 | 1 1 118 | 1.1 0.80901699 119 | 1.2 0.30901699 120 | 1.3 -0.30901699 121 | 1.4 -0.80901699 122 | 1.5 -1 123 | 1.6 -0.80901699 124 | 1.7 -0.30901699 125 | 1.8 0.30901699 126 | 1.9 0.80901699 127 | }; 128 | \end{groupplot} 129 | 130 | \draw ({$(current bounding box.south west)!0.5!(current bounding box.south east)$}|-{$(current bounding box.south west)!0.98!(current bounding box.north west)$}) node[ 131 | scale=0.9, 132 | anchor=north, 133 | text=black, 134 | rotate=0.0 135 | ]{\bfseries PLOT TITLE}; 136 | \end{tikzpicture} 137 | -------------------------------------------------------------------------------- /tests/test_subplots_with_colorbars.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | 4 | def plot(): 5 | import numpy as np 6 | from matplotlib import pyplot as plt 7 | 8 | data = np.zeros((3, 3)) 9 | data[:2, :2] = 1.0 10 | 11 | fig = plt.figure() 12 | 13 | ax1 = plt.subplot(131) 14 | ax2 = plt.subplot(132) 15 | ax3 = plt.subplot(133) 16 | axes = [ax1, ax2, ax3] 17 | 18 | for ax in axes: 19 | im = ax.imshow(data) 20 | fig.colorbar(im, ax=ax, orientation="horizontal") 21 | 22 | return fig 23 | 24 | 25 | # TODO reintroduce 26 | @pytest.mark.skip("Fails?") 27 | def test(): 28 | from .helpers import assert_equality 29 | 30 | assert_equality(plot, __file__[:-3] + "_reference.tex") 31 | -------------------------------------------------------------------------------- /tests/test_subplots_with_colorbars_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{groupplot}[group style={group size=3 by 1}] 6 | \nextgroupplot[ 7 | colorbar horizontal, 8 | colormap/viridis, 9 | point meta max=1, 10 | point meta min=0, 11 | tick align=outside, 12 | tick pos=left, 13 | x grid style={darkgray176}, 14 | xmin=-0.5, xmax=2.5, 15 | xtick style={color=black}, 16 | y dir=reverse, 17 | y grid style={darkgray176}, 18 | ymin=-0.5, ymax=2.5, 19 | ytick style={color=black} 20 | ] 21 | \addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=2.5, ymin=2.5, ymax=-0.5] {tmp-000.png}; 22 | 23 | \nextgroupplot[ 24 | colorbar horizontal, 25 | colormap/viridis, 26 | point meta max=1, 27 | point meta min=0, 28 | tick align=outside, 29 | tick pos=left, 30 | x grid style={darkgray176}, 31 | xmin=-0.5, xmax=2.5, 32 | xtick style={color=black}, 33 | y dir=reverse, 34 | y grid style={darkgray176}, 35 | ymin=-0.5, ymax=2.5, 36 | ytick style={color=black} 37 | ] 38 | \addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=2.5, ymin=2.5, ymax=-0.5] {tmp-001.png}; 39 | 40 | \nextgroupplot[ 41 | colorbar horizontal, 42 | colormap/viridis, 43 | point meta max=1, 44 | point meta min=0, 45 | tick align=outside, 46 | tick pos=left, 47 | x grid style={darkgray176}, 48 | xmin=-0.5, xmax=2.5, 49 | xtick style={color=black}, 50 | y dir=reverse, 51 | y grid style={darkgray176}, 52 | ymin=-0.5, ymax=2.5, 53 | ytick style={color=black} 54 | ] 55 | \addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=2.5, ymin=2.5, ymax=-0.5] {tmp-002.png}; 56 | \end{groupplot} 57 | 58 | \end{tikzpicture} 59 | -------------------------------------------------------------------------------- /tests/test_text_overlay.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | 5 | def plot(): 6 | fig = plt.figure() 7 | 8 | xxx = np.linspace(0, 5) 9 | yyy = xxx**2 10 | plt.text( 11 | 1, 12 | 5, 13 | "test1", 14 | size=50, 15 | rotation=30.0, 16 | ha="center", 17 | va="bottom", 18 | color="r", 19 | style="italic", 20 | weight="light", 21 | bbox=dict( 22 | boxstyle="round, pad=0.2", 23 | ec=(1.0, 0.5, 0.5), 24 | fc=(1.0, 0.8, 0.8), 25 | ls="dashdot", 26 | ), 27 | ) 28 | plt.text( 29 | 3, 30 | 6, 31 | "test2", 32 | size=50, 33 | rotation=-30.0, 34 | ha="center", 35 | va="center", 36 | color="b", 37 | weight="bold", 38 | bbox=dict(boxstyle="square", ec=(1.0, 0.5, 0.5), fc=(1.0, 0.8, 0.8)), 39 | ) 40 | plt.text( 41 | 4, 42 | 8, 43 | "test3", 44 | size=20, 45 | rotation=90.0, 46 | ha="center", 47 | va="center", 48 | color="b", 49 | weight="demi", 50 | bbox=dict( 51 | boxstyle="rarrow", ls="dashed", ec=(1.0, 0.5, 0.5), fc=(1.0, 0.8, 0.8) 52 | ), 53 | ) 54 | plt.text( 55 | 4, 56 | 16, 57 | "test4", 58 | size=20, 59 | rotation=90.0, 60 | ha="center", 61 | va="center", 62 | color="b", 63 | weight="heavy", 64 | bbox=dict( 65 | boxstyle="larrow", ls="dotted", ec=(1.0, 0.5, 0.5), fc=(1.0, 0.8, 0.8) 66 | ), 67 | ) 68 | plt.text( 69 | 2, 70 | 18, 71 | "test5", 72 | size=20, 73 | ha="center", 74 | va="center", 75 | color="b", 76 | bbox=dict(boxstyle="darrow", ec=(1.0, 0.5, 0.5), fc=(1.0, 0.8, 0.8)), 77 | ) 78 | plt.text( 79 | 1, 80 | 20, 81 | "test6", 82 | size=20, 83 | ha="center", 84 | va="center", 85 | color="b", 86 | bbox=dict(boxstyle="circle", ec=(1.0, 0.5, 0.5), fc=(1.0, 0.8, 0.8)), 87 | ) 88 | plt.text( 89 | 3, 90 | 23, 91 | "test7", 92 | size=20, 93 | ha="center", 94 | va="center", 95 | color="b", 96 | bbox=dict(boxstyle="roundtooth", ec=(1.0, 0.5, 0.5), fc=(1.0, 0.8, 0.8)), 97 | ) 98 | plt.text( 99 | 3, 100 | 20, 101 | "test8", 102 | size=20, 103 | ha="center", 104 | va="center", 105 | color="b", 106 | bbox=dict(boxstyle="sawtooth", ec=(1.0, 0.5, 0.5), fc=(1.0, 0.8, 0.8)), 107 | ) 108 | plt.plot(xxx, yyy, label="a graph") 109 | plt.legend() 110 | 111 | return fig 112 | 113 | 114 | def test(): 115 | from .helpers import assert_equality 116 | 117 | assert_equality(plot, __file__[:-3] + "_reference.tex") 118 | -------------------------------------------------------------------------------- /tests/test_text_overlay_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | \definecolor{lightgray204}{RGB}{204,204,204} 5 | \definecolor{pink255204204}{RGB}{255,204,204} 6 | \definecolor{salmon255127127}{RGB}{255,127,127} 7 | \definecolor{steelblue31119180}{RGB}{31,119,180} 8 | 9 | \begin{axis}[ 10 | legend cell align={left}, 11 | legend style={ 12 | fill opacity=0.8, 13 | draw opacity=1, 14 | text opacity=1, 15 | at={(0.03,0.97)}, 16 | anchor=north west, 17 | draw=lightgray204 18 | }, 19 | tick align=outside, 20 | tick pos=left, 21 | x grid style={darkgray176}, 22 | xmin=-0.25, xmax=5.25, 23 | xtick style={color=black}, 24 | y grid style={darkgray176}, 25 | ymin=-1.25, ymax=26.25, 26 | ytick style={color=black} 27 | ] 28 | \addplot [semithick, steelblue31119180] 29 | table {% 30 | 0 0 31 | 0.10204082 0.010412328 32 | 0.20408163 0.041649313 33 | 0.30612245 0.093710954 34 | 0.40816327 0.16659725 35 | 0.51020408 0.2603082 36 | 0.6122449 0.37484382 37 | 0.71428571 0.51020408 38 | 0.81632653 0.666389 39 | 0.91836735 0.84339858 40 | 1.0204082 1.0412328 41 | 1.122449 1.2598917 42 | 1.2244898 1.4993753 43 | 1.3265306 1.7596835 44 | 1.4285714 2.0408163 45 | 1.5306122 2.3427738 46 | 1.6326531 2.665556 47 | 1.7346939 3.0091628 48 | 1.8367347 3.3735943 49 | 1.9387755 3.7588505 50 | 2.0408163 4.1649313 51 | 2.1428571 4.5918367 52 | 2.244898 5.0395668 53 | 2.3469388 5.5081216 54 | 2.4489796 5.997501 55 | 2.5510204 6.5077051 56 | 2.6530612 7.0387339 57 | 2.755102 7.5905873 58 | 2.8571429 8.1632653 59 | 2.9591837 8.756768 60 | 3.0612245 9.3710954 61 | 3.1632653 10.006247 62 | 3.2653061 10.662224 63 | 3.3673469 11.339025 64 | 3.4693878 12.036651 65 | 3.5714286 12.755102 66 | 3.6734694 13.494377 67 | 3.7755102 14.254477 68 | 3.877551 15.035402 69 | 3.9795918 15.837151 70 | 4.0816327 16.659725 71 | 4.1836735 17.503124 72 | 4.2857143 18.367347 73 | 4.3877551 19.252395 74 | 4.4897959 20.158267 75 | 4.5918367 21.084965 76 | 4.6938776 22.032486 77 | 4.7959184 23.000833 78 | 4.8979592 23.990004 79 | 5 25 80 | }; 81 | \addlegendentry{a graph} 82 | \draw (axis cs:1,5) node[ 83 | scale=2.5, 84 | fill=pink255204204, 85 | draw=salmon255127127, 86 | line width=0.4pt, 87 | inner sep=2pt, 88 | rounded corners, 89 | dash pattern=on 0.4pt off 1.2pt on 2.4pt off 1.2pt, 90 | anchor=south, 91 | text=red, 92 | rotate=30.0 93 | ]{\itshape test1}; 94 | \draw (axis cs:3,6) node[ 95 | scale=2.5, 96 | fill=pink255204204, 97 | draw=salmon255127127, 98 | line width=0.4pt, 99 | inner sep=3pt, 100 | text=blue, 101 | rotate=330.0 102 | ]{\bfseries test2}; 103 | \draw (axis cs:4,8) node[ 104 | fill=pink255204204, 105 | draw=salmon255127127, 106 | line width=0.4pt, 107 | inner sep=3pt, 108 | single arrow, 109 | dashed, 110 | text=blue, 111 | rotate=90.0 112 | ]{\bfseries test3}; 113 | \draw (axis cs:4,16) node[ 114 | fill=pink255204204, 115 | draw=salmon255127127, 116 | line width=0.4pt, 117 | inner sep=3pt, 118 | single arrow, 119 | shape border rotate=180, 120 | dotted, 121 | text=blue, 122 | rotate=90.0 123 | ]{\bfseries test4}; 124 | \draw (axis cs:2,18) node[ 125 | fill=pink255204204, 126 | draw=salmon255127127, 127 | line width=0.4pt, 128 | inner sep=3pt, 129 | double arrow, 130 | text=blue, 131 | rotate=0.0 132 | ]{test5}; 133 | \draw (axis cs:1,20) node[ 134 | fill=pink255204204, 135 | draw=salmon255127127, 136 | line width=0.4pt, 137 | inner sep=3pt, 138 | circle, 139 | text=blue, 140 | rotate=0.0 141 | ]{test6}; 142 | \draw (axis cs:3,23) node[ 143 | fill=pink255204204, 144 | draw=salmon255127127, 145 | line width=0.4pt, 146 | inner sep=3pt, 147 | decorate, 148 | decoration={snake,amplitude=0.5,segment length=3}, 149 | text=blue, 150 | rotate=0.0 151 | ]{test7}; 152 | \draw (axis cs:3,20) node[ 153 | fill=pink255204204, 154 | draw=salmon255127127, 155 | line width=0.4pt, 156 | inner sep=3pt, 157 | decorate, 158 | decoration={zigzag,amplitude=0.5,segment length=3}, 159 | text=blue, 160 | rotate=0.0 161 | ]{test8}; 162 | \end{axis} 163 | 164 | \end{tikzpicture} 165 | -------------------------------------------------------------------------------- /tests/test_tick_positions.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | from matplotlib import pyplot as plt 3 | 4 | x = [1, 2, 3, 4] 5 | y = [1, 4, 9, 6] 6 | 7 | fig = plt.figure() 8 | 9 | ax = plt.subplot(4, 4, 1) 10 | plt.plot(x, y, "ro") 11 | plt.tick_params(axis="x", which="both", bottom="off", top="off") 12 | plt.tick_params(axis="y", which="both", left="off", right="off") 13 | 14 | ax = plt.subplot(4, 4, 2) 15 | plt.plot(x, y, "ro") 16 | plt.tick_params(axis="x", which="both", bottom="off", top="off") 17 | plt.tick_params(axis="y", which="both", left="off", right="on") 18 | 19 | ax = plt.subplot(4, 4, 3) 20 | plt.plot(x, y, "ro") 21 | plt.tick_params(axis="x", which="both", bottom="off", top="off") 22 | plt.tick_params(axis="y", which="both", left="on", right="off") 23 | 24 | ax = plt.subplot(4, 4, 4) 25 | ax.plot(x, y, "ro") 26 | plt.tick_params(axis="x", which="both", bottom="off", top="off") 27 | plt.tick_params(axis="y", which="both", left="on", right="on") 28 | 29 | ax = plt.subplot(4, 4, 5) 30 | ax.plot(x, y, "ro") 31 | plt.tick_params(axis="x", which="both", bottom="off", top="on") 32 | plt.tick_params(axis="y", which="both", left="off", right="off") 33 | 34 | ax = plt.subplot(4, 4, 6) 35 | plt.plot(x, y, "ro") 36 | plt.tick_params(axis="x", which="both", bottom="off", top="on") 37 | plt.tick_params(axis="y", which="both", left="off", right="on") 38 | 39 | ax = plt.subplot(4, 4, 7) 40 | plt.plot(x, y, "ro") 41 | plt.tick_params(axis="x", which="both", bottom="off", top="on") 42 | plt.tick_params(axis="y", which="both", left="on", right="off") 43 | 44 | ax = plt.subplot(4, 4, 8) 45 | ax.plot(x, y, "ro") 46 | plt.tick_params(axis="x", which="both", bottom="off", top="on") 47 | plt.tick_params(axis="y", which="both", left="on", right="on") 48 | 49 | ax = plt.subplot(4, 4, 9) 50 | ax.plot(x, y, "ro") 51 | plt.tick_params(axis="x", which="both", bottom="on", top="off") 52 | plt.tick_params(axis="y", which="both", left="off", right="off") 53 | 54 | ax = plt.subplot(4, 4, 10) 55 | plt.plot(x, y, "ro") 56 | plt.tick_params(axis="x", which="both", bottom="on", top="off") 57 | plt.tick_params(axis="y", which="both", left="off", right="on") 58 | 59 | ax = plt.subplot(4, 4, 11) 60 | plt.plot(x, y, "ro") 61 | plt.tick_params(axis="x", which="both", bottom="on", top="off") 62 | plt.tick_params(axis="y", which="both", left="on", right="off") 63 | 64 | ax = plt.subplot(4, 4, 12) 65 | ax.plot(x, y, "ro") 66 | plt.tick_params(axis="x", which="both", bottom="on", top="off") 67 | plt.tick_params(axis="y", which="both", left="on", right="on") 68 | 69 | ax = plt.subplot(4, 4, 13) 70 | ax.plot(x, y, "ro") 71 | plt.tick_params(axis="x", which="both", bottom="on", top="on") 72 | plt.tick_params(axis="y", which="both", left="off", right="off") 73 | 74 | ax = plt.subplot(4, 4, 14) 75 | ax.plot(x, y, "ro") 76 | plt.tick_params(axis="x", which="both", bottom="on", top="on") 77 | plt.tick_params(axis="y", which="both", left="off", right="on") 78 | 79 | ax = plt.subplot(4, 4, 15) 80 | ax.plot(x, y, "ro") 81 | plt.tick_params(axis="x", which="both", bottom="on", top="on") 82 | plt.tick_params(axis="y", which="both", left="on", right="off") 83 | 84 | ax = plt.subplot(4, 4, 16) 85 | ax.plot(x, y, "ro") 86 | plt.tick_params(axis="x", which="both", bottom="on", top="on") 87 | plt.tick_params(axis="y", which="both", left="on", right="on") 88 | 89 | return fig 90 | 91 | 92 | def test(): 93 | from .helpers import assert_equality 94 | 95 | assert_equality(plot, __file__[:-3] + "_reference.tex") 96 | -------------------------------------------------------------------------------- /tests/test_viridis.py: -------------------------------------------------------------------------------- 1 | def plot(): 2 | import matplotlib.cm as cm 3 | import matplotlib.pyplot as plt 4 | import numpy as np 5 | 6 | x, y = np.meshgrid(np.linspace(0, 1), np.linspace(0, 1)) 7 | z = x**2 - y**2 8 | 9 | fig = plt.figure() 10 | plt.pcolormesh(x, y, z, cmap=cm.viridis, shading="gouraud") 11 | # plt.colorbar() 12 | 13 | return fig 14 | 15 | 16 | def test(): 17 | from .helpers import assert_equality 18 | 19 | # test relative data path 20 | assert_equality( 21 | plot, 22 | __file__[:-3] + "_reference.tex", 23 | # tex_relative_path_to_data="data/files" 24 | ) 25 | -------------------------------------------------------------------------------- /tests/test_viridis_reference.tex: -------------------------------------------------------------------------------- 1 | \begin{tikzpicture} 2 | 3 | \definecolor{darkgray176}{RGB}{176,176,176} 4 | 5 | \begin{axis}[ 6 | tick align=outside, 7 | tick pos=left, 8 | x grid style={darkgray176}, 9 | xmin=0, xmax=1, 10 | xtick style={color=black}, 11 | y grid style={darkgray176}, 12 | ymin=0, ymax=1, 13 | ytick style={color=black} 14 | ] 15 | \addplot graphics [includegraphics cmd=\pgfimage,xmin=0, xmax=1, ymin=0, ymax=1] {tmp-000.png}; 16 | \end{axis} 17 | 18 | \end{tikzpicture} 19 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py3 3 | isolated_build = True 4 | 5 | [testenv] 6 | deps = 7 | pandas 8 | pytest 9 | pytest-cov 10 | pytest-codeblocks 11 | matplotlib == 3.5.1 12 | pytest-randomly 13 | commands = 14 | pytest {posargs} --codeblocks 15 | --------------------------------------------------------------------------------