An Example Analysis of python Drawing Element Quadratic Equation Curve

  • 2021-11-14 06:02:21
  • OfStack

Description

1. The Matplotlib function can draw graphics, and the plot function can draw curves.

2. x coordinates and Y coordinates of 200 points need to be input into plot function in sequence, and then show function is called to display graphics.

Instances


import matplotlib.pyplot as plt
#200 A dot x Coordinates 
x=range(-100,100)
# Generate y Coordinates of points 
y=[i**2 for i in x ]
# Draw 1 Yuan 2 Subcurve 
plt.plot(x,y)
# Call savefig Will 1 Yuan 2 Save the subcurve as result.jpg
plt.savefig('result.jpg') # If it is written directly as  plt.savefig('cos')  Will generate cos.png
plt.show()

Instance extension:


import matplotlib.pyplot as plt

#  Define domain [-10, 11]
x_val = [i for i in range(-10,11)]
#  Solve the strain 
y_val = [5*x**2 for x in x_val]
print(x_val)
print(y_val)

#  Settings matplotlib Drawing tool 
fig, ax = plt.subplots()
ax.plot(x_val, y_val)
ax.set(xlabel='x(independentVariable)', ylabel='y(strain)', title='On the equation of a quadratic function')
ax.grid()

#  Save Picture 
fig.savefig("test.png")
#  Show pictures 
plt.show()

Related articles: