Recently, I was doing a graph of linear equations in junior high school, and I decided to make it because I thought, "If you let Python do this, you can enjoy it ~~ it's interesting ~~".
Enter the rate of change (slope) and intercept to display the graph. If you want the rate of change to be a fraction, enter it in the form of y / x.
--Python2.7 (Because I just installed PyCharm ...) --PyCharm community edition (design is cool!) --matplotlib (sin curve for the time being when asked to use Mac) --numpy (quite favorite)
Import what you need for the time being
main.py
import matplotlib.pylab as plt
import numpy as np
Input is done with raw_input ()
.
main.py
#abridgement
import matplotlib.pylab as plt
import numpy as np
print("Liner equation:")
le = raw_input()
Get the rate of change from the formula. Since the expression has a form like $ rx + i $, the string before +
is the rate of change. Get it with split
.
main.py
import matplotlib.pylab as plt
import numpy as np
print("Liner equation:")
le = raw_input()
rate = le.split("x")[0]
Then get the intercept. Let's consider the case where the intercept is 0.
main.py
import matplotlib.pylab as plt
import numpy as np
print("Liner equation:")
le = raw_input()
rate = le.split("x")[0]
ic = 0 if le.split("x")[1] == "" else le.split("x")[1]
Next, we will initialize the amount of change in x and y.
main.py
import matplotlib.pylab as plt
import numpy as np
print("Liner equation:")
le = raw_input()
rate = le.split("x")[0]
ic = 0 if le.split("x")[1] == "" else le.split("x")[1]
x_rate = 1
y_rate = 1
Next, create a process when the rate of change is a fraction (when separated by /) and a process when it is not separated.
main.py
import matplotlib.pylab as plt
import numpy as np
print("Liner equation:")
le = raw_input()
rate = le.split("x")[0]
ic = 0 if le.split("x")[1] == "" else le.split("x")[1]
x_rate = 1
y_rate = 1
if rate.find("/") > -1:
x_rate = int(rate.split("/")[1])
y_rate = int(rate.split("/")[0])
else:
x_rate = 1
y_rate = 1 if rate == "" else int(rate)
The rest is to generate an array with the amount of change.
main.py
import matplotlib.pylab as plt
import numpy as np
print("Liner equation:")
le = raw_input()
rate = le.split("x")[0]
ic = 0 if le.split("x")[1] == "" else le.split("x")[1]
x_rate = 1
y_rate = 1
if rate.find("/") > -1:
x_rate = int(rate.split("/")[1])
y_rate = int(rate.split("/")[0])
else:
x_rate = 1
y_rate = 1 if rate == "" else int(rate)
x = np.linspace(-x_rate / 2,x_rate,4)
y = x * y_rate + int(ic)
Finally, draw the graph from the array.
main.py
import matplotlib.pylab as plt
import numpy as np
print("Liner equation:")
le = raw_input()
rate = le.split("x")[0]
ic = 0 if le.split("x")[1] == "" else le.split("x")[1]
x_rate = 1
y_rate = 1
if rate.find("/") > -1:
x_rate = int(rate.split("/")[1])
y_rate = int(rate.split("/")[0])
else:
x_rate = 1
y_rate = 1 if rate == "" else int(rate)
x = np.linspace(-x_rate / 2,x_rate,4)
y = x * y_rate + int(ic)
plt.plot(x,y,"r-")
plt.show()
That's all there is to it!
After that, execute it and input the linear equation, and it will be drawn.
$ 4x+5 $ $ x+1 $ $ -3x-5 $
I'm still new to Python, so please give me some advice.
I just noticed that the class to draw a graph from the formula was over ...
Recommended Posts