简体   繁体   中英

I want to add an existing graph from matplotlib to my wxpython GUI

i wrote a function, that draws a graph from given values with matplotlib. i wanted to add the graph to my GUI, that is in another class, just by calling the graph and placing it the top left. I have searched for a while, but havent found a good solution yet. How could i achieve this?

The code of the graph:

def draw_graph(y_money, x_date):


counter = -1
now_money = []

for value in y_money:

    if counter >= 0:
        before_value = now_money[-1]
        now_value = before_value + y_money[counter+1]
        now_money.append(now_value)
        counter += 1

    else:
        now_money.append(y_money[0])
        counter += 1
plt.rcParams['toolbar'] = 'None'
plt.plot(now_money, "lime", label="Money")

before = 0
counter_2 = 0

#  Show if before value was lower than now value(red/green)
for element in now_money:

    if element >= before:
        plt.scatter(x_date[counter_2], element, c="green", s=50)
        counter_2 += 1
        before = element

    else:
        plt.scatter(x_date[counter_2], element, c="red", s=50)
        counter_2 += 1
        before = element

x_tick = []
for item in x_date:
    try:
        x_tick.append(item[:3])
    except IndexError:
        x_tick.append("")

plt.xticks(np.arange(7), x_tick)
plt.legend()
plt.show()

Since you have not specified where (in terms of wxWidgets) you are trying to add your plot on your gui, I will try to give a general answer. In my applications I use wx.Panel as the basis or canvas of the matplotlib figures.

Inside the panel you can create a figure and plot with your own function. As you can see from the code, this is a class based implementation.

import wx
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

class PlotPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
        # self.fig = Figure((10, 4), 75)
        sizer = wx.BoxSizer()
        sizer.Add(self, 1, wx.EXPAND)
        self.Parent.SetSizer(sizer)
        # self.fig = Figure(facecolor='blue')
        self.fig = Figure()
        self.canvas = FigureCanvasWxAgg(self, -1, self.fig)
        # Create toolbar for the figure
        self.toolbar = Toolbar(self.canvas)  # matplotlib toolbar
        self.toolbar.Realize()
        # Now put all into a sizer
        sizer = wx.BoxSizer(wx.VERTICAL)
        # This way of adding to sizer allows resizing
        sizer.Add(self.canvas, 1, wx.EXPAND | wx.ALL | wx.CENTER | wx.CENTER | wx.GROW)
        # Best to allow the toolbar to resize!
        sizer.Add(self.toolbar, 0, wx.EXPAND | wx.ALL | wx.GROW)
        self.SetSizer(sizer)
        self.Fit()

    def GetToolBar(self):
        # You will need to override GetToolBar if you are using an
        # unmanaged toolbar in your frame
        return self.toolbar

    def onEraseBackground(self, evt):
        # this is supposed to prevent redraw flicker on some X servers...
        pass

    def draw_graph(self, y_money, x_date):
        """ You can access above parameters and plot
    your graph """
    # Your own plotting function definition

This is partly my implementation using the source from this old scipy wiki link . Some of the stuff inside the init function might be useless for your application like toolbar etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM