Skip to main content

Module to plot 2-d vectors as arrows using Matplotlib.

Project description

Making Attractive 2-D Vector Diagrams in Python with PlotVec

How do I make attractive vector diagrams in Python? PlotVec makes plotting vectors easier.

(Note that the figures below use my matplotlibrc file, which is included in the GitHub repository for plotvec.)

Let's start by loading the plotvec() function and plotting a single vector:

$$ \mathbf{a} = [2,3]^T $$

import numpy as np
from plotvec import plotvec

a = np.array([2, 3])

plotvec(a)

png

We can add multiple vectors to the diagram easily. Let's add these vectors:

$$ \mathbf{b} =[1,-2]^T $$

$$ \mathbf{c} =[-1,-1]^T $$

a = np.array([2, 3])
b = np.array([1, -2])
c = np.array([-2, -1])

plotvec(a, b, c)

png

Note that by default plotvec() uses an equal aspect ratio -- this is important in many vector diagrams, for instance to tell whether two vectors are orthogonal. For instance, in the diagram above, vectors b and c are at 90 degree angles because they are orthogonal.

If an equal aspect ratio is not needed, plotvecR() can be used to plot vectors but orthogonal vectors will not necessarily be at 90 degree angles:

from plotvec import plotvecR

plotvecR(a, b, c)

png

A legend can be added by specifying labels:

import numpy as np
from plotvec import plotvec, plotvecR

a = np.array([2, 3])
b = np.array([1, -2])

plotvecR(a, b,
        labels = ['$\mathbf{a} = [ 2,3]^T$',
                  '$\mathbf{b} = [ 1, -2]^T$'],
        legendloc='upper left')

png

By default, vectors will be plotted with their tails at the origin (0,0). We can specify a different tail using the tail keyword argument:

plotvec(c, tail=[2,1])

png

When plotting a sequence of vectors, we can have the tail of each vector be positioned at the head of the previous vector by settin chain=True:

plotvec(a, b, c, chain=True)

png

When plotting with chain=True, the head of the last vector is at the position of the sum of the vectors. We can ask plotvec() to show this sum as a vector using plotsum=True:

plotvec(a, b, c, chain=True, plotsum=True)

png

You can combine plot the result of multiple plotvec() commands on the same axes by specifying newfig=False. When using this option, it is good to either specify the colors of the vectors or else use the color_offset keyword parameter to tell later calls where to start in the color cycle. The example below also shows how to use plot.annotate() to label vectors:

import matplotlib.pyplot as plt

plotvec([3,0], [0,4], chain=True);
plotvec([3,4], newfig=False, color_offset=2)
plt.annotate('3', (1.6, 0.1) );
plt.annotate('4', (3.1, 1.8) );
plt.annotate('5', (1.1, 1.9) );

png

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plotvec-1.7.2.tar.gz (145.8 kB view details)

Uploaded Source

Built Distribution

plotvec-1.7.2-py2.py3-none-any.whl (6.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file plotvec-1.7.2.tar.gz.

File metadata

  • Download URL: plotvec-1.7.2.tar.gz
  • Upload date:
  • Size: 145.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for plotvec-1.7.2.tar.gz
Algorithm Hash digest
SHA256 6b0a0ae84eaea23b4f2a654a5322e0f2a78406b90de8b5b786f45d94e249f59c
MD5 10abf28aeda0056ffe4af0eff74dd2d5
BLAKE2b-256 6cb5ef07a15c4c66ec98ca812a867e8c3c481280fa9563401e875b3218ddd85a

See more details on using hashes here.

File details

Details for the file plotvec-1.7.2-py2.py3-none-any.whl.

File metadata

  • Download URL: plotvec-1.7.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for plotvec-1.7.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7ab0298da6e63ac8b2ae76b0001f24efba72eef46d71ed63be66e488de14c09f
MD5 59b5a1681e67516662d8b3a3ce37e79c
BLAKE2b-256 dbb9469afeb5a3a2dfd8e30264630a1d0e64ef23e9424a064153154c10de6255

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page