An simple interface plotted by matplotlib and cartopy isn't showed in wxpython

这一生的挚爱 提交于 2020-01-05 05:46:07

问题


I'm completely a newcomer in wxpython.The following codes show a simple plot:

#-*-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() 

I have tried to make it showed in wxpython,but it doesn't work.The following codes are what i have tried.It's unnecessary to revise the codes,because many wrong codes may be found.I just want to identify the truth i have tried.

#-*-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:


Maybe the question is confused,or something are expressed unclear.These cause no answer.Because it's an easy question.Finally, i resolve it with following codes:

#-*-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()


来源:https://stackoverflow.com/questions/49893460/an-simple-interface-plotted-by-matplotlib-and-cartopy-isnt-showed-in-wxpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!