source: qesdi/linplot/trunk/tests/linplot_tests/draw/layout_test.py @ 6446

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/linplot/trunk/tests/linplot_tests/draw/layout_test.py@6446
Revision 6446, 2.6 KB checked in by pnorton, 11 years ago (diff)

Made several changes to try and improve the layout of the plot. Also introduced rotating axis labels to avoid labels being drawn on top of each other.

Line 
1'''
2Created on 29 Jan 2010
3
4@author: pnorton
5'''
6from matplotlib.backends.backend_cairo import RendererCairo as Renderer
7from matplotlib.backends.backend_cairo import FigureCanvasCairo as FigureCanvas
8from matplotlib.figure import Figure
9import matplotlib
10import numpy
11import logging
12
13import os
14
15from linplot_tests.draw import getDrawOutputDir, SHORT_LOG_FORMAT_STRING
16from linplot_tests.draw.common import seed, getData, getLabel
17
18from linplot.layout_manager import AxisPosition, Padding, LayoutManager
19
20COLORS = ['red', 'blue', 'yellow', 'green', 'orange', 'purple', 'cyan', 'black', 'brown', 'pink', 'grey', 'lightblue', 'darkblue', 'lightgreen', 'darkgreen']
21
22def layoutTest(s=None):
23   
24    seed(s)
25   
26    fileName = 'layout_manager_test.png'
27    outputDir = getDrawOutputDir()
28    outputFile = os.path.join(outputDir, fileName)
29   
30    nLines = 8
31   
32    width=1200;height=950;dpi=100;transparent=False
33   
34    maxAxisPosition = AxisPosition(0.05, 0.05, 0.95, 0.95)
35   
36    figsize=(width / float(dpi), height / float(dpi))
37    fig = Figure(figsize=figsize, dpi=dpi, facecolor='w', frameon=(not transparent))
38   
39    a = fig.add_axes(maxAxisPosition.getLocationAndSize(), 
40                     xticks=[], yticks=[], frameon=True)
41   
42   
43    a.patch.set_facecolor('r')
44    axes = fig.add_axes(maxAxisPosition.getLocationAndSize(), 
45                        frameon=True)
46    print "axes.get_position() = ", axes.get_position()
47   
48    axes.set_title(getLabel(20,50))
49    axes.set_xlabel(getLabel(10,30))
50    axes.set_ylabel(getLabel(10,30))
51           
52    xdata = [1,2,3,4,5,6,7,8,9]
53   
54    for i in range(nLines):
55        ydata = getData(len(xdata))
56        kwargs = {'label':getLabel(10,20), 'color':COLORS[i]}
57        axes.add_line(matplotlib.lines.Line2D(xdata, ydata, **kwargs))
58   
59    xlabels = [getLabel(12,12)  for i in range( len(xdata) + 1 )]
60    print "xlabels = ", xlabels
61       
62    locations = numpy.arange(len(xlabels))
63    print "locations = ", locations
64    loc = matplotlib.ticker.FixedLocator(locations)
65    axes.get_xaxis().set_major_locator(loc)
66   
67    axes.set_xticklabels(xlabels)
68   
69    lm = LayoutManager(Renderer, dpi, width, height)
70   
71    lm.fitPlotInAxis(axes)
72   
73    axes.autoscale_view(tight=True)
74   
75    canvas = FigureCanvas(fig)
76    canvas.print_figure(outputFile, dpi=fig.get_dpi(), facecolor=fig.get_facecolor(), edgecolor=fig.get_edgecolor())
77    print "wrote %s" % (outputFile,)
78   
79    print [ l.get_position() for l in axes.get_xticklabels()]
80if __name__ == '__main__':
81
82   
83    logging.basicConfig(level=logging.DEBUG, format=SHORT_LOG_FORMAT_STRING)
84    layoutTest()   
Note: See TracBrowser for help on using the repository browser.