How do I make matplotlib generate a single plot instead of a plot for each data point?
<p>I am trying to generate three plots, each using the same inputs. 
When I run my code I generate a plot for each x input instead of three plots consisting of all of their data points.</p>

<p>See my code below:</p>

<pre><code>xlist = np.linspace(0, 2.5)

for name, f, df in zip(func_names, funcs, diff_funcs):
 for x in xlist:
  plt.plot(diff(f, x, h=0.01), 'bs', forwdiff(f, x, h=0.01), 'g^')
  plt.title(name)
  plt.xlabel('x')
  plt.ylabel('f(x)')
  bluesq = plt.Line2D([], [], color='blue', marker='s',
                      markersize=15, label='Centered Difference')
  greentr = plt.Line2D([], [], color='green', marker='^',
                      markersize=15, label='Forward Difference')
  l1 = plt.legend(handles = [bluesq], loc=1)
  l2 = plt.legend(handles = [greentr], loc=4)
  plt.gca().add_artist(l1)
  plt.gca().add_artist(l2)
  plt.show()
</code></pre>

<p>Full code:</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt

print("---Forward Diff---")

def forwdiff(f, x, h=1e-5):
     """
     Returns the forward derivative of a function f
     """
     return 1 / (h) * (f(x + h) - f(x))

from math import exp, cos, sin, pi, log


f1 = lambda x: exp(-2 * x ** 2)
df1 = lambda x: -4 * x * exp(-2 * x ** 2)
f2 = lambda x: cos(x)
df2 = lambda x: -sin(x)
f3 = lambda x: sin(x)
df3 = lambda x: cos(x)

funcs = [f1, f2, f3]
diff_funcs = [df1, df2, df3]
func_names = ['exp(-2x^2)', 'cos(x)', 'sin(x)']
values = [2, 0.6, 0.6]

print '%10s %8s %8s %8s' % ('function', 'exact', 'approx', 'error')

for name, f, df, x in zip(func_names, funcs, diff_funcs, values):
    exact = df(x)
    approx = forwdiff(f, x, h=0.01)
    error = abs(exact - approx)
    print '%10s %.6f %.6f %.6f' % (name, exact, approx, error)


def test_forwdiff():
    success = 6 - forwdiff(lambda x: x**2, 3, h=0.01) &lt; 0.00000000001
    msg = "test_forwdiff failed"
    assert success, msg

print("---Centered Diff---")


def diff(f, x, h=1e-5):
     """
     Returns the derivative of a function f
     """
     return 1 / (2 * h) * (f(x + h) - f(x - h))

from math import exp, cos, sin, pi, log

f1 = lambda x: exp(-2 * x ** 2)
df1 = lambda x: -4 * x * exp(-2 * x ** 2)
f2 = lambda x: cos(x)
df2 = lambda x: -sin(x)
f3 = lambda x: sin(x)
df3 = lambda x: cos(x)

funcs = [f1, f2, f3]
diff_funcs = [df1, df2, df3]
func_names = ['exp(-2x^2)', 'cos(x)', 'sin(x)']
values = [2, 0.6, 0.6]

print '%10s %8s %8s %8s' % ('function', 'exact', 'approx', 'error')

for name, f, df, x in zip(func_names, funcs, diff_funcs, values):
    exact = df(x)
    approx = diff(f, x, h=0.01)
    error = abs(exact - approx)
    print '%10s %.6f %.6f %.6f' % (name, exact, approx, error)


def test_diff():
    success = 6 - diff(lambda x: x**2, 3, h=0.01) &lt; 0.00000000001
    msg = "test_diff failed"
    assert success, msg

xlist = np.linspace(0, 2.5)

for name, f, df in zip(func_names, funcs, diff_funcs):
    for x in xlist:
     plt.plot(diff(f, x, h=0.01), 'bs', forwdiff(f, x, h=0.01), 'g^')
     plt.title(name)
     plt.xlabel('x')
     plt.ylabel('f(x)')
     bluesq = plt.Line2D([], [], color='blue', marker='s',
                          markersize=15, label='Centered Difference')
     greentr = plt.Line2D([], [], color='green', marker='^',
                          markersize=15, label='Forward Difference')
     l1 = plt.legend(handles = [bluesq], loc=1)
     l2 = plt.legend(handles = [greentr], loc=4)
     plt.gca().add_artist(l1)
     plt.gca().add_artist(l2)
     plt.show()
</code></pre>

<p>So, I found out that plot() needs to have the entire x_list and y_list as arguments.</p>

<p>which got me to this:</p>

<pre><code>    xlist = np.linspace(0, 2.5)

for name, f in zip(func_names, funcs):
    ylist = [forwdiff(f, x, h=0.01) for x in xlist]
    plt.plot(xlist, ylist, 'g^')

    ylist = [diff(f, x, h=0.01) for x in xlist]
    plt.plot(xlist, ylist, 'bs')
    plt.title(name)

plt.xlabel('x')
plt.ylabel('f(x)')
bluesq = plt.Line2D([], [], color='blue', marker='s',
                          markersize=15, label='Centered Difference')
greentr = plt.Line2D([], [], color='green', marker='^',
                          markersize=15, label='Forward Difference')
l1 = plt.legend(handles = [bluesq], loc=1)
l2 = plt.legend(handles = [greentr], loc=4)
plt.gca().add_artist(l1)
plt.gca().add_artist(l2)
plt.show()
</code></pre>

<p>This correctly plots all of the inputs onto one plot, but I was attempting to generate three plots. One for each f input. The code given calls f and df and generates a single plot for each, but plots them to the same window. How would I separate that plot into three different windows showing forwdiff and diff for each f?</p>

<p>Sorry if this is a stupid question, my background is not in computer science/programming.</p>

<p>I want to separate the plots into three plots of forwdiff and diff for each of exp(-2x2), cos(x), and sin(x).</p>

<p>So, I found out that plot() needs to have the entire x_list and y_list as arguments.</p>

<p>which got me to this:</p>

<pre><code>    xlist = np.linspace(0, 2.5)

for name, f in zip(func_names, funcs):
    ylist = [forwdiff(f, x, h=0.01) for x in xlist]
    plt.plot(xlist, ylist, 'g^')

    ylist = [diff(f, x, h=0.01) for x in xlist]
    plt.plot(xlist, ylist, 'bs')
    plt.title(name)

plt.xlabel('x')
plt.ylabel('f(x)')
bluesq = plt.Line2D([], [], color='blue', marker='s',
                          markersize=15, label='Centered Difference')
greentr = plt.Line2D([], [], color='green', marker='^',
                          markersize=15, label='Forward Difference')
l1 = plt.legend(handles = [bluesq], loc=1)
l2 = plt.legend(handles = [greentr], loc=4)
plt.gca().add_artist(l1)
plt.gca().add_artist(l2)
plt.show()
</code></pre>

<p>This correctly plots all of the inputs onto one plot, but I was attempting to generate three plots. One for each f input. The code given calls f and df and generates a single plot for each, but plots them to the same window. How would I separate that plot into three different windows showing forwdiff and diff for each f?</p>

<p>Sorry if this is a stupid question, my background is not in computer science/programming.</p>

<p>I want to separate the plots into three plots of forwdiff and diff for each of exp(-2x2), cos(x), and sin(x).</p>

python matplotlib plot