Plot a graph using Python plot the graph of f(x)=1/x in[-5,5]
Plot the graph of f(x)=1/x in[-5,5]
In this program, we are polt a graph using python
The Program is divide into step
Step1:-- import python package using
from pylab import *
import numpy as np
Step2:- assign the value of x and function
x = np.linspace(-5, 5, 100)
y = 1/x
Step3:- Plot the graph and show it using , plot() function
plot(x, y, color="red", marker='*', label="y=1/x")
Step4 :- Then we are adding one more function legend
legend -: legend function shows your graph information
4 way to plot legend:-
legend(loc=0) --- is by default
legend(loc=1)
legend(loc=2)
legend(loc=3)
legend(loc=4)
# legend(loc=1)
# legend(loc=2)
# legend(loc=3)
# legend(loc=4)
legend(loc=0)
Step5 :- Show your graph using ,show() function
xlabel("x")
ylabel("Y")
title("graph of y=1/x")
show()
Complete program
# plot the graph of f(x)=1/x in[-5,5]
from pylab import *
import numpy as np
x = np.linspace(-5, 5, 100)
y = 1/x
plot(x, y label="y=1/x")
# legend(loc=1)
# legend(loc=2)
# legend(loc=3)
# legend(loc=4)
legend(loc=0)
xlabel("x")
ylabel("Y")
title("graph of y=1/x")
show()
Output
legend(loc=0)
legend(loc=1)
loc=1 loc= 0 are same
legend(loc=2)
Now we apply the style on the graph
Q1. how to change the graph line color
--- using plot function we add one more command in plot function, applying color command in plot function you can change the color of the graph line
# plot(x, y, color="r", label="y=1/x")
plot(x, y, color="red", label="y=1/x")
Q2. how to change the graph lining style
--- using plot function we add one more command in plot function, applying marker command in plot function you can change the style of the graph line
plot(x, y, color="red", marker='^', label="y=1/x")
Change the graph line color complete program
# plot the graph of f(x)=1/x in[-5,5]
from pylab import *
import numpy as np
x = np.linspace(-5, 5, 100)
y = 1/x
plot(x, y, label="y=1/x")
# plot(x, y, color="r", label="y=1/x")
plot(x, y, color="red", label="y=1/x")
legend(loc=0)
xlabel("x")
ylabel("Y")
title("graph of y=1/x")
show()
Output
Change the graph lining style complete program
# plot the graph of f(x)=1/x in[-5,5]
from pylab import *
import numpy as np
x = np.linspace(-5, 5, 100)
y = 1/x
plot(x, y, label="y=1/x")
# plot(x, y, color="r", label="y=1/x")
plot(x, y, color="red", label="y=1/x")
# plot(x, y, color="red", marker='^', label="y=1/x")
plot(x, y, color="red", marker='*', label="y=1/x")
legend(loc=0)
xlabel("x")
ylabel("Y")
title("graph of y=1/x")
show()
Output
0 Comments