Skip to content

Commit 4de8086

Browse files
committed
rebuild docs with new discourse on front page
1 parent 58ebfb0 commit 4de8086

File tree

7,384 files changed

+190088
-16456
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

7,384 files changed

+190088
-16456
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
"""
2+
=====================
3+
Curvilinear grid demo
4+
=====================
5+
6+
Custom grid and ticklines.
7+
8+
This example demonstrates how to use
9+
`~.grid_helper_curvelinear.GridHelperCurveLinear` to define custom grids and
10+
ticklines by applying a transformation on the grid. This can be used, as
11+
shown on the second plot, to create polar projections in a rectangular box.
12+
"""
13+
14+
import numpy as np
15+
16+
import matplotlib.pyplot as plt
17+
from matplotlib.projections import PolarAxes
18+
from matplotlib.transforms import Affine2D
19+
20+
from mpl_toolkits.axisartist import (
21+
angle_helper, Subplot, SubplotHost, ParasiteAxesAuxTrans)
22+
from mpl_toolkits.axisartist.grid_helper_curvelinear import (
23+
GridHelperCurveLinear)
24+
25+
26+
def curvelinear_test1(fig):
27+
"""
28+
Grid for custom transform.
29+
"""
30+
31+
def tr(x, y):
32+
x, y = np.asarray(x), np.asarray(y)
33+
return x, y - x
34+
35+
def inv_tr(x, y):
36+
x, y = np.asarray(x), np.asarray(y)
37+
return x, y + x
38+
39+
grid_helper = GridHelperCurveLinear((tr, inv_tr))
40+
41+
ax1 = Subplot(fig, 1, 2, 1, grid_helper=grid_helper)
42+
# ax1 will have a ticks and gridlines defined by the given
43+
# transform (+ transData of the Axes). Note that the transform of
44+
# the Axes itself (i.e., transData) is not affected by the given
45+
# transform.
46+
47+
fig.add_subplot(ax1)
48+
49+
xx, yy = tr([3, 6], [5, 10])
50+
ax1.plot(xx, yy, linewidth=2.0)
51+
52+
ax1.set_aspect(1)
53+
ax1.set_xlim(0, 10)
54+
ax1.set_ylim(0, 10)
55+
56+
ax1.axis["t"] = ax1.new_floating_axis(0, 3)
57+
ax1.axis["t2"] = ax1.new_floating_axis(1, 7)
58+
ax1.grid(True, zorder=0)
59+
60+
61+
def curvelinear_test2(fig):
62+
"""
63+
Polar projection, but in a rectangular box.
64+
"""
65+
66+
# PolarAxes.PolarTransform takes radian. However, we want our coordinate
67+
# system in degree
68+
tr = Affine2D().scale(np.pi/180, 1) + PolarAxes.PolarTransform()
69+
# Polar projection, which involves cycle, and also has limits in
70+
# its coordinates, needs a special method to find the extremes
71+
# (min, max of the coordinate within the view).
72+
extreme_finder = angle_helper.ExtremeFinderCycle(
73+
nx=20, ny=20, # Number of sampling points in each direction.
74+
lon_cycle=360, lat_cycle=None,
75+
lon_minmax=None, lat_minmax=(0, np.inf),
76+
)
77+
# Find grid values appropriate for the coordinate (degree, minute, second).
78+
grid_locator1 = angle_helper.LocatorDMS(12)
79+
# Use an appropriate formatter. Note that the acceptable Locator and
80+
# Formatter classes are a bit different than that of Matplotlib, which
81+
# cannot directly be used here (this may be possible in the future).
82+
tick_formatter1 = angle_helper.FormatterDMS()
83+
84+
grid_helper = GridHelperCurveLinear(
85+
tr, extreme_finder=extreme_finder,
86+
grid_locator1=grid_locator1, tick_formatter1=tick_formatter1)
87+
ax1 = SubplotHost(fig, 1, 2, 2, grid_helper=grid_helper)
88+
89+
# make ticklabels of right and top axis visible.
90+
ax1.axis["right"].major_ticklabels.set_visible(True)
91+
ax1.axis["top"].major_ticklabels.set_visible(True)
92+
# let right axis shows ticklabels for 1st coordinate (angle)
93+
ax1.axis["right"].get_helper().nth_coord_ticks = 0
94+
# let bottom axis shows ticklabels for 2nd coordinate (radius)
95+
ax1.axis["bottom"].get_helper().nth_coord_ticks = 1
96+
97+
fig.add_subplot(ax1)
98+
99+
ax1.set_aspect(1)
100+
ax1.set_xlim(-5, 12)
101+
ax1.set_ylim(-5, 10)
102+
103+
ax1.grid(True, zorder=0)
104+
105+
# A parasite axes with given transform
106+
ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal")
107+
# note that ax2.transData == tr + ax1.transData
108+
# Anything you draw in ax2 will match the ticks and grids of ax1.
109+
ax1.parasites.append(ax2)
110+
ax2.plot(np.linspace(0, 30, 51), np.linspace(10, 10, 51), linewidth=2)
111+
112+
113+
if __name__ == "__main__":
114+
fig = plt.figure(figsize=(7, 4))
115+
116+
curvelinear_test1(fig)
117+
curvelinear_test2(fig)
118+
119+
plt.show()
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
=====================
3+
Polygon Selector Demo
4+
=====================
5+
6+
Shows how one can select indices of a polygon interactively.
7+
8+
"""
9+
import numpy as np
10+
11+
from matplotlib.widgets import PolygonSelector
12+
from matplotlib.path import Path
13+
14+
15+
class SelectFromCollection(object):
16+
"""Select indices from a matplotlib collection using `PolygonSelector`.
17+
18+
Selected indices are saved in the `ind` attribute. This tool fades out the
19+
points that are not part of the selection (i.e., reduces their alpha
20+
values). If your collection has alpha < 1, this tool will permanently
21+
alter the alpha values.
22+
23+
Note that this tool selects collection objects based on their *origins*
24+
(i.e., `offsets`).
25+
26+
Parameters
27+
----------
28+
ax : :class:`~matplotlib.axes.Axes`
29+
Axes to interact with.
30+
31+
collection : :class:`matplotlib.collections.Collection` subclass
32+
Collection you want to select from.
33+
34+
alpha_other : 0 <= float <= 1
35+
To highlight a selection, this tool sets all selected points to an
36+
alpha value of 1 and non-selected points to `alpha_other`.
37+
"""
38+
39+
def __init__(self, ax, collection, alpha_other=0.3):
40+
self.canvas = ax.figure.canvas
41+
self.collection = collection
42+
self.alpha_other = alpha_other
43+
44+
self.xys = collection.get_offsets()
45+
self.Npts = len(self.xys)
46+
47+
# Ensure that we have separate colors for each object
48+
self.fc = collection.get_facecolors()
49+
if len(self.fc) == 0:
50+
raise ValueError('Collection must have a facecolor')
51+
elif len(self.fc) == 1:
52+
self.fc = np.tile(self.fc, (self.Npts, 1))
53+
54+
self.poly = PolygonSelector(ax, self.onselect)
55+
self.ind = []
56+
57+
def onselect(self, verts):
58+
path = Path(verts)
59+
self.ind = np.nonzero(path.contains_points(self.xys))[0]
60+
self.fc[:, -1] = self.alpha_other
61+
self.fc[self.ind, -1] = 1
62+
self.collection.set_facecolors(self.fc)
63+
self.canvas.draw_idle()
64+
65+
def disconnect(self):
66+
self.poly.disconnect_events()
67+
self.fc[:, -1] = 1
68+
self.collection.set_facecolors(self.fc)
69+
self.canvas.draw_idle()
70+
71+
72+
if __name__ == '__main__':
73+
import matplotlib.pyplot as plt
74+
75+
fig, ax = plt.subplots()
76+
grid_size = 5
77+
grid_x = np.tile(np.arange(grid_size), grid_size)
78+
grid_y = np.repeat(np.arange(grid_size), grid_size)
79+
pts = ax.scatter(grid_x, grid_y)
80+
81+
selector = SelectFromCollection(ax, pts)
82+
83+
print("Select points in the figure by enclosing them within a polygon.")
84+
print("Press the 'esc' key to start a new polygon.")
85+
print("Try holding the 'shift' key to move all of the vertices.")
86+
print("Try holding the 'ctrl' key to move a single vertex.")
87+
88+
plt.show()
89+
90+
selector.disconnect()
91+
92+
# After figure is closed print the coordinates of the selected points
93+
print('\nSelected points:')
94+
print(selector.xys[selector.ind])

0 commit comments

Comments
 (0)