How to devide wx and matplotlib logic

hi,
i am trying to separate the logic of matplotlib and wx gui elements. means i dont want to have plotting logic in wxpanel. so calculation which it based on can be done separetly.

in the sample app below, a panel is having 3 sizers…
one of the sizer is having the canvas of figure…
so far i can return the fig object at the from the calc. part, but cannot find a way to add to current panel.

i tried many combinations from stackovf but cant find the right answer. .
any suggestions?

panel_3_support.py (614 Bytes) panel_3.py (3.6 KB)

wxGlade has some matplotlib examples.
Have a look here: https://github.com/wxGlade/wxGlade/tree/master/examples

Hi, vink

The self.canvas is actually a wx.Window object and should not be recreated.
If I would fix this,

  1. Pass the figure object as an argument of z_method
-def z_method():
-    myfig = Figure()
+def z_method(myfig):
  1. Call the method and just draw
    def third_value(self, event):
        ps.z_method(self.figure)
        self.canvas.draw()
1 Like

did you have a look at wxmplot ?

thx, this is working for sample app. further qs.,
is it possible to call z_method with additional parameters and still get figure in return?
like,

i am trying,

class myfigure_1():

	def y_method(myfig, abc):
		abc = abc
		
		def z_method(myfig):
            .....
            .....
			return myfig
        return z_method

this is because plotting logic needs co-ordinates which are coming from y_method.

yes knew about it… but wanted to get some foundation on matplotlib since its parent lib…

checked it out no ex. comes closer to my scenario, but will have another look throughly … thx

It’s hard for me to tell what you want your code to do. It seems you are creating and even re-creating FigureCanvas() instances, but may not be fully drawing them. Your third_value() method seems to create an Axes3D and put into a new FigureCanvas (overwriting the value of self.canvas created in MyFrame1.__init__(), but not used. I would guess that you want to make a different figure / figurecanvas and Panel and show that somewhere else?

As da-dada mentioned, the wxmplot library might be useful. If not directly, then perhaps as a fair amount of working, testable code that gives examples of how to combine matplotlib with wx effectively.

In this case, I don’t think you need to define local z_method function in y_method method. To share data between y_method and z_method, your model would be:

class myfigure_1():
    def _init__(self):
        ...
        self.X = X
        self.Y = Y
        self.Z = Z
    
    def y_method(myfig, abc):
        ax = Axes3D(myfig)
        ax.plot_wireframe(self.X, self.Y, self.Z)
    
    def z_method(myfig):
        ax = myfig.axes[0]
        ...

I think axes object is more appropriate than figure object as an argument of plot functions.
In my understanding, axes is a real plotting field of the artists, while figure is just a holder of one or more axes and provides some helper functions.

Well, the wxGlade examples are complete and runnable and are showing how to integrate wxPython and matplotlib, including some more advanced topics.
Your code is too far away from doing useful things as to expect someone else to fix it. You should start with looking at code that is actually working and then adapt things to your needs.

1 Like