Using matplotlib.pyplot in iPython Notebook

I am new to Python and I just sketched a very simple graph in iPython Notebook.

    import matplotlib.pyplot as plt
    plt.plot([1,2,3,4])
    plt.show()

      

This is what I wrote, expecting a simple diagonal line plot, but when I run it, it prints nothing at all.

If I delete

    plt.show()

      

I get this as output:

    [<matplotlib.lines.Line2D at 0x109fcdb50>]

      

How can I display the plot?

+3


source to share


1 answer


I am also new to MatPlotLib but this sample got me a simple sine wave plot

The key was the challenge

plt.draw()



complete code below.

from math import sin
from matplotlib import pyplot as plt


def plot_update(x_data, y_data, lcl_my_title, lcl_x_lbl, lcl_y_lbl, legend):
    plt.grid(color='b', linestyle='--', linewidth=1)
    plt.ylabel(lcl_y_lbl)
    plt.xlabel(lcl_x_lbl)
    plt.title(lcl_my_title)
    plt.plot(x_data, y_data, label=legend)
    plt.legend(loc=3, prop={'size': 6})
    plt.draw()
    plt.show()
    return


t = range(0, 360, 1)
s = []

for x in t:
    s.append(sin(x))

print len(t)
print len(s)

plot_update(t, s, 'new graph', 'voltage', 'current', 'blah')

      

-1


source







All Articles