Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ result_images
.pytest_cache
dist
*.egg-info
docs/_build/
docs/api/generated
docs/examples/
112 changes: 5 additions & 107 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,118 +12,16 @@ You can install matplotview using pip:
pip install matplotview
```

## Usage

matplotview provides two methods, `view`, and `inset_zoom_axes`. The `view`
method accepts two `Axes`, and makes the first axes a view of the second. The
`inset_zoom_axes` method provides the same functionality as `Axes.inset_axes`,
but the returned inset axes is configured to be a view of the parent axes.

## Examples

An example of two axes showing the same plot.
```python
from matplotview import view
import matplotlib.pyplot as plt
import numpy as np

fig, (ax1, ax2) = plt.subplots(1, 2)

# Plot a line, circle patch, some text, and an image...
ax1.plot([i for i in range(10)], "r")
ax1.add_patch(plt.Circle((3, 3), 1, ec="black", fc="blue"))
ax1.text(10, 10, "Hello World!", size=20)
ax1.imshow(np.random.rand(30, 30), origin="lower", cmap="Blues", alpha=0.5,
interpolation="nearest")

# Turn axes 2 into a view of axes 1.
view(ax2, ax1)
# Modify the second axes data limits to match the first axes...
ax2.set_aspect(ax1.get_aspect())
ax2.set_xlim(ax1.get_xlim())
ax2.set_ylim(ax1.get_ylim())

fig.tight_layout()
fig.show()
```
![First example plot results, two views of the same plot.](https://user-images.githubusercontent.com/47544550/149814592-dd815f95-c3ef-406d-bd7e-504859c836bf.png)

An inset axes example.
```python
from matplotlib import cbook
import matplotlib.pyplot as plt
import numpy as np
from matplotview import inset_zoom_axes

def get_demo_image():
z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
# z is a numpy array of 15x15
return z, (-3, 4, -4, 3)

fig, ax = plt.subplots(figsize=[5, 4])
Examples can be found in the example gallery:

# Make the data...
Z, extent = get_demo_image()
Z2 = np.zeros((150, 150))
ny, nx = Z.shape
Z2[30:30+ny, 30:30+nx] = Z
[https://matplotview.readthedocs.io/en/latest/examples/index.html](https://matplotview.readthedocs.io/en/latest/examples/index.html)

ax.imshow(Z2, extent=extent, interpolation='nearest', origin="lower")
## Documentation

# Creates an inset axes with automatic view of the parent axes...
axins = inset_zoom_axes(ax, [0.5, 0.5, 0.47, 0.47])
# Set limits to sub region of the original image
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
axins.set_xticklabels([])
axins.set_yticklabels([])
Additional documentation can be found at the link below:

ax.indicate_inset_zoom(axins, edgecolor="black")
[https://matplotview.readthedocs.io/en/latest/](https://matplotview.readthedocs.io/en/latest/)

fig.show()
```
![Second example plot results, an inset axes showing a zoom view of an image.](https://user-images.githubusercontent.com/47544550/149814558-c2b1228d-2e5d-41be-86c0-f5dd01d42884.png)

Because views support recursive drawing, they can be used to create
fractals also.
```python
import matplotlib.pyplot as plt
import matplotview as mpv
from matplotlib.patches import PathPatch
from matplotlib.path import Path
from matplotlib.transforms import Affine2D

outside_color = "black"
inner_color = "white"

t = Affine2D().scale(-0.5)

outer_triangle = Path.unit_regular_polygon(3)
inner_triangle = t.transform_path(outer_triangle)
b = outer_triangle.get_extents()

fig, ax = plt.subplots(1)
ax.set_aspect(1)

ax.add_patch(PathPatch(outer_triangle, fc=outside_color, ec=[0] * 4))
ax.add_patch(PathPatch(inner_triangle, fc=inner_color, ec=[0] * 4))
ax.set_xlim(b.x0, b.x1)
ax.set_ylim(b.y0, b.y1)

ax_locs = [
[0, 0, 0.5, 0.5],
[0.5, 0, 0.5, 0.5],
[0.25, 0.5, 0.5, 0.5]
]

for loc in ax_locs:
inax = mpv.inset_zoom_axes(ax, loc, render_depth=6)
inax.set_xlim(b.x0, b.x1)
inax.set_ylim(b.y0, b.y1)
inax.axis("off")
inax.patch.set_visible(False)

fig.show()
```
![Third example plot results, a Sierpiński triangle](https://user-images.githubusercontent.com/47544550/150047401-e9364f0f-becd-45c5-a6f4-062118ce713f.png)
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
20 changes: 20 additions & 0 deletions docs/_static/gallery_mods.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

.sphx-glr-thumbcontainer[tooltip]:hover:after {
background: var(--sg-tooltip-background);
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
color: var(--sg-tooltip-foreground);
content: "";
opacity: 0.35;
padding: 10px;
z-index: 98;
width: 100%;
height: 100%;
position: absolute;
pointer-events: none;
top: 0;
box-sizing: border-box;
overflow: hidden;
backdrop-filter: blur(3px);
}
13 changes: 13 additions & 0 deletions docs/api/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
API
===

The public facing functions of matplotview.

.. autosummary::
:toctree: generated

matplotview.view
matplotview.stop_viewing
matplotview.inset_zoom_axes


47 changes: 47 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from pathlib import Path
import sys

# Add project root directory to python path...
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))


project = 'matplotview'
copyright = '2022, Isaac Robinson'
author = 'Isaac Robinson'
release = '1.0.0'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.autosummary',
'sphinx.ext.doctest',
'sphinx.ext.intersphinx',
'sphinx.ext.viewcode',
'numpydoc',
'matplotlib.sphinxext.mathmpl',
'matplotlib.sphinxext.plot_directive',
'sphinx_gallery.gen_gallery'
]

templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

from sphinx_gallery.sorting import FileNameSortKey

sphinx_gallery_conf = {
"examples_dirs": "../examples",
"gallery_dirs": "examples",
"line_numbers": True,
"within_subsection_order": FileNameSortKey
}

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'alabaster'
html_static_path = ['_static']
html_css_files = ['gallery_mods.css']

plot_include_source = True
22 changes: 22 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.. matplotview documentation master file, created by
sphinx-quickstart on Sat Aug 13 19:55:28 2022.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.

Matplotview |release| Documentation
===================================

.. toctree::
:maxdepth: 2
:caption: Contents:

installation
examples/index
api/index


Additional Links
================

* :ref:`genindex`
* :ref:`search`
9 changes: 9 additions & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Installation
============

Matplotview can be installed using `pip <https://pypi.org/project/matplotview>`__:

.. code-block:: bash

pip install matplotview

35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
7 changes: 7 additions & 0 deletions examples/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Examples
========

Because of the way matplotview is designed, it can work with any Axes and projection
types, and works with all the default projection modes included in matplotlib.
The following examples showcase using matplotview in several different scenarios and
with different projections.
23 changes: 23 additions & 0 deletions examples/plot_00_simplest_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
The Simplest View
=================

The simplest example: We make a view of a line! Views can be created quickly
using :meth:`matplotview.view` .
"""

from matplotview import view
import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2)

# Plot a line in the first axes.
ax1.plot([i for i in range(10)], "-o")

# Create a view! Turn axes 2 into a view of axes 1.
view(ax2, ax1)
# Modify the second axes data limits so we get a slightly zoomed out view
ax2.set_xlim(-5, 15)
ax2.set_ylim(-5, 15)

fig.show()
29 changes: 29 additions & 0 deletions examples/plot_01_multiple_artist_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
A View With Several Plot Elements
=================================

A simple example with an assortment of plot elements.
"""

from matplotview import view
import matplotlib.pyplot as plt
import numpy as np

fig, (ax1, ax2) = plt.subplots(1, 2)

# Plot a line, circle patch, some text, and an image...
ax1.plot([i for i in range(10)], "r")
ax1.add_patch(plt.Circle((3, 3), 1, ec="black", fc="blue"))
ax1.text(10, 10, "Hello World!", size=20)
ax1.imshow(np.random.rand(30, 30), origin="lower", cmap="Blues", alpha=0.5,
interpolation="nearest")

# Turn axes 2 into a view of axes 1.
view(ax2, ax1)
# Modify the second axes data limits to match the first axes...
ax2.set_aspect(ax1.get_aspect())
ax2.set_xlim(ax1.get_xlim())
ax2.set_ylim(ax1.get_ylim())

fig.tight_layout()
fig.show()
Loading