Draw a graph of a quadratic function in Python

Overview

Practice the Python library Matplotlib. It's so common that you don't know what the number is, but please forgive me.

Install Matplotlib

Install with pip.

pip install matplotlib

Depiction

import matplotlib.pyplot as plt
import numpy as np

# -10 < x < 10
x = np.arange(-10, 10, 0.1)

# a, b,Substitute each value for c
a = int(input("a : "))
b = int(input("b : "))
c = int(input("c : "))

# y = ax^2 + bx + c
y = a*x**2 + b*x + c

#Plot execution to graph
plt.plot(x, y)
plt.show()

Execution result

a=1, b=2, c=2 | y=x^2+2x+2 y=x^2+2x+2.png

a=4, b=5, c=2 | y=4x^2+5x+2 y=4x^2+5x+2.png

I was able to describe it firmly.

Improvement

I want to improve it already.

Set title

plt.title("y =" + str(a) + "x²+" + str(b) + "x+" + str(c))

x-axis and y-axis labels

plt.xlabel("x")
plt.ylabel("y", rotation = 0)

Set the display range of y to -5 <y <10

plt.ylim(-5, 10)

Show grid lines

plt.grid()

Depicted again

import matplotlib.pyplot as plt
import numpy as np

# -10 < x < 10
x = np.arange(-10, 10, 0.1)

# a, b,Substitute each value for c
a = int(input("a : "))
b = int(input("b : "))
c = int(input("c : "))

# y = ax^2 + bx + c
y = a*x**2 + b*x + c

#Set title
plt.title("y =" + str(a) + "x²+" + str(b) + "x+" + str(c))

#Other adjustments
plt.xlabel("x") #x-axis label display
plt.ylabel("y", rotation = 0) #Y-axis label display"
plt.ylim(-5, 10) #Display range of y-5 < y <Set to 10
plt.grid() #Show grid lines

#Plot execution to graph
plt.plot(x, y)

Execution result

a=3, b=2, c=4 | y=3x^2+2x+4 y=3x^2+2x+4.png

a=2, b=6, c=1 | y=2x^2+6x+1 y=2x^2+6x+1.png

I think it has become more accurate.

Recommended Posts

Draw a graph of a quadratic function in Python
Draw graph in python
Get the caller of a function in Python
Create a function in Python
Draw implicit function in python
Draw a heart in Python
Draw a scatterplot matrix in python
Draw a CNN diagram in Python
Have the equation graph of the linear function drawn in Python
A memo of writing a basic function in Python using recursion
Precautions when pickling a function in python
Display a list of alphabets in Python 3
Draw a heart in Python Part 2 (SymPy)
Draw a tree in Python 3 using graphviz
A function that measures the processing time of a method in python
Draw a graph with Japanese labels in Jupyter
To execute a Python enumerate function in JavaScript
Draw a "breast curved surface" in a 3D graph (1)
Create a standard normal distribution graph in Python
[Python] Draw a directed graph with Dash Cytoscape
Graph drawing in python
Make a copy of the list in Python
Rewriting elements in a loop of lists (Python)
Make a joyplot-like plot of R in python
Output in the form of a python array
Get a glimpse of machine learning in Python
[Python] How to draw a histogram in Matplotlib
Draw a "breast curved surface" in a 3D graph (2)
A well-prepared record of data analysis in Python
Draw multiple photos in a graph from multiple folders
This is a sample of function application in dataframe.
[Python] How to draw a line graph with Matplotlib
A collection of code often used in personal Python
Until drawing a 3D graph in Python on windows10
Group by consecutive elements of a list in Python
Display a histogram of image brightness values in python
A collection of Excel operations often used in Python
A reminder about the implementation of recommendations in Python
What does the last () in a function mean in Python?
Take a screenshot in Python
Draw a graph with NetworkX
Draw mp3 waveform in Python
Create a dictionary in Python
Use callback function in Python
[python] Value of function object (?)
ntile (decile) function in python
[Python] Etymology of python function names
Draw Poincare's disk in Python
Equivalence of objects in Python
Draw "Draw Ferns Programmatically" in Python
Let's draw a logistic function
Make a bookmarklet in Python
Nonlinear function modeling in Python
Immediate function in python (lie)
Implementation of quicksort in Python
Draw a graph with networkx
Draw a weakness graph in Python and save it in various formats (Raspberry Pi, macOS)
[Python] Representing the number of complaints from life insurance companies in a bar graph
Study math with Python: Draw a sympy (scipy) graph with matplotlib
Find out the apparent width of a string in python
A function that divides iterable into N pieces in Python