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>The <code>lambda</code> function would be very useful in this situation. You can declare your function using <code>f = lambda x: 2*x + 3</code>. The <code>map</code> function would be a good way to obtain all of your <code>y</code> values in a simple way: <code>y = map(f, x)</code>.</p>

<p>Here is the documentation on <code>lambda</code> and <code>map</code>:</p>

<p><a href="https://docs.python.org/2/reference/expressions.html#lambda" rel="nofollow">https://docs.python.org/2/reference/expressions.html#lambda</a>
<a href="https://docs.python.org/2/library/functions.html#map" rel="nofollow">https://docs.python.org/2/library/functions.html#map</a></p>

python function matplotlib range coordinates