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

""" 

Bar Charts 

""" 

 

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

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

# William Stein <wstein@gmail.com>, 

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

# 

# 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.plot.plot import minmax_data 

from sage.plot.graphics import Graphics 

from sage.misc.decorators import options, rename_keyword 

 

#TODO: make bar_chart more general 

class BarChart(GraphicPrimitive): 

""" 

Graphics primitive that represents a bar chart. 

 

EXAMPLES:: 

 

sage: from sage.plot.bar_chart import BarChart 

sage: g = BarChart(list(range(4)), [1,3,2,0], {}); g 

BarChart defined by a 4 datalist 

sage: type(g) 

<class 'sage.plot.bar_chart.BarChart'> 

""" 

def __init__(self, ind, datalist, options): 

""" 

Initialize a ``BarChart`` primitive. 

 

EXAMPLES:: 

 

sage: from sage.plot.bar_chart import BarChart 

sage: BarChart(list(range(3)), [10,3,5], {'width':0.7}) 

BarChart defined by a 3 datalist 

""" 

self.datalist = datalist 

self.ind = ind 

GraphicPrimitive.__init__(self, options) 

 

def get_minmax_data(self): 

""" 

Returns a dictionary with the bounding box data. 

 

EXAMPLES:: 

 

sage: b = bar_chart([-2.3,5,-6,12]) 

sage: d = b.get_minmax_data() 

sage: d['xmin'] 

0 

sage: d['xmax'] 

4 

""" 

return minmax_data([0, len(self.datalist)], self.datalist, dict=True) 

 

def _allowed_options(self): 

""" 

Return the allowed options with descriptions for this graphics 

primitive. This is used in displaying an error message when the 

user gives an option that doesn't make sense. 

 

EXAMPLES:: 

 

sage: from sage.plot.bar_chart import BarChart 

sage: g = BarChart(list(range(4)), [1,3,2,0], {}) 

sage: list(sorted(g._allowed_options().items())) 

[('hue', 'The color given as a hue.'), ('legend_label', 'The label for this item in the legend.'), ('rgbcolor', 'The color as an RGB tuple.'), ('width', 'The width of the bars'), ('zorder', 'The layer level in which to draw')] 

""" 

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

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

'legend_label':'The label for this item in the legend.', 

'width':'The width of the bars', 

'zorder':'The layer level in which to draw'} 

 

def _repr_(self): 

""" 

Return text representation of this bar chart graphics primitive. 

 

EXAMPLES:: 

 

sage: from sage.plot.bar_chart import BarChart 

sage: g = BarChart(list(range(4)), [1,3,2,0], {}) 

sage: g._repr_() 

'BarChart defined by a 4 datalist' 

""" 

return "BarChart defined by a %s datalist"%(len(self.datalist)) 

 

def _render_on_subplot(self, subplot): 

""" 

Render this bar chart graphics primitive on a matplotlib subplot 

object. 

 

EXAMPLES: 

 

This rendering happens implicitly when the following command 

is executed:: 

 

sage: bar_chart([1,2,10]) 

Graphics object consisting of 1 graphics primitive 

""" 

options = self.options() 

color = options['rgbcolor'] 

width = float(options['width']) 

# it is critical to make NumPy arrays of type float below, 

# or bar will go boom: 

import numpy 

ind = numpy.array(self.ind, dtype=float) 

datalist = numpy.array(self.datalist, dtype=float) 

subplot.bar(ind, datalist, color=color, width=width, label=options['legend_label']) 

 

@rename_keyword(color='rgbcolor') 

@options(width=0.5, rgbcolor=(0,0,1), legend_label=None, aspect_ratio='automatic') 

def bar_chart(datalist, **options): 

""" 

A bar chart of (currently) one list of numerical data. 

Support for more data lists in progress. 

 

EXAMPLES: 

 

A bar_chart with blue bars:: 

 

sage: bar_chart([1,2,3,4]) 

Graphics object consisting of 1 graphics primitive 

 

A bar_chart with thinner bars:: 

 

sage: bar_chart([x^2 for x in range(1,20)], width=0.2) 

Graphics object consisting of 1 graphics primitive 

 

A bar_chart with negative values and red bars:: 

 

sage: bar_chart([-3,5,-6,11], rgbcolor=(1,0,0)) 

Graphics object consisting of 1 graphics primitive 

 

A bar chart with a legend (it's possible, not necessarily useful):: 

 

sage: bar_chart([-1,1,-1,1], legend_label='wave') 

Graphics object consisting of 1 graphics primitive 

 

Extra options will get passed on to show(), as long as they are valid:: 

 

sage: bar_chart([-2,8,-7,3], rgbcolor=(1,0,0), axes=False) 

Graphics object consisting of 1 graphics primitive 

sage: bar_chart([-2,8,-7,3], rgbcolor=(1,0,0)).show(axes=False) # These are equivalent 

""" 

dl = len(datalist) 

#if dl > 1: 

# print "WARNING, currently only 1 data set allowed" 

# datalist = datalist[0] 

if dl == 3: 

datalist = datalist+[0] 

#bardata = [] 

#cnt = 1 

#for pnts in datalist: 

#ind = [i+cnt/dl for i in range(len(pnts))] 

#bardata.append([ind, pnts, xrange, yrange]) 

#cnt += 1 

 

g = Graphics() 

g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 

#TODO: improve below for multiple data sets! 

#cnt = 1 

#for ind, pnts, xrange, yrange in bardata: 

#options={'rgbcolor':hue(cnt/dl),'width':0.5/dl} 

# g._bar_chart(ind, pnts, xrange, yrange, options=options) 

# cnt += 1 

#else: 

ind = list(range(len(datalist))) 

g.add_primitive(BarChart(ind, datalist, options=options)) 

if options['legend_label']: 

g.legend(True) 

return g