Compute y-value based on x-value and function - Python
<p>I'm currently trying to teach myself how to plot functions with matplotlib in Python, and I'm stuck on a rather simple task: <strong>Given a function f(x) = 2x +3, plot the coordinates in matplotlib.</strong> </p>

<p>I have solved this the hard way, through <em>manual</em> calculation, creating a list of both the x- and y-values for x-values from -2 to 3.</p>

<p><strong>The Hard Way</strong></p>

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

x = range(-2, 4)
y = [-1, 1, 3, 5, 7, 9]

plt.plot(x, y, 'ro')
plt.show()
</code></pre>

<p>This works!</p>

<p><strong>However</strong>, is there a way to quickly obtain the y-values, based on x-values (from -2 to 3) and the function?</p>

<pre><code>f(x) = 2x+3
</code></pre>

<p>Any help is highly appreciated!</p>

<p>Here's an easy way to plot your function within a certain range <code>[A,B]</code>:</p>

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


def graph(formula, x_range):
    x = np.array(x_range)
    y = formula(x)
    plt.plot(x, y)
    plt.show()


def f(x):
    return 2 * x + 3

A, B = -10, 10
graph(f, range(A, B))
</code></pre>

python function matplotlib range coordinates