Have the equation graph of the linear function drawn in Python

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 ~~".

specification

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.

What to use

--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)

flow

  1. Enter the rate of change and intercept
  2. Create x and y data
  3. Draw a graph with matplotlib

Production started!

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

Have the equation graph of the linear function drawn in Python
Draw a graph of a quadratic function in Python
Get the caller of a function in Python
Find the solution of the nth-order equation in python
Solving the equation of motion in Python (odeint)
Try transcribing the probability mass function of the binomial distribution in Python
A function that measures the processing time of a method in python
Check the behavior of destructor in Python
OR the List in Python (zip function)
[Python3] Rewrite the code object of the function
The result of installing python in Anaconda
The basics of running NoxPlayer in Python
About the Normal Equation of Linear Regression
In search of the fastest FizzBuzz in Python
Double pendulum equation of motion in python
Output the number of CPU cores in Python
[Python] Sort the list of pathlib.Path in natural sort
Match the distribution of each group in Python
View the result of geometry processing in Python
Make a copy of the list in Python
Find the divisor of the value entered in python
Fix the argument of the function used in map
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Output in the form of a python array
I implemented the inverse gamma function in python
Set an upper limit on the number of recursive function iterations in Python
Graph drawing in python
the zen of Python
Linear search in Python
Draw graph in python
"Linear regression" and "Probabilistic version of linear regression" in Python "Bayesian linear regression"
I tried to graph the packages installed in Python
Experience the good calculation efficiency of vectorization in Python
[Python] Get the numbers in the graph image with OCR
Omit the decimal point of the graph scale in matplotlib
[python] Get the list of classes defined in the module
A python implementation of the Bayesian linear regression class
The story of FileNotFound in Python open () mode ='w'
Learn the design pattern "Chain of Responsibility" in Python
Implement the solution of Riccati algebraic equations in Python
Get the size (number of elements) of UnionFind in Python
Not being aware of the contents of the data in python
Reproduce the execution example of Chapter 4 of Hajipata in Python
Let's use the open data of "Mamebus" in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
[Python] Outputs all combinations of elements in the list
Overview of generalized linear models and implementation in Python
Get the URL of the HTTP redirect destination in Python
A reminder about the implementation of recommendations in Python
Reproduce the execution example of Chapter 5 of Hajipata in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
The attitude that programmers should have (The Zen of Python)
Check the asymptotic nature of the probability distribution in Python
What does the last () in a function mean in Python?
Note: The meaning of specifying only * (asterisk) as an argument in the Python function definition.
[Python] Representing the number of complaints from life insurance companies in a bar graph
Try scraping the data of COVID-19 in Tokyo with Python
Try to get the function list of Python> os package
Find out the apparent width of a string in python