wxpython 中未显示由 matplotlib 和 cartopy 绘制的简单界面
An simple interface plotted by matplotlib and cartopy isn't showed in wxpython
我完全是wxpython.The的新手,以下代码显示了一个简单的情节:
#-*-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中显示,但是它没有work.The下面的代码是我有的 tried.It不需要修改代码,因为可能有很多错误的代码found.I 只是想确定我试过的真相。
#-*-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()
可能问题比较混乱,或者表达了一些东西unclear.These因为没有answer.Because这很简单question.Finally,我用下面的代码解决它:
#-*-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()
我完全是wxpython.The的新手,以下代码显示了一个简单的情节:
#-*-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中显示,但是它没有work.The下面的代码是我有的 tried.It不需要修改代码,因为可能有很多错误的代码found.I 只是想确定我试过的真相。
#-*-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()
可能问题比较混乱,或者表达了一些东西unclear.These因为没有answer.Because这很简单question.Finally,我用下面的代码解决它:
#-*-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()