在 WxPython 面板中嵌入 Seaborn 图

Embedding Seaborn plot in WxPython panel

请问如何在wxPython面板中嵌入seaborn人物

与此类似post, I want to embed an external figure in a wxPython panel. I would like a specific panel of my wxPython GUI to plot the density contours of my data based on bandwidth values of a Gaussian kernel, according to Seaborn's kdeplot function, along with a scatter plot of the data points. Here is an example of what I would like to be plotted in the panel:

直到现在,我已经设法在 wxPython panel.Is 的单独图中得到了我想要的东西,可以在 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(...),我会得到错误

Attribute Error: AxesSubplot object has not attribute .sns

我不知道我是否可以在 wxPython 面板中嵌入一个 Seaborn 图,或者我应该尝试以其他方式实现它。有什么建议吗?

提前致谢。

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

我从未使用过 Seaborn,但我猜是因为文档说 "Seaborn is a Python visualization library based on matplotlib",您或许可以使用名为 FigureCanvasWxAgg 的 MPL class。

这是在 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 的 "Figure" 对象上绘图。

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