【问题标题】:An simple interface plotted by matplotlib and cartopy isn't showed in wxpythonwxpython中没有显示一个由matplotlib和cartopy绘制的简单界面
【发布时间】:2018-04-18 07:16:50
【问题描述】:

我完全是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()

【问题讨论】:

    标签: python matplotlib wxpython cartopy


    【解决方案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()
    

    【讨论】:

    • 显然,无花果是通过“FigureCanvas”添加到框架中的
    猜你喜欢
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 2021-08-10
    • 1970-01-01
    • 2017-03-14
    • 2021-04-03
    相关资源
    最近更新 更多