底图上的ax.transAxes matplotlib不起作用

时间:2014-01-19 10:04:35

标签: python matplotlib matplotlib-basemap

我正在做地图情节使用底图,我想要一个图形上的一些艺术家(例如:多边形)获得变换(缩放时)并应用transform = ax.transAxes但是我不工作而我不能明白为什么。这是代码:

import pygtk
pygtk.require('2.0')
import gtk

import sys

import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from mpl_toolkits.basemap import Basemap

from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
from matplotlib.backends.backend_gtkagg import NavigationToolbar2GTKAgg as NavigationToolbar
from matplotlib.patches import Polygon

class CanvasFrame(FigureCanvas):

    def __init__(self):
        self.figure = Figure(dpi=75)
        FigureCanvas.__init__(self, self.figure)
        self.plot_map()      
    def plot_map(self):
        self.ax = self.figure.add_subplot(111)
        self.map = Basemap(projection='mill',resolution = 'l', llcrnrlon=103.5,llcrnrlat=-6.25,urcrnrlon=107.,urcrnrlat=-3.70,ax = self.ax)
        self.map.drawcoastlines()
        self.map.drawcountries()
        self.map.drawmapboundary()
        self.map.fillcontinents(color='coral', lake_color='aqua')
        self.map.drawmapboundary(fill_color='aqua')
        self.lons = [104.1739913, 105.3069591, 105.7375288]
        self.lats = [-5.1845939, -5.1163347, -5.3127146]
        self.draw_bus_colorbars(self.lons, self.lats)
    def draw_bus_colorbars(self, lons, lats):
        for i in range(len(lons)):
             self._lonpoly = [lons[i]-0.03, lons[i]+0.03, lons[i]+0.03, lons[i]-0.03]
             self._latpoly = [lats[i]+0.03, lats[i]+0.03, lats[i]+0.2, lats[i]+0.2]
             self.draw_polygon(self._lonpoly, self._latpoly)
    def draw_polygon(self, lons, lats):
        self.lonpoints, self.latpoints = self.map(lons, lats)
        self.lonlatpoly = zip(self.lonpoints, self.latpoints)
        self.poly = Polygon( self.lonlatpoly, True, transform=self.ax.transAxes, facecolor='grey', alpha=0.4)
        self.ax.add_patch(self.poly)

class Main(object):
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("PowerSAgtk")
        width = min(1024, int(0.8 * float(gtk.gdk.screen_width())))
        height = min(768, int(0.8 * float(gtk.gdk.screen_height())))
        self.window.set_default_size(width, height)
        self.window.connect('delete_event', self.destroyevent)
        self.vbox = gtk.VBox()
        self.window.add(self.vbox)
        self.canvas = CanvasFrame()
        self.toolbar = NavigationToolbar(self.canvas, self.window)
        self._frame = gtk.Frame()
        self._frame.add(self.toolbar)
        self.paned = gtk.HPaned()
        self.vbox.pack_start(self.paned)
        self.paned.pack1(self._frame, False, False)
        self.paned.pack2(self.canvas, True, True)
        self.paned.set_position(30)
        self.window.show_all()
        gtk.main()
    def destroyevent(self, *args):
        self.window.hide()
        if gtk.main_level() > 0:
            gtk.main_quit()
        print 'Selamat Tinggal!'
        sys.exit(0)

def app():
    powersa = Main()
    try:
        gtk.main()
    except KeyboardInterrupt:
        powersa.destroyevent()

if __name__ == '__main__':
    app()

我希望你能帮助我!

0 个答案:

没有答案