- Timestamp:
- 01/02/10 13:11:23 (11 years ago)
- Location:
- qesdi/linplot/trunk
- Files:
-
- 7 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
qesdi/linplot/trunk/src/linplot/plot.py
r6329 r6446 12 12 from linplot.range import Range 13 13 14 from linplot.config import config 15 16 from linplot.layout_manager import LayoutManager, BASE_AXIS_POSITION, BASE_LEGEND_POSITION 17 14 18 log = logging.getLogger(__name__) 15 16 17 18 #AXIS_POSITION = [0.1, 0.15, 0.7, 0.7]19 #LEGEND_POSITION = [0.82, 0.15, 0.15, 0.7]20 21 # one row22 BASE_AXIS_POSITION = [0.1, 0.20, 0.8, 0.7]23 BASE_LEGEND_POSITION = [0.1, 0.05, 0.8, 0.05]24 25 # two rows26 #AXIS_POSITION = [0.1, 0.25, 0.8, 0.65]27 #LEGEND_POSITION = [0.1, 0.05, 0.8, 0.1]28 29 ##three rows30 #AXIS_POSITION = [0.1, 0.30, 0.8, 0.6]31 #LEGEND_POSITION = [0.1, 0.05, 0.8, 0.15]32 19 33 20 COLORS = ['red', 'blue', 'yellow', 'green', 'orange', 'purple', 'cyan', 'black', 'brown', 'pink', 'grey', 'lightblue', 'darkblue', 'lightgreen', 'darkgreen'] … … 39 26 self.size = size 40 27 self.dpi = dpi 28 29 41 30 42 31 self._xRange = None … … 52 41 kwargs.setdefault('label', 'Line #%s' % (nlines,)) 53 42 kwargs.setdefault('color', COLORS[nlines % len(COLORS)]) 54 43 log.debug("kwargs = %s" % (kwargs,)) 44 55 45 if utils.isString(xdata[0]): 56 46 self._plotIndexXAxis(xdata, ydata, kwargs) … … 107 97 108 98 def saveImage(self, outputFile): 109 110 if len(self._ax.lines) == 0: 111 raise Exception("Trying to create a plot with no lines.") 112 113 self._scaleAxes() 114 self._populateLegend() 115 im = utils.figureToImage(self._fig) 99 im = self.getImage() 116 100 im.save(outputFile) 117 101 … … 120 104 raise Exception("Trying to create a plot with no lines.") 121 105 106 lm = LayoutManager(utils.Renderer, self.dpi, self.size[0], self.size[1]) 107 122 108 self._scaleAxes() 123 self._populateLegend() 109 110 self._populateLegend(lm) 111 112 lm.fitPlotInAxis(self._ax) 113 124 114 im = utils.figureToImage(self._fig) 125 115 return im … … 130 120 131 121 def _makeAxes(self): 122 # 123 # a = self._fig.add_axes(BASE_AXIS_POSITION, 124 # xticks=[], yticks=[], frameon=True) 132 125 return self._fig.add_axes(BASE_AXIS_POSITION, frameon=True) 133 126 134 127 def _makeLegendAxes(self): 135 return self._fig.add_axes(BASE_LEGEND_POSITION, xticks=[], yticks=[], frameon=False) 128 129 # a = self._fig.add_axes(BASE_LEGEND_POSITION, 130 # xticks=[], yticks=[], frameon=True) 131 # 132 # for loc, spine in a.spines.iteritems(): 133 # spine.set_edgecolor('b') 134 135 legAx = self._fig.add_axes(BASE_LEGEND_POSITION, xticks=[], yticks=[], frameon=False) 136 137 return legAx 136 138 137 139 def _scaleAxes(self): … … 158 160 self._ax.set_ylim(float(self._yRange.minimum), float(self._yRange.maximum)) 159 161 160 def _populateLegend(self): 161 162 log.debug("self._ax.lines = %s" % (self._ax.lines,)) 162 def _populateLegend(self, lm): 163 163 164 164 handles, labels = self._ax.get_legend_handles_labels() 165 log.debug("len(handles) = %s" % (len(handles),))166 165 167 ncol = len(handles) if len(handles) < 3 else 3 168 maxLabelLen = max([len(x) for x in labels]) 169 log.debug("labels = %s" % (labels,)) 170 log.debug("maxLabelLen = %s" % (maxLabelLen,)) 171 172 if maxLabelLen > 15 and ncol > 2: 173 ncol = 2 174 175 if maxLabelLen > 30 and ncol > 1: 176 ncol = 1 177 178 log.debug("ncol = %s" % (ncol,)) 166 ncol = lm.calculateLegendNcol(labels) 179 167 180 168 leg = self._legendAx.legend(handles, labels, ncol=ncol, 181 loc= 2, mode='expand', borderaxespad=0)169 loc=8, mode='expand', borderaxespad=0) 182 170 183 nlines = len(self._ax.lines) - 1 184 nrow = math.ceil(float(nlines)/ncol) 171 # set the legend text font 185 172 186 axisPos, legPos = self.getAxisPositions(nrow) 173 legendFont = config['LegendFont'] 174 [ legendFont.setProperties(lt) for lt in leg.get_texts()] 187 175 188 self._ax.set_position(axisPos) 189 self._legendAx.set_position(legPos)176 177 lm.setAxisPositions(labels, ncol, leg, self._legendAx, self._ax) 190 178 191 179 vp = leg._legend_box._children[-1]._children[0] … … 193 181 194 182 return leg 195 196 197 def getAxisPositions(self, nrow):198 199 rowHeight = 0.05200 shift = rowHeight * (nrow - 1)201 202 axisPos = [0.0] * 4203 axisPos[0] = BASE_AXIS_POSITION[0]204 axisPos[1] = BASE_AXIS_POSITION[1] + shift205 axisPos[2] = BASE_AXIS_POSITION[2]206 axisPos[3] = BASE_AXIS_POSITION[3] - shift207 208 legPos = [0.0] * 4209 legPos[0] = BASE_LEGEND_POSITION[0]210 legPos[1] = BASE_LEGEND_POSITION[1]211 legPos[2] = BASE_LEGEND_POSITION[2]212 legPos[3] = BASE_LEGEND_POSITION[3] + shift213 214 return axisPos, legPos215 216 183 217 184 def setXRange(self, minVal, maxVal=None): -
qesdi/linplot/trunk/src/linplot/utils.py
r6318 r6446 12 12 13 13 from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas 14 from matplotlib.backends.backend_agg import RendererAgg as Renderer 14 15 15 16 from linplot.image_import import Image -
qesdi/linplot/trunk/tests/linplot_tests/draw/draw_data.py
r6318 r6446 11 11 from linplot_tests.draw import getDrawOutputDir, SHORT_LOG_FORMAT_STRING 12 12 13 def draw_data( ):13 def draw_data(s=None): 14 14 15 15 fileName = 'data.png' … … 29 29 print "Wrote %s" % (outputFile) 30 30 31 32 31 if __name__ == '__main__': 33 32 -
qesdi/linplot/trunk/tests/linplot_tests/draw/draw_named_index.py
r6318 r6446 11 11 from linplot_tests.draw import getDrawOutputDir, SHORT_LOG_FORMAT_STRING 12 12 13 def draw_named_index(): 13 from common import getData, seed 14 import random 15 log = logging.getLogger(__name__) 16 17 def draw_named_index_short(): 14 18 15 fileName = 'named_index .png'19 fileName = 'named_index_short.png' 16 20 outputDir = getDrawOutputDir() 17 21 outputFile = os.path.join(outputDir, fileName) … … 47 51 print "Wrote %s" % (outputFile) 48 52 53 def draw_named_index_long(): 54 55 56 fileName = 'named_index_long.png' 57 outputDir = getDrawOutputDir() 58 outputFile = os.path.join(outputDir, fileName) 59 60 l = [ "scheme with long name %s" % (x,) for x in ['A','B','C','D','E','F' ]] 61 62 plt = Plot() 63 nlines = 3 64 65 # set the order of the labels to be used 66 plt.addXAxisIndexValues(l) 67 68 for i in range(nlines): 69 nItems = random.randint(2, len(l)) 70 71 xdata = numpy.array(random.sample(l, nItems)) 72 xdata.sort() 73 ydata = getData(len(xdata)) 74 label="model number %s" % (i,) 75 log.debug("label= %s, xdata = %s, ydata = %s" % (label, xdata, ydata,)) 76 plt.draw(xdata, ydata, label=label) 77 78 plt.setXLabel('X - year') 79 plt.setYLabel('Y - temp') 80 81 plt.saveImage(outputFile) 82 print "Wrote %s" % (outputFile) 83 84 def draw_named_index(s=None): 85 seed(s) 86 draw_named_index_short() 87 draw_named_index_long() 49 88 50 89 if __name__ == '__main__': 51 90 91 52 92 logging.basicConfig(level=logging.DEBUG, format=SHORT_LOG_FORMAT_STRING) 53 93 draw_named_index() -
qesdi/linplot/trunk/tests/linplot_tests/draw/draw_trig_functions.py
r6161 r6446 11 11 from linplot_tests.draw import getDrawOutputDir, SHORT_LOG_FORMAT_STRING 12 12 13 def draw_trig_functions( ):13 def draw_trig_functions(s=None): 14 14 15 15 fileName = 'trig_functions.png'
Note: See TracChangeset
for help on using the changeset viewer.