[英]Fitting matplotlib figure to wxpnel
I am trying to display a plot in a wxpanel so that when initialized it has 5 pixels gap from left top and right edges.我正在尝试在 wxpanel 中显示 plot ,以便在初始化时它与左上边缘和右边缘有 5 个像素的间隙。
I tried the code below but it looks like when the plot is created it sticks right to the right edge of the panel.我尝试了下面的代码,但看起来当 plot 创建时它会粘在面板的右边缘。 After resizing the gap appear.
调整大小后出现间隙。
# -*- coding: utf-8 -*-
import wx
#import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from relpermtable import RelPermTable as RelPermCl
class KrPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
main_sizer = wx.BoxSizer(wx.VERTICAL)
self.list_ctrl = wx.ListCtrl(
self, size = (-1, 300),
style = wx.LC_REPORT | wx.BORDER_SUNKEN
)
self.list_ctrl.InsertColumn(0, 'Sw', width=100)
self.list_ctrl.InsertColumn(1, 'Krw', width=100)
self.list_ctrl.InsertColumn(2, 'Krg', width=100)
self.list_ctrl.InsertColumn(3, 'Pc', width=100)
main_sizer.Add(self.list_ctrl, 0, wx.ALL | wx.EXPAND, 5)
self.SetSizer(main_sizer)
def load_KrPc_data(self, in_dfr):
for index, row in in_dfr.iterrows():
self.list_ctrl.InsertItem(index, str(row["Sg"]))
self.list_ctrl.SetItem(index, 1, str(row["Krg"]))
self.list_ctrl.SetItem(index, 2, str(row["Krw"]))
self.list_ctrl.SetItem(index, 3, str(row["Pc"]))
class PlotPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
def PlotKrPcData(self, df_in, in_swco, in_sgco):
right_sizer = wx.BoxSizer(wx.VERTICAL)
self.fig, self.ax1 = plt.subplots() # figsize=(5,5)
# self.ax2 = self.ax1.twinx()
self.ax1.plot(df_in['Sg'], df_in['Krg'], 'r-')
self.ax1.plot(df_in['Sg'], df_in['Krw'], 'g-')
# self.ax2.plot(new_df['Sg'], new_df['Pc'], 'b-')
self.ax1.plot((1-in_swco), 0, 'go')
self.ax1.annotate('1-SWL', ((1-in_swco), 0.05))
self.ax1.plot((in_sgco), 0, 'go', label = 'SGL')
self.ax1.annotate('SGL', (in_sgco, 0.05))
self.canvas = FigureCanvas(self, -1, self.fig)
right_sizer.Add(self.canvas, 0, wx.ALL | wx.EXPAND, 5)
self.SetSizer(right_sizer)
class KrFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None,
title='Gas Relative Permeability Editor', size=(900, 800))
self.sp = wx.SplitterWindow(self)
self.panel01 = KrPanel(self.sp)
self.panel02 = PlotPanel(self.sp)
self.sp.SplitVertically(self.panel01, self.panel02, 450)
self.create_menu()
self.Show()
def create_menu(self):
menu_bar = wx.MenuBar()
file_menu = wx.Menu()
open_file_menu_item = file_menu.Append(
wx.ID_ANY, 'Open File', 'Open a Relative Permeability File'
)
exit_menu_item = file_menu.Append(
wx.ID_EXIT, "Exit", "Exit the application"
)
menu_bar.Append(file_menu, '&File')
self.Bind(
event = wx.EVT_MENU,
handler = self.on_open_file,
source = open_file_menu_item
)
self.Bind(
event = wx.EVT_MENU,
handler = self.on_exit,
source = exit_menu_item
)
self.SetMenuBar(menu_bar)
def on_open_file(self, event):
title = "Choose a Relative Permeability file:"
dlg = wx.FileDialog(self, title, "", "",
"Eclipse include files (*.INC) | *.INC", style = wx.FD_DEFAULT_STYLE)
if dlg.ShowModal() == wx.ID_OK:
object = RelPermCl(dlg.GetPath())
new_df = object.load_KrPc_data()
new_sgco = object.calc_sgco()
new_swco = object.calc_swco()
# new_sgmax = object.calc_sgmax()
# new_swmax = object.calc_swmax()
# new_sgcr = object.calc_sgcr()
# new_swcr = object.calc_swcr()
self.panel01.load_KrPc_data(new_df)
self.panel02.PlotKrPcData(new_df, new_sgco, new_swco)
# print(new_df.head())
dlg.Destroy()
def on_exit(self, e):
self.Close()
if __name__ == '__main__':
app = wx.App(False)
frame = KrFrame()
app.MainLoop()
del app
I'd like to have 5 pixels gap on the right of the plot when the plot is created.创建 plot 时,我想在 plot 的右侧有 5 个像素的间隙。 But somehow the gap is created only after resize.
但不知何故,只有在调整大小后才会产生差距。 Is there a way to set plot size let's say to the size of the panel independently of panel size and specify border sizes?
有没有办法设置 plot 大小让我们说面板的大小独立于面板大小并指定边框大小?
Try to put a frame.SendSizeEvent() before the Show call.尝试在 Show 调用之前放置一个 frame.SendSizeEvent()。 If it doesn't work, try with wx.CallAfter(frame.SendSizeEvent)
如果它不起作用,请尝试使用 wx.CallAfter(frame.SendSizeEvent)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.