File: plot_interp_grid.py

package info (click to toggle)
scipy 1.17.0-1exp2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 235,340 kB
  • sloc: cpp: 506,914; python: 357,038; ansic: 215,028; javascript: 89,566; fortran: 19,308; cs: 3,081; f90: 1,150; sh: 860; makefile: 519; pascal: 284; lisp: 134; xml: 56; perl: 51
file content (63 lines) | stat: -rw-r--r-- 1,985 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import math

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import ticker


orders = [2, 3]
fig, axes = plt.subplots(1, len(orders), figsize=(11, 5))
n_cells = 7  # grid will be size (n_cells, n_cells)

# desired interpolation coordinate (xi, yi)
xi, yi = 3.3, 3.7


def get_start(cc, order):
    if order % 1 == 0:
        start = math.floor(cc) - order // 2
    else:
        start = math.floor(cc + 0.5) - order // 2
    return start


for ax, order in zip(axes, orders):
    # draw open circles at the locations of pixel centers
    for n in range(n_cells):
        ax.plot(np.arange(n_cells), -np.full(n_cells, n), 'ko',
                fillstyle='none')

    # draw pixel borders
    for n in range(n_cells + 1):
        ax.plot([n - 0.5, n - 0.5], [0.5, -n_cells + .5], 'k-')
        ax.plot([-0.5, n_cells - .5], [-n + 0.5, -n + 0.5], 'k-')

    # plot an example coordinate location to interpolate
    ax.plot([xi], [-yi], 'rx')

    # plot filled circles for the points that will be involved in the
    # interpolation
    startx = get_start(xi, order)
    starty = get_start(yi, order)
    xc = np.tile(np.arange(startx, startx + order + 1)[:, np.newaxis],
                 (1, order + 1)).ravel()
    yc = np.tile(np.arange(starty, starty + order + 1)[np.newaxis, :],
                 (order + 1, 1)).ravel()
    ax.plot(xc, -yc, 'ko')
    ax.set_title(f"Interpolation (order = {order})",
                 fontdict=dict(size=16, weight='bold'))

    # set limits and ticks for 0, 0 voxel at upper left
    ax.axis('square')
    ax.set_xticks(np.arange(n_cells + 1))
    ax.xaxis.tick_top()
    ax.xaxis.set_label_position('top')
    yticks = ticker.FixedLocator(-np.arange(n_cells, -1, -1))
    ax.yaxis.set_major_locator(yticks)
    yticklabels = ticker.FixedFormatter(np.arange(n_cells, -1, -1))
    ax.yaxis.set_major_formatter(yticklabels)
    ax.set_ylim([-n_cells + 0.5, 0.5])
    ax.set_xlim([-0.5, n_cells - 0.5])

plt.tight_layout()
plt.plot()