|
1 | | -.. _adding-new-scales: |
2 | | - |
3 | | -*********************************************** |
4 | | -Adding new scales and projections to matplotlib |
5 | | -*********************************************** |
6 | | - |
7 | | -.. ::author Michael Droettboom |
8 | | -
|
9 | | -Matplotlib supports the addition of custom procedures that transform |
10 | | -the data before it is displayed. |
11 | | - |
12 | | -There is an important distinction between two kinds of |
13 | | -transformations. Separable transformations, working on a single |
14 | | -dimension, are called "scales", and non-separable transformations, |
15 | | -that handle data in two or more dimensions at a time, are called |
16 | | -"projections". |
17 | | - |
18 | | -From the user's perspective, the scale of a plot can be set with |
19 | | -:meth:`~matplotlib.axes.Axes.set_xscale` and |
20 | | -:meth:`~matplotlib.axes.Axes.set_xscale`. Projections can be chosen |
21 | | -using the ``projection`` keyword argument to the |
22 | | -:func:`~matplotlib.pylab.plot` or :func:`~matplotlib.pylab.subplot` |
23 | | -functions, e.g.:: |
24 | | - |
25 | | - plot(x, y, projection="custom") |
26 | | - |
27 | | -This document is intended for developers and advanced users who need |
28 | | -to create new scales and projections for matplotlib. The necessary |
29 | | -code for scales and projections can be included anywhere: directly |
30 | | -within a plot script, in third-party code, or in the matplotlib source |
31 | | -tree itself. |
32 | | - |
33 | | -.. _creating-new-scale: |
34 | | - |
35 | | -Creating a new scale |
36 | | -==================== |
37 | | - |
38 | | -Adding a new scale consists of defining a subclass of |
39 | | -:class:`matplotlib.scale.ScaleBase`, that includes the following |
40 | | -elements: |
41 | | - |
42 | | - - A transformation from data coordinates into display coordinates. |
43 | | - |
44 | | - - An inverse of that transformation. This is used, for example, to |
45 | | - convert mouse positions from screen space back into data space. |
46 | | - |
47 | | - - A function to limit the range of the axis to acceptable values |
48 | | - (``limit_range_for_scale()``). A log scale, for instance, would |
49 | | - prevent the range from including values less than or equal to |
50 | | - zero. |
51 | | - |
52 | | - - Locators (major and minor) that determine where to place ticks in |
53 | | - the plot, and optionally, how to adjust the limits of the plot to |
54 | | - some "good" values. Unlike ``limit_range_for_scale()``, which is |
55 | | - always enforced, the range setting here is only used when |
56 | | - automatically setting the range of the plot. |
57 | | - |
58 | | - - Formatters (major and minor) that specify how the tick labels |
59 | | - should be drawn. |
60 | | - |
61 | | -Once the class is defined, it must be registered with matplotlib so |
62 | | -that the user can select it. |
63 | | - |
64 | | -A full-fledged and heavily annotated example is in |
65 | | -:file:`examples/api/custom_scale_example.py`. There are also some classes |
66 | | -in :mod:`matplotlib.scale` that may be used as starting points. |
67 | | - |
68 | | - |
69 | | -.. _creating-new-projection: |
70 | | - |
71 | | -Creating a new projection |
72 | | -========================= |
73 | | - |
74 | | -Adding a new projection consists of defining a subclass of |
75 | | -:class:`matplotlib.axes.Axes`, that includes the following elements: |
76 | | - |
77 | | - - A transformation from data coordinates into display coordinates. |
78 | | - |
79 | | - - An inverse of that transformation. This is used, for example, to |
80 | | - convert mouse positions from screen space back into data space. |
81 | | - |
82 | | - - Transformations for the gridlines, ticks and ticklabels. Custom |
83 | | - projections will often need to place these elements in special |
84 | | - locations, and matplotlib has a facility to help with doing so. |
85 | | - |
86 | | - - Setting up default values (overriding |
87 | | - :meth:`~matplotlib.axes.Axes.cla`), since the defaults for a |
88 | | - rectilinear axes may not be appropriate. |
89 | | - |
90 | | - - Defining the shape of the axes, for example, an elliptical axes, |
91 | | - that will be used to draw the background of the plot and for |
92 | | - clipping any data elements. |
93 | | - |
94 | | - - Defining custom locators and formatters for the projection. For |
95 | | - example, in a geographic projection, it may be more convenient to |
96 | | - display the grid in degrees, even if the data is in radians. |
97 | | - |
98 | | - - Set up interactive panning and zooming. This is left as an |
99 | | - "advanced" feature left to the reader, but there is an example of |
100 | | - this for polar plots in :mod:`matplotlib.projections.polar`. |
101 | | - |
102 | | - - Any additional methods for additional convenience or features. |
103 | | - |
104 | | -Once the class is defined, it must be registered with matplotlib |
105 | | -so that the user can select it. |
106 | | - |
107 | | -A full-fledged and heavily annotated example is in |
108 | | -:file:`examples/api/custom_projection_example.py`. The polar plot |
109 | | -functionality in :mod:`matplotlib.projections.polar` may also be of |
110 | | -interest. |
111 | | - |
112 | | -API documentation |
113 | | -================= |
114 | | - |
115 | | -matplotlib.scale |
116 | | ----------------- |
117 | | - |
118 | | -.. automodule:: matplotlib.scale |
119 | | - :members: |
120 | | - :show-inheritance: |
121 | | - |
122 | | -matplotlib.projections |
123 | | ----------------------- |
124 | | - |
125 | | -.. automodule:: matplotlib.projections |
126 | | - :members: |
127 | | - :show-inheritance: |
128 | | - |
129 | | -matplotlib.projections.polar |
130 | | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
131 | | - |
132 | | -.. automodule:: matplotlib.projections.polar |
133 | | - :members: |
134 | | - :show-inheritance: |
| 1 | +.. _adding-new-scales: |
| 2 | + |
| 3 | +*********************************************** |
| 4 | +Adding new scales and projections to matplotlib |
| 5 | +*********************************************** |
| 6 | + |
| 7 | +.. ::author Michael Droettboom |
| 8 | +
|
| 9 | +Matplotlib supports the addition of custom procedures that transform |
| 10 | +the data before it is displayed. |
| 11 | + |
| 12 | +There is an important distinction between two kinds of |
| 13 | +transformations. Separable transformations, working on a single |
| 14 | +dimension, are called "scales", and non-separable transformations, |
| 15 | +that handle data in two or more dimensions at a time, are called |
| 16 | +"projections". |
| 17 | + |
| 18 | +From the user's perspective, the scale of a plot can be set with |
| 19 | +:meth:`~matplotlib.axes.Axes.set_xscale` and |
| 20 | +:meth:`~matplotlib.axes.Axes.set_xscale`. Projections can be chosen |
| 21 | +using the ``projection`` keyword argument to the |
| 22 | +:func:`~matplotlib.pylab.plot` or :func:`~matplotlib.pylab.subplot` |
| 23 | +functions, e.g.:: |
| 24 | + |
| 25 | + plot(x, y, projection="custom") |
| 26 | + |
| 27 | +This document is intended for developers and advanced users who need |
| 28 | +to create new scales and projections for matplotlib. The necessary |
| 29 | +code for scales and projections can be included anywhere: directly |
| 30 | +within a plot script, in third-party code, or in the matplotlib source |
| 31 | +tree itself. |
| 32 | + |
| 33 | +.. _creating-new-scale: |
| 34 | + |
| 35 | +Creating a new scale |
| 36 | +==================== |
| 37 | + |
| 38 | +Adding a new scale consists of defining a subclass of |
| 39 | +:class:`matplotlib.scale.ScaleBase`, that includes the following |
| 40 | +elements: |
| 41 | + |
| 42 | + - A transformation from data coordinates into display coordinates. |
| 43 | + |
| 44 | + - An inverse of that transformation. This is used, for example, to |
| 45 | + convert mouse positions from screen space back into data space. |
| 46 | + |
| 47 | + - A function to limit the range of the axis to acceptable values |
| 48 | + (``limit_range_for_scale()``). A log scale, for instance, would |
| 49 | + prevent the range from including values less than or equal to |
| 50 | + zero. |
| 51 | + |
| 52 | + - Locators (major and minor) that determine where to place ticks in |
| 53 | + the plot, and optionally, how to adjust the limits of the plot to |
| 54 | + some "good" values. Unlike ``limit_range_for_scale()``, which is |
| 55 | + always enforced, the range setting here is only used when |
| 56 | + automatically setting the range of the plot. |
| 57 | + |
| 58 | + - Formatters (major and minor) that specify how the tick labels |
| 59 | + should be drawn. |
| 60 | + |
| 61 | +Once the class is defined, it must be registered with matplotlib so |
| 62 | +that the user can select it. |
| 63 | + |
| 64 | +A full-fledged and heavily annotated example is in |
| 65 | +:file:`examples/api/custom_scale_example.py`. There are also some classes |
| 66 | +in :mod:`matplotlib.scale` that may be used as starting points. |
| 67 | + |
| 68 | + |
| 69 | +.. _creating-new-projection: |
| 70 | + |
| 71 | +Creating a new projection |
| 72 | +========================= |
| 73 | + |
| 74 | +Adding a new projection consists of defining a subclass of |
| 75 | +:class:`matplotlib.axes.Axes`, that includes the following elements: |
| 76 | + |
| 77 | + - A transformation from data coordinates into display coordinates. |
| 78 | + |
| 79 | + - An inverse of that transformation. This is used, for example, to |
| 80 | + convert mouse positions from screen space back into data space. |
| 81 | + |
| 82 | + - Transformations for the gridlines, ticks and ticklabels. Custom |
| 83 | + projections will often need to place these elements in special |
| 84 | + locations, and matplotlib has a facility to help with doing so. |
| 85 | + |
| 86 | + - Setting up default values (overriding |
| 87 | + :meth:`~matplotlib.axes.Axes.cla`), since the defaults for a |
| 88 | + rectilinear axes may not be appropriate. |
| 89 | + |
| 90 | + - Defining the shape of the axes, for example, an elliptical axes, |
| 91 | + that will be used to draw the background of the plot and for |
| 92 | + clipping any data elements. |
| 93 | + |
| 94 | + - Defining custom locators and formatters for the projection. For |
| 95 | + example, in a geographic projection, it may be more convenient to |
| 96 | + display the grid in degrees, even if the data is in radians. |
| 97 | + |
| 98 | + - Set up interactive panning and zooming. This is left as an |
| 99 | + "advanced" feature left to the reader, but there is an example of |
| 100 | + this for polar plots in :mod:`matplotlib.projections.polar`. |
| 101 | + |
| 102 | + - Any additional methods for additional convenience or features. |
| 103 | + |
| 104 | +Once the class is defined, it must be registered with matplotlib |
| 105 | +so that the user can select it. |
| 106 | + |
| 107 | +A full-fledged and heavily annotated example is in |
| 108 | +:file:`examples/api/custom_projection_example.py`. The polar plot |
| 109 | +functionality in :mod:`matplotlib.projections.polar` may also be of |
| 110 | +interest. |
| 111 | + |
| 112 | +API documentation |
| 113 | +================= |
| 114 | + |
| 115 | +matplotlib.scale |
| 116 | +---------------- |
| 117 | + |
| 118 | +.. automodule:: matplotlib.scale |
| 119 | + :members: |
| 120 | + :show-inheritance: |
| 121 | + |
| 122 | +matplotlib.projections |
| 123 | +---------------------- |
| 124 | + |
| 125 | +.. automodule:: matplotlib.projections |
| 126 | + :members: |
| 127 | + :show-inheritance: |
| 128 | + |
| 129 | +matplotlib.projections.polar |
| 130 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 131 | + |
| 132 | +.. automodule:: matplotlib.projections.polar |
| 133 | + :members: |
| 134 | + :show-inheritance: |
0 commit comments