Plotting with interpolation using Matplotlib Program description:

You are given a set of two functions: $$f=x^3-6x^2+x+5; g=(x-2)^2-6$$
Plot them using Matprolib on a user input segment [a; b].

My solution:
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interpolate # for smoothing

def func(x):
    return [pow(x, 3) - 6 * pow(x, 2) + x + 5, pow((x-2), 2) - 6]
# plt.plot([1,5, -3, 0.5], [1, 25, 9, 0.5])
# plt.plot(1, 7, &quot;r+&quot;)
# plt.plot(-1, 7, &quot;bo&quot;)
f = []
f_1 = []
x = []
interval = [int(x) for x in input(&quot;Define segment (i.e. a b): &quot;).split(' ')]
for val in range(interval[0], interval[1] + 1):
    x.append(val)
    f.append(func(val)[0])
    f_1.append(func(val)[1])

linear_space = np.linspace(interval[0], interval[1], 300)
a_BSpline = interpolate.make_interp_spline(x, f)
b_BSpline = interpolate.make_interp_spline(x, f_1)

f_new = a_BSpline(linear_space)
f_1_new = b_BSpline(linear_space)

plt.plot(linear_space, f_new, color=&quot;#47c984&quot;,
linestyle=&quot;solid&quot;, linewidth=1)

plt.plot(linear_space, f_1_new, color=&quot;#fc6703&quot;,
linestyle=&quot;solid&quot;, linewidth=1)

plt.gca().spines[&quot;left&quot;].set_position(&quot;zero&quot;)
plt.gca().spines[&quot;bottom&quot;].set_position(&quot;zero&quot;)
plt.show()

Input: -10 10
Output:

Question:
Is there any way to make this code more concise?
Thank you in advance.
As I pointed out in the comments, I do not think smoothing is needed when plotting functions that are already smooth, such as polynominals. In those cases, the graphs would look rough only if the points plotted are not dense enough (on the x axis).

Since you have already imported and used numpy, it would be better to use numpy.polyval for vectorized evaluation of polynominals.

Drawing spines at zero would not work well if the input range does not include zero. In that case, the &quot;center&quot; position might be used instead of &quot;zero&quot;. (I will not implement this logic in my code below)

The top and right spines should not be shown.

Consider using plt.style.use for setting common style information. See here for alternative options for style setting.

Avoid constants like &quot;#47c984&quot;. Name them with appropriate constant names for better readability.

It is a good practice to comply to the PEP 8 style when writing Python code. Use an IDE (such as PyCharm) or an online checker (such as this) for automatic PEP 8 violation checking.

When running code outside a method / class, it is a good practice to put the code inside a main guard. See here for more explanation.


Here is an improved version:
import numpy as np
import matplotlib.pyplot as plt

if __name__ == &quot;__main__&quot;:
    # Define constants (color names come from https://colornamer.robertcooper.me/)
    INPUT_MESSAGE = &quot;Lower and upper bounds of x, separated by a space: &quot;
    NUM_POINTS = 300
    PARIS_GREEN = &quot;#47c984&quot;
    BLAZE_ORANGE = &quot;#fc6703&quot;

    # Read input
    lb, ub = map(float, input(INPUT_MESSAGE).split())

    # Compute coordinates of points to be plotted
    x = np.linspace(lb, ub, NUM_POINTS)
    f_x = np.polyval([1, -6, 1, 5], x)
    g_x = np.square(x - 2) - 6

    # Plotting
    plt.style.use({&quot;lines.linestyle&quot;: &quot;solid&quot;, &quot;lines.linewidth&quot;: 1})
    plt.plot(x, f_x, color=PARIS_GREEN)
    plt.plot(x, g_x, color=BLAZE_ORANGE)

    # Adjust spines
    spines = plt.gca().spines
    spines[&quot;left&quot;].set_position(&quot;zero&quot;)
    spines[&quot;bottom&quot;].set_position(&quot;zero&quot;)
    spines[&quot;right&quot;].set_visible(False)
    spines[&quot;top&quot;].set_visible(False)

    # Display plot
    plt.show()