由matplotlib和cartopy绘制的简单界面未在wxpython中显示

时间:2018-04-18 07:16:50

标签: python matplotlib wxpython cartopy

我完全是wxpython的新手。下面的代码显示了一个简单的情节:

#-*-coding:utf-8-*-

import matplotlib.pyplot as plt

import matplotlib.ticker as mticker
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER

from matplotlib.offsetbox import AnnotationBbox,OffsetImage
from PIL import Image

fig=plt.figure(figsize=(20,10))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
ax.stock_img()
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
                  linewidth=2, color='gray', alpha=15, linestyle='--')
gl.xlabels_top = False
gl.ylabels_left = False
gl.xlines = False
gl.xlocator = mticker.FixedLocator([-180, -45, 0, 45, 180])
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlabel_style = {'size': 15, 'color': 'gray'}
gl.xlabel_style = {'color': 'red', 'weight': 'bold'}

img=Image.open(r'E:\python_file\untitled\p.png')
imagebox=OffsetImage(img,zoom=0.05)
imagebox.image.axes=ax

ab=AnnotationBbox(imagebox,[55,10],pad=0,frameon=False)
ax.add_artist(ab)


plt.show() 

我试图在wxpython中显示它,但它不起作用。下面的代码是我试过的。没有必要修改代码,因为可能找到许多错误的代码。我只想识别我试过的事实。

#-*-coding:utf-8-*-

import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from matplotlib.offsetbox import AnnotationBbox,OffsetImage
from PIL import Image
import wx
class Canvas(wx.Panel):
    def __init__(self,parent):

            self.fig=plt.figure(figsize=(20,10))
            self.ax = plt.axes(projection=ccrs.PlateCarree())
            self.ax.coastlines()
            self.ax.stock_img()
            self.gl = self.ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
                              linewidth=2, color='gray', alpha=15, linestyle='--')
            self.gl.xlabels_top = False
            self.gl.ylabels_left = False
            self.gl.xlines = False
            self.gl.xlocator = mticker.FixedLocator([-180, -45, 0, 45, 180])
            self.gl.xformatter = LONGITUDE_FORMATTER
            self.gl.yformatter = LATITUDE_FORMATTER
            self.gl.xlabel_style = {'size': 15, 'color': 'gray'}
            self.gl.xlabel_style = {'color': 'red', 'weight': 'bold'}
    def draw(self):
            img=Image.open(r'E:\python_file\untitled\p.png')
            imagebox=OffsetImage(img,zoom=0.05)
            imagebox.image.axes=self.ax
            ab=AnnotationBbox(imagebox,[55,10],pad=0,frameon=False)
            self.ax.add_artist(ab)



if __name__ == "__main__":
    app = wx.App()
    fr = wx.Frame(None, title='test')
    panel = Canvas(fr)
    panel.draw()
    fr.Show()
    app.MainLoop()

1 个答案:

答案 0 :(得分:1)

也许问题很混乱,或者某些事情表达不清楚。这些都没有答案。因为这是一个简单的问题。最后,我用以下代码解决:

#-*-coding:utf-8-*-
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from matplotlib.offsetbox import AnnotationBbox,OffsetImage
from PIL import Image
import wx


from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
class Canvas(wx.Panel):
    def __init__(self,parent):
            wx.Panel.__init__(self, parent)
            self.fig=plt.figure()
            self.ax = plt.axes(projection=ccrs.PlateCarree())
            self.ax.coastlines()
            self.ax.stock_img()
            self.canvas = FigureCanvas(self, -1, self.fig)

            self.gl = self.ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
                              linewidth=2, color='gray', alpha=15, linestyle='--')
            self.gl.xlabels_top = False
            self.gl.ylabels_left = False
            self.gl.xlines = False
            self.gl.xlocator = mticker.FixedLocator([-180, -45, 0, 45, 180])
            self.gl.xformatter = LONGITUDE_FORMATTER
            self.gl.yformatter = LATITUDE_FORMATTER
            self.gl.xlabel_style = {'size': 15, 'color': 'gray'}
            self.gl.xlabel_style = {'color': 'red', 'weight': 'bold'}
            self.sizer = wx.BoxSizer(wx.VERTICAL)
            self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
            self.SetSizer(self.sizer)
            self.Fit()
    def draw(self):
            img=Image.open(r'E:\python_file\untitled\p.png')
            imagebox=OffsetImage(img,zoom=0.05)
            imagebox.image.axes=self.ax
            ab=AnnotationBbox(imagebox,[55,10],pad=0,frameon=False)
            self.ax.add_artist(ab)



if __name__ == "__main__":
    app = wx.App()
    fr=wx.Frame(None,title='test')
    panel=Canvas(fr)
    print('fine')
    panel.draw()
    fr.Show()
    print('just')
    app.MainLoop()