Search code examples
pythonmatplotlibipywidgets

Dynamic ipywidgets function for matplotlib does not print the plot


I am using this tutorial and want to create a basic interactive scatterplot with n-columns.

My data looks like:

psm pb  procedures
36  999 33
25  567 34
24  352 20
45  456 24

My code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widgets
import warnings
warnings.filterwarnings("ignore")
%matplotlib widget

def create_scatter(feature1, feature2):
    with plt.style.context("ggplot"):
        fig = plt.figure(figsize=(8,4))

        plt.scatter(x = psm_df[feature1],
                y = psm_df[feature2],
                s=20
               )

        plt.xlabel(feature1.capitalize())
        plt.ylabel(feature2.capitalize())

        plt.title("%s vs %s"%(feature1.capitalize(), feature2.capitalize()))

 widgets.interact(create_scatter, feature1=psm_df.columns, feature2=psm_df.columns);

Filters appear, but the plot does not

enter image description here

I thought there is an error, but I cannot reproduce a single plot from that tutorial, they are not printed too. What is wrong? Thanks!


Solution

  • Interestingly enough, the problem is here.

    Please make a note that we have used matplotlib with widget extension by calling jupyter magic command %matplotlib widget. The reason behind this is that we want a matplotlib plot as a widget that can be easily linked to other widgets and easily modified.

    I removed # %matplotlib widget and it worked.

    enter image description here