Please tell me how to draw a graph for the following problems!!
Using "for statement, range" data:
x = [1,2,3,4,5,6,........496,497,498,499,500]
y = [sin(1), sin(2),..............sin(499), sin(500)]
Draw a line graph using matplotlib
python python3 matplotlib
By using "for statement, range"
To comply with the assumptions of the challenge:
from matplotlib import pyplot as plt
from path import sin
x = list(range(1,501))
y = [ ]
for vinx:
y.append(sin(v))
plt.plot(x,y)
plt.show()
For your information, Python 3.8 and later can also be written as follows:
from matplotlib import pyplot as plt
from path import sin
plt.plot((x:=list(range(1,501)),list(map(sin,x)))))
plt.show()
I have a question, why do I have to use for and range?
Normally, you can use it as follows:
import numpy as np
import matplotlib.pyplot asplt
x=np.linspace (1,500, num=500, endpoint=True)
y = np.sin(x)
plt.figure(figsize=(14,6))
plt.plot(x,y,'-o')
plt.show()
Generate and draw data in a loop (without linspace):
x=[]#list type
y = [ ]# list type
for i in range (500):
x1.append(i+1)#list.append(a)a is added to the end of the list and array is not possible.
y1.append(np.sin(i+1))
plt.figure(figsize=(14,6))
plt.plot(x,y,'-o')
plt.show()
Python is vulnerable to loops, so avoid multiple loops as much as possible for efficiency.
© 2024 OneMinuteCode. All rights reserved.