Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding Seaborn plot in WxPython panel

I would like to ask how I could embed a seaborn figure in wxPython panel.

Similarly to this 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: example

Until now, I have managed to get what I want in a separate figure out of the wxPython panel.Is it possible to embed a seaborn plot in a wxPython panel or should find an alternative way to implement what I want?

Below is the specific part of my code that generates the plot in case it is needed:

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()

This part of the code plots in the wxPython panel the scattered data points and creates an external figure for the density contours. But, if I try ax.sns.kdeplot(...) I get the error

Attribute Error: AxesSubplot object has not attribute .sns

I don't know if I can embed a Seaborn figure in wxPython panel or I should try implement it in another way. Any suggestions?

Thanks in advance.

like image 443
user_jt Avatar asked Aug 01 '15 04:08

user_jt


People also ask

How to use Seaborn to plot distribution in Python?

Seaborn 1 Visualize Distributions With Seaborn. Seaborn is a library that uses Matplotlib underneath to plot graphs. ... 2 Install Seaborn. 3 Distplots. ... 4 Import Matplotlib. ... 5 Import Seaborn 6 Plotting a Displot 7 Plotting a Distplot Without the Histogram. ...

What is the difference between Seaborn and Matplotlib?

We use .swarmplot () function to plot swarn plot. Another difference that we can notice in Seaborn and Matplotlib is that working with DataFrames doesn’t go quite as smoothly with Matplotlib, which can be annoying if we doing exploratory analysis with Pandas.

How to plot categorical data with the help of Seaborn?

Explanation : This is the one of kind of scatter plot of categorical data with the help of seaborn. Categorical data is represented in x-axis and values correspond to them represented through y-axis. .striplot () function is used to define the type of the plot and to plot them on canvas using .

How do I install Seaborn on Linux?

Installation: The easiest way to install seaborn is to use pip. Type following command in terminal: OR, you can download it from here and install it manually. Explanation: This is the one kind of scatter plot of categorical data with the help of seaborn.


2 Answers

I don't know anything about wxPython but if you want to plot onto a specific axes use the ax keyword argument.

like image 148
mwaskom Avatar answered Sep 18 '22 22:09

mwaskom


I never used Seaborn but I guess because the doc says "Seaborn is a Python visualization library based on matplotlib", you can probably use MPL class called FigureCanvasWxAgg.

Here's a sample code for embedding a MPL figure in wxPython.

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()

You could probably just replace the plotting code with sns stuff and just make sure to plot on "Figure" object from MPL.

PS. Out of interest, I pip installed it and just importing seaborn already changed the style of MPL. So, it seems working. Because of matplotlib.use call, you will want to import seaborn after MPL imports.

enter image description here

like image 44
otterb Avatar answered Sep 17 '22 22:09

otterb