locked
How to Draw Plots in AureML studio while Jupyter notebook is removed from the studio? RRS feed

  • Question

  • I am doing the "Final Exam and survey section" of Data Science course. I need to draw Histograms, Box plots and Scatter Plots to answer the questions but the Jupyter notebook is removed from the AzureML studio (classic). How can I complete the course? I appreciate your help and guidance.
    Thursday, August 6, 2020 12:01 PM

All replies

  • I had been particularly looking for a good answer for the scenario where one thread is pumping data and we want Jupyter notebook to keep updating graph without blocking anything. After looking through about dozen or so related answers, here are some of the findings:

    Caution

    Do not use below magic if you want a live graph. The graph update does not work if the notebook uses below:

    %load_ext autoreload
    %autoreload 2
    You need below magic in your notebook before you import matplotlib:

    %matplotlib notebook
    Method 1: Using FuncAnimation

    This has a disadvantage that graph update occurs even if your data hasn't been updated yet. Below example shows another thread updating data while Jupyter notebook updating graph through FuncAnimation.

    %matplotlib notebook

    from matplotlib import pyplot as plt
    from matplotlib.animation import FuncAnimation
    from random import randrange
    from threading import Thread
    import time

    class LiveGraph:
        def __init__(self):
            self.x_data, self.y_data = [], []
            self.figure = plt.figure()
            self.line, = plt.plot(self.x_data, self.y_data)
            self.animation = FuncAnimation(self.figure, self.update, interval=1000)
            self.th = Thread(target=self.thread_f, daemon=True)
            self.th.start()

        def update(self, frame):
            self.line.set_data(self.x_data, self.y_data)
            self.figure.gca().relim()
            self.figure.gca().autoscale_view()
            return self.line,

        def show(self):
            plt.show()

        def thread_f(self):
            x = 0
            while True:
                self.x_data.append(x)
                x += 1
                self.y_data.append(randrange(0, 100))   
                time.sleep(1)  

    g = LiveGraph()
    g.show()
    Method 2: Direct Update

    The second method is to update the graph as data arrives from another thread. This is risky because matplotlib is not thread safe but it does seem to work as long as there is only one thread doing updates.

    %matplotlib notebook

    from matplotlib import pyplot as plt
    from matplotlib.animation import FuncAnimation
    from random import randrange
    from threading import Thread
    import time

    class LiveGraph:
        def __init__(self):
            self.x_data, self.y_data = [], []
            self.figure = plt.figure()
            self.line, = plt.plot(self.x_data, self.y_data)

            self.th = Thread(target=self.thread_f, daemon=True)
            self.th.start()

        def update_graph(self):
            self.line.set_data(self.x_data, self.y_data)
            self.figure.gca().relim()
            self.figure.gca().autoscale_view()

        def show(self):
            plt.show()

        def thread_f(self):
            x = 0
            while True:
                self.x_data.append(x)
                x += 1
                self.y_data.append(randrange(0, 100))  

                self.update_graph()

                time.sleep(1)  


    from live_graph import LiveGraph

    g = LiveGraph()
    g.show()
    Thursday, August 6, 2020 2:41 PM