Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/plot/scatter_plot.py
4034 views
1
"""
2
Scatter Plots
3
"""
4
5
#*****************************************************************************
6
# Copyright (C) 2006 Alex Clemesha <[email protected]>,
7
# William Stein <[email protected]>,
8
# 2008 Mike Hansen <[email protected]>,
9
# 2009 Emily Kirkman
10
#
11
# Distributed under the terms of the GNU General Public License (GPL)
12
#
13
# This code is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
# General Public License for more details.
17
#
18
# The full text of the GPL is available at:
19
#
20
# http://www.gnu.org/licenses/
21
#*****************************************************************************
22
from sage.plot.primitive import GraphicPrimitive
23
from sage.misc.decorators import options
24
25
class ScatterPlot(GraphicPrimitive):
26
"""
27
Scatter plot graphics primitive.
28
29
Input consists of two lists/arrays of the same length, whose
30
values give the horizontal and vertical coordinates of each
31
point in the scatter plot. Options may be passed in
32
dictionary format.
33
34
EXAMPLES::
35
36
sage: from sage.plot.scatter_plot import ScatterPlot
37
sage: ScatterPlot([0,1,2], [3.5,2,5.1], {'facecolor':'white', 'marker':'s'})
38
Scatter plot graphics primitive on 3 data points
39
"""
40
def __init__(self, xdata, ydata, options):
41
"""
42
Scatter plot graphics primitive.
43
44
EXAMPLES::
45
46
sage: import numpy
47
sage: from sage.plot.scatter_plot import ScatterPlot
48
sage: ScatterPlot(numpy.array([0,1,2]), numpy.array([3.5,2,5.1]), {'facecolor':'white', 'marker':'s'})
49
Scatter plot graphics primitive on 3 data points
50
"""
51
self.xdata = xdata
52
self.ydata = ydata
53
GraphicPrimitive.__init__(self, options)
54
55
def get_minmax_data(self):
56
"""
57
Returns a dictionary with the bounding box data.
58
59
EXAMPLES::
60
61
sage: s = scatter_plot([[0,1],[2,4],[3.2,6]])
62
sage: d = s.get_minmax_data()
63
sage: d['xmin']
64
0.0
65
sage: d['ymin']
66
1.0
67
"""
68
return {'xmin': self.xdata.min(),
69
'xmax': self.xdata.max(),
70
'ymin': self.ydata.min(),
71
'ymax': self.ydata.max()}
72
73
def _allowed_options(self):
74
"""
75
Return the dictionary of allowed options for the scatter plot
76
graphics primitive.
77
78
EXAMPLES::
79
80
sage: from sage.plot.scatter_plot import ScatterPlot
81
sage: list(sorted(ScatterPlot([-1,2], [17,4], {})._allowed_options().iteritems()))
82
[('alpha', 'How transparent the marker border is.'),
83
('clip', 'Whether or not to clip.'),
84
('edgecolor', 'The color of the marker border.'),
85
('facecolor', 'The color of the marker face.'),
86
('hue', 'The color given as a hue.'),
87
('marker', 'What shape to plot the points.'),
88
('markersize', 'the size of the markers.'),
89
('rgbcolor', 'The color as an RGB tuple.'),
90
('zorder', 'The layer level in which to draw.')]
91
"""
92
return {'markersize': 'the size of the markers.',
93
'marker': 'What shape to plot the points.',
94
'alpha':'How transparent the marker border is.',
95
'rgbcolor':'The color as an RGB tuple.',
96
'hue':'The color given as a hue.',
97
'facecolor':'The color of the marker face.',
98
'edgecolor':'The color of the marker border.',
99
'zorder':'The layer level in which to draw.',
100
'clip': 'Whether or not to clip.'}
101
102
def _repr_(self):
103
"""
104
Text representation of a scatter plot graphics primitive.
105
106
EXAMPLES::
107
108
sage: import numpy
109
sage: from sage.plot.scatter_plot import ScatterPlot
110
sage: ScatterPlot(numpy.array([0,1,2]), numpy.array([3.5,2,5.1]), {})
111
Scatter plot graphics primitive on 3 data points
112
"""
113
return 'Scatter plot graphics primitive on %s data points'%len(self.xdata)
114
115
def _render_on_subplot(self, subplot):
116
"""
117
Render this scatter plot in a subplot. This is the key function that
118
defines how this scatter plot graphics primitive is rendered in
119
matplotlib's library.
120
121
EXAMPLES::
122
123
sage: scatter_plot([[0,1],[2,2],[4.3,1.1]], marker='s')
124
125
::
126
127
sage: scatter_plot([[n,n] for n in range(5)])
128
"""
129
from matplotlib.pyplot import scatter
130
options = self.options()
131
p = subplot.scatter(self.xdata, self.ydata, alpha=options['alpha'],
132
zorder=options['zorder'], marker=options['marker'],
133
s=options['markersize'], facecolors=options['facecolor'],
134
edgecolors=options['edgecolor'], clip_on=options['clip'])
135
if not options['clip']:
136
self._bbox_extra_artists=[p]
137
138
@options(alpha=1, markersize=50, marker='o', zorder=5, facecolor='#fec7b8', edgecolor='black', clip=True, aspect_ratio='automatic')
139
def scatter_plot(datalist, **options):
140
"""
141
Returns a Graphics object of a scatter plot containing all points in
142
the datalist. Type ``scatter_plot.options`` to see all available
143
plotting options.
144
145
INPUT:
146
147
- ``datalist`` -- a list of tuples ``(x,y)``
148
149
- ``alpha`` -- default: 1
150
151
- ``markersize`` -- default: 50
152
153
- ``marker`` - The style of the markers (default ``"o"``), which is one of
154
- ``"None"`` or ``" "`` or ``""`` (nothing)
155
- ``","`` (pixel), ``"."`` (point)
156
- ``"_"`` (horizontal line), ``"|"`` (vertical line)
157
- ``"o"`` (circle), ``"p"`` (pentagon), ``"s"`` (square), ``"x"`` (x), ``"+"`` (plus), ``"*"`` (star)
158
- ``"D"`` (diamond), ``"d"`` (thin diamond)
159
- ``"H"`` (hexagon), ``"h"`` (alternative hexagon)
160
- ``"<"`` (triangle left), ``">"`` (triangle right), ``"^"`` (triangle up), ``"v"`` (triangle down)
161
- ``"1"`` (tri down), ``"2"`` (tri up), ``"3"`` (tri left), ``"4"`` (tri right)
162
- ``0`` (tick left), ``1`` (tick right), ``2`` (tick up), ``3`` (tick down)
163
- ``4`` (caret left), ``5`` (caret right), ``6`` (caret up), ``7`` (caret down)
164
- ``"$...$"`` (math TeX string)
165
166
- ``facecolor`` -- default: ``'#fec7b8'``
167
168
- ``edgecolor`` -- default: ``'black'``
169
170
- ``zorder`` -- default: 5
171
172
EXAMPLES::
173
174
sage: scatter_plot([[0,1],[2,2],[4.3,1.1]], marker='s')
175
176
Extra options will get passed on to :meth:`~Graphics.show`, as long as they are valid::
177
178
sage: scatter_plot([(0, 0), (1, 1)], markersize=100, facecolor='green', ymax=100)
179
sage: scatter_plot([(0, 0), (1, 1)], markersize=100, facecolor='green').show(ymax=100) # These are equivalent
180
"""
181
import numpy
182
from sage.plot.all import Graphics
183
g = Graphics()
184
g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
185
data = numpy.array(datalist, dtype='float')
186
if len(data) != 0:
187
xdata = data[:,0]
188
ydata = data[:,1]
189
g.add_primitive(ScatterPlot(xdata, ydata, options=options))
190
return g
191
192