Drawing arrows for matplotlib made easy.
Project description
drawarrow
Drawing arrows for matplotlib made easy.
Table of content:
Installation
You can install drawarrow
directly from PyPI with:
pip install drawarrow
Alternatively you can install the development version with:
pip install git+https://github.com/JosephBARBIERDARNAL/drawarrow.git
Quick Start
import matplotlib.pyplot as plt
from drawarrow import fig_arrow
fig, ax = plt.subplots()
ax.scatter(x=[1, 2, 3, 4, 5], y=[1, 2, 3, 4, 5], s=100)
fig_arrow(head_position=(0.5, 0.5), tail_position=(0.2, 0.7), fig=fig, color="r")
plt.show()
Usage guide
drawarrow
provides 2 functions:
fig_arrow()
: draw an arrow on a Matplotlib figure (e.g. the whole chart)ax_arrow()
: draw an arrow on Matplotlib axes (e.g. a subplot)
The difference between those functions mainly lies in their coordinates. An arrow on a Figure would usually be between 0 and 1 for both x and y axis. However, when drawing an arrow on an Axes, it's on the data coordinates.
Those functions have a set a common arguments:
tail_position
(array-like of length 2): position of the tail of the arrow (on the figure/axes)head_position
(array-like of length 2): position of the head of the arrow (on the figure/axes)invert
(bool, default to False): whether to invert or not the angle of the arrow (only used ifradius
!=0)radius
(float, default to 0.1):tail_width
(float, default to 0.5): Width of the tail of the arrowhead_width
(float, default to 4): Head width of the tail of the arrowhead_length
(float, default to 8): Head length of the tail of the arrowlinewidth
(float, default to 0.5): Width of the body of the arrowcolor
: color of the arrow,
ax_arrow()
This function is recommended when what you want to highlight is related to the data itself, such as a specific point in a scatter plot.
Minimalist usage
import matplotlib.pyplot as plt
from drawarrow import ax_arrow
fig, ax = plt.subplots()
ax.scatter(x=[1, 5, 10], y=[5, 12, 3])
ax_arrow((2, 8), (8, 2), ax=ax, color="blue", radius=0.3)
plt.show()
Usage with multiple subplots
import matplotlib.pyplot as plt
from drawarrow import ax_arrow
fig, axs = plt.subplots(ncols=3, nrows=3)
for ax in axs.flat:
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_xticks([])
ax.set_yticks([])
ax_arrow((2, 2), (8, 8), ax=ax, color="red", radius=0.4)
plt.show()
fig_arrow()
Minimalist usage
This function is recommended when what you want to highlight something that is more global and not data specific.
import matplotlib.pyplot as plt
from drawarrow import fig_arrow
fig, ax = plt.subplots()
fig_arrow((0.3, 0.3), (0.8, 0.8), fig=fig)
plt.show()
More complex usage
import matplotlib.pyplot as plt
from drawarrow import fig_arrow
fig, ax = plt.subplots()
fig_arrow(
(0.3, 0.3),
(0.8, 0.8),
color="#2a9d8f",
tail_width=2,
head_length=20,
head_width=10,
linewidth=2,
radius=0.7,
invert=True,
fig=fig,
)
plt.show()
Tips and tricks
Straight arrow
To draw a straight arrow, you just have to pass radius=0
:
import matplotlib.pyplot as plt
from drawarrow import fig_arrow
fig, ax = plt.subplots()
fig_arrow((0.3, 0.3), (0.8, 0.8), radius=0, fig=fig)
plt.show()
Back and forth arrow
radius
has no real bounds in terms of possible values:
import matplotlib.pyplot as plt
from drawarrow import fig_arrow
fig, ax = plt.subplots()
fig_arrow((0.3, 0.3), (0.8, 0.8), radius=2, fig=fig)
plt.show()
Contributions
Contributions (and feedback) are welcome.
TODO features:
- control opacity (alpha parameter in matplotlib)
- control zorder
- draw arrow with an inflection point (see this)
- ...
Installation for contributions
Note: the following steps are for Mac, but not very different in other OS.
- Fork this repo
git clone https://github.com/yourusername/drawarrow.git
cd drawarrow
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip install -e .
git checkout -b feature-name
- start coding!
Acknowledgement
Thanks pizzadude for the font used in the logo.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file drawarrow-0.0.2.tar.gz
.
File metadata
- Download URL: drawarrow-0.0.2.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6adedc7814aa01abd9a9a8a534df6f80f07cd3c9ca70fde014211433f9684754 |
|
MD5 | 71a436d258835239b46216b0a87ef524 |
|
BLAKE2b-256 | 333d3985c0b27a3715d71ad84fdb48de27aafdca86b4f624775504109160dff1 |
File details
Details for the file drawarrow-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: drawarrow-0.0.2-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6734a24066fa5739139278ede848623e3394850156a0de53a2cf272dd2466853 |
|
MD5 | 8e2a8b450b3d6520f208f7af72ef694d |
|
BLAKE2b-256 | 5214c7d999a174083896b91b58424d5b34888c374e6405270067f9f3bb7083c1 |