Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

""" 

Scatter Plots 

""" 

 

#***************************************************************************** 

# Copyright (C) 2006 Alex Clemesha <clemesha@gmail.com>, 

# William Stein <wstein@gmail.com>, 

# 2008 Mike Hansen <mhansen@gmail.com>, 

# 2009 Emily Kirkman 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from sage.plot.primitive import GraphicPrimitive 

from sage.misc.decorators import options 

 

class ScatterPlot(GraphicPrimitive): 

""" 

Scatter plot graphics primitive. 

 

Input consists of two lists/arrays of the same length, whose 

values give the horizontal and vertical coordinates of each 

point in the scatter plot. Options may be passed in 

dictionary format. 

 

EXAMPLES:: 

 

sage: from sage.plot.scatter_plot import ScatterPlot 

sage: ScatterPlot([0,1,2], [3.5,2,5.1], {'facecolor':'white', 'marker':'s'}) 

Scatter plot graphics primitive on 3 data points 

""" 

def __init__(self, xdata, ydata, options): 

""" 

Scatter plot graphics primitive. 

 

EXAMPLES:: 

 

sage: import numpy 

sage: from sage.plot.scatter_plot import ScatterPlot 

sage: ScatterPlot(numpy.array([0,1,2]), numpy.array([3.5,2,5.1]), {'facecolor':'white', 'marker':'s'}) 

Scatter plot graphics primitive on 3 data points 

""" 

self.xdata = xdata 

self.ydata = ydata 

GraphicPrimitive.__init__(self, options) 

 

def get_minmax_data(self): 

""" 

Returns a dictionary with the bounding box data. 

 

EXAMPLES:: 

 

sage: s = scatter_plot([[0,1],[2,4],[3.2,6]]) 

sage: d = s.get_minmax_data() 

sage: d['xmin'] 

0.0 

sage: d['ymin'] 

1.0 

""" 

return {'xmin': self.xdata.min(), 

'xmax': self.xdata.max(), 

'ymin': self.ydata.min(), 

'ymax': self.ydata.max()} 

 

def _allowed_options(self): 

""" 

Return the dictionary of allowed options for the scatter plot 

graphics primitive. 

 

EXAMPLES:: 

 

sage: from sage.plot.scatter_plot import ScatterPlot 

sage: list(sorted(ScatterPlot([-1,2], [17,4], {})._allowed_options().items())) 

[('alpha', 'How transparent the marker border is.'), 

('clip', 'Whether or not to clip.'), 

('edgecolor', 'The color of the marker border.'), 

('facecolor', 'The color of the marker face.'), 

('hue', 'The color given as a hue.'), 

('marker', 'What shape to plot the points. See the documentation of plot() for the full list of markers.'), 

('markersize', 'the size of the markers.'), 

('rgbcolor', 'The color as an RGB tuple.'), 

('zorder', 'The layer level in which to draw.')] 

""" 

return {'markersize': 'the size of the markers.', 

'marker': 'What shape to plot the points. See the documentation of plot() for the full list of markers.', 

'alpha':'How transparent the marker border is.', 

'rgbcolor':'The color as an RGB tuple.', 

'hue':'The color given as a hue.', 

'facecolor':'The color of the marker face.', 

'edgecolor':'The color of the marker border.', 

'zorder':'The layer level in which to draw.', 

'clip': 'Whether or not to clip.'} 

 

def _repr_(self): 

""" 

Text representation of a scatter plot graphics primitive. 

 

EXAMPLES:: 

 

sage: import numpy 

sage: from sage.plot.scatter_plot import ScatterPlot 

sage: ScatterPlot(numpy.array([0,1,2]), numpy.array([3.5,2,5.1]), {}) 

Scatter plot graphics primitive on 3 data points 

""" 

return 'Scatter plot graphics primitive on %s data points'%len(self.xdata) 

 

def _render_on_subplot(self, subplot): 

""" 

Render this scatter plot in a subplot. This is the key function that 

defines how this scatter plot graphics primitive is rendered in 

matplotlib's library. 

 

EXAMPLES:: 

 

sage: scatter_plot([[0,1],[2,2],[4.3,1.1]], marker='s') 

Graphics object consisting of 1 graphics primitive 

 

:: 

 

sage: scatter_plot([[n,n] for n in range(5)]) 

Graphics object consisting of 1 graphics primitive 

""" 

from matplotlib.pyplot import scatter 

options = self.options() 

p = subplot.scatter(self.xdata, self.ydata, alpha=options['alpha'], 

zorder=options['zorder'], marker=options['marker'], 

s=options['markersize'], facecolors=options['facecolor'], 

edgecolors=options['edgecolor'], clip_on=options['clip']) 

if not options['clip']: 

self._bbox_extra_artists=[p] 

 

@options(alpha=1, markersize=50, marker='o', zorder=5, facecolor='#fec7b8', edgecolor='black', clip=True, aspect_ratio='automatic') 

def scatter_plot(datalist, **options): 

""" 

Returns a Graphics object of a scatter plot containing all points in 

the datalist. Type ``scatter_plot.options`` to see all available 

plotting options. 

 

INPUT: 

 

- ``datalist`` -- a list of tuples ``(x,y)`` 

 

- ``alpha`` -- default: 1 

 

- ``markersize`` -- default: 50 

 

- ``marker`` - The style of the markers (default ``"o"``). See the 

documentation of :func:`plot` for the full list of markers. 

 

- ``facecolor`` -- default: ``'#fec7b8'`` 

 

- ``edgecolor`` -- default: ``'black'`` 

 

- ``zorder`` -- default: 5 

 

EXAMPLES:: 

 

sage: scatter_plot([[0,1],[2,2],[4.3,1.1]], marker='s') 

Graphics object consisting of 1 graphics primitive 

 

Extra options will get passed on to :meth:`~Graphics.show`, as long as they are valid:: 

 

sage: scatter_plot([(0, 0), (1, 1)], markersize=100, facecolor='green', ymax=100) 

Graphics object consisting of 1 graphics primitive 

sage: scatter_plot([(0, 0), (1, 1)], markersize=100, facecolor='green').show(ymax=100) # These are equivalent 

""" 

import numpy 

from sage.plot.all import Graphics 

g = Graphics() 

g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 

data = numpy.array(datalist, dtype='float') 

if len(data) != 0: 

xdata = data[:,0] 

ydata = data[:,1] 

g.add_primitive(ScatterPlot(xdata, ydata, options=options)) 

return g