Using matplotlib.pyplot in iPython Notebook

201 views Asked by At

I'm brand new to Python, and I was just plot a really 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 graph of a diagonal line, but when I run it, nothing prints at all.

If I delete

    plt.show()

I get this as output:

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

How do I get the plot to show?

1

There are 1 answers

2
JamesD On

I am also new to MatPlotLib but this sample got me a simple sin wave graph

The key was the call to

plt.draw()

the complete code is 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')