在WxPython面板

时间:2015-08-01 04:57:19

标签: python matplotlib wxpython seaborn kernel-density

我想问一下如何在wxPython面板中嵌入一个海盗形象。

与此post类似,我想在wxPython面板中嵌入一个外部数字。根据Seaborn的kdeplot函数,我希望我的wxPython GUI的特定面板根据高斯内核的带宽值绘制数据的密度轮廓,以及散点图的数据点。以下是我希望在面板中绘制的示例:example

到目前为止,我已经设法从wxPython小组的单独数字中得到了我想要的内容。是否有可能在wxPython小组中嵌入一个seaborn情节或者应该找到另一种方式实现我想要的东西?

以下是我的代码的特定部分,以便在需要时生成绘图:

import seaborn as sns
import numpy as np

fig = self._view_frame.figure

data = np.loadtxt(r'data.csv',delimiter=',')
ax = fig.add_subplot(111)
ax.cla()
sns.kdeplot(data, bw=10, kernel='gau',  cmap="Reds")
ax.scatter(data[:,0],data[:,1], color='r')

fig.canvas.draw()

这部分代码在wxPython面板中绘制了散乱的数据点,并为密度等值线创建了一个外部图形。但是,如果我尝试ax.sns.kdeplot(...),我会收到错误

  

属性错误:AxesSubplot对象没有属性.sns

我不知道我是否可以在wxPython小组中嵌入Seaborn人物,或者我应该尝试以另一种方式实施它。有什么建议吗?

提前致谢。

2 个答案:

答案 0 :(得分:2)

我对wxPython一无所知,但如果你想在特定的轴上绘图,请使用ax关键字参数。

答案 1 :(得分:2)

我从未使用过Seaborn,但我猜是因为文档说" Seaborn是一个基于matplotlib"的Python可视化库,你可以使用名为FigureCanvasWxAgg的MPL类。

以下是在wxPython中嵌入MPL数据的示例代码。

import numpy as np
import wx

import matplotlib
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg

import seaborn

class test(wx.Frame):

    def __init__(self):

        wx.Frame.__init__(self, None, title='Main frame')

        # just a normal MPL "Figure" object
        figure = Figure(None)
        # Place a widget to hold MPL figure. no sizer because this is the only widget
        fc = FigureCanvasWxAgg(self, -1, figure)

        # your plotting code here, this can be sns calls i think
        subplot = figure.add_subplot(111)
        subplot.plot(np.arange(10))

        # Lastly show them
        self.Show()


if __name__ == '__main__':

    app = wx.App(0)
    testframe = test()
    app.MainLoop()

你可能只是用sns的东西替换绘图代码,并确保在"图"来自MPL的对象。

PS。出于兴趣,我pip安装它,只是导入seaborn已经改变了MPL的风格。所以,它似乎有效。由于matplotlib.use调用,您将需要在MPL导入后导入seaborn。

enter image description here