Hi.
I adjust the objects in the plot dynamically, but it only applies when the frame has been resized manually by mouse dragging.
I had got a similar problem long before and Refresh(), Update(), or something was helpful at that time as I remember and the old posts says. But it is not working now.
Here is the simplified code that adds and removes a marker by button clicked. As you can see, you have to resize the frame to see the change. (Only the first addition is shown immediately as you add)
How can I force update?
[Environment]
Windows 10
Python 3.9.6
wxPython 4.1.1
import wx
import numpy as np
from wx.lib.plot import PlotCanvas, PlotGraphics, PolyMarker
class myframe(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
self.x = []
self.y = []
self.marker = PolyMarker([])
self.graphics = PlotGraphics([self.marker])
self.canvas = PlotCanvas(self.panel)
self.canvas.Draw(self.graphics, xAxis=(-1, 11), yAxis=(-1, 11))
sizer_btn = wx.BoxSizer(wx.HORIZONTAL)
self.btn1 = wx.Button(self.panel, label='Add')
self.btn1.Bind(wx.EVT_BUTTON, self.add)
sizer_btn.Add(self.btn1, 0, wx.ALL, 3)
self.btn2 = wx.Button(self.panel, label='Remove')
self.btn2.Bind(wx.EVT_BUTTON, self.remove)
sizer_btn.Add(self.btn2, 0, wx.ALL, 3)
sizer.Add(self.canvas, 1, wx.EXPAND)
sizer.Add(sizer_btn, 0, wx.ALIGN_RIGHT)
self.panel.SetSizer(sizer)
def add(self, e):
self.x.append(len(self.x))
self.y.append(len(self.y))
self.my_redraw()
def remove(self, e):
if len(self.x) >= 1:
self.x.pop()
self.y.pop()
self.my_redraw()
def my_redraw(self):
points = np.array([self.x, self.y]).transpose().tolist()
self.marker.points = points
# Option 1 ------------
self.canvas.Redraw()
# ---------------------
# Option 2 ------------
# self.canvas.Clear()
# self.canvas.Draw(self.graphics, xAxis=(0, 10), yAxis=(0, 10))
# ---------------------
# Nothing worked here ---------------------
# self.canvas.Refresh()
# self.canvas.Update()
# self.canvas.Layout()
# self.panel.Refresh()
# self.panel.Update()
# self.panel.Layout()
# self.Refresh()
# self.Update()
# self.Layout()
# -----------------------------------------
if __name__ == '__main__':
app = wx.App()
frame = myframe()
frame.Show()
app.MainLoop()