在对象函数中使用matplotlib绘图

时间:2018-03-05 19:58:49

标签: python matplotlib matplotlib-basemap

我有一个名为Map的对象,我想调用它的方法showBasemap()

地图对象

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.basemap import Basemap
    from datetime import datetime

    class Map(object):

        def __init__(self,data,sites):
            self.data = data
            self.sites = sites
            self.cmap = None
            self.vmin = None
            self.vmax = None
            print("UPDATE: Map object created with current data")

        def setupBasemap(self):
            baseMap = Basemap(projection='mill',lon_0=180)
            # plot coastlines, draw label meridians and parallels.
            baseMap.drawcoastlines()
            baseMap.drawparallels(np.arange(-90,90,30),labels=[1,0,0,0])
            baseMap.drawmeridians(np.arange(baseMap.lonmin,baseMap.lonmax+30,60),labels=[0,0,0,1])
            # fill continents 'coral' (with zorder=0), color wet areas 'aqua'
            baseMap.drawmapboundary(fill_color='aqua')
            baseMap.fillcontinents(color='coral',lake_color='aqua')
            # shade the night areas, with alpha transparency so the
            # map shows through. Use current time in UTC.
            date = datetime.utcnow()
            CS=baseMap.nightshade(date)
            plt.title('Day/Night Map for %s (UTC)' % date.strftime("%d %b %Y %H:%M:%S"))
            plt.show()
            print("UPDATE: Basemap Setup...")

main.py

    from Map import *
    import matplotlib.pyplot as plt

    def main():
        try:
            sites = []
            weather = [2,2,2]
            map = Map(weather,sites)
            map.setupBasemap()

    except IndexError:
        fmt = 'invalid file name'
        print(fmt.format(__file__.split('/')[-1]))

    if __name__ == "__main__":
        main()

我重组了我原来的帖子。我希望这可以解决问题...如果我调用一个具有plt功能的对象函数,我无法获得任何plt。我将方法中的代码复制到test.py文件中,它完美地运行。我很困惑为什么绘图不按照我的布局方式工作。

0 个答案:

没有答案