Issue
I want to plot a 'Sequence of function' in Python with the corresponding label for function inside the plot.
I use matplotlib, however, I am facing the below issues.
(1). The graph should label $f_n$ corresponding to each function.
(2). It must save to a file with the labels included.
Here is my code :
import matplotlib as mpl
mpl.rc('text', usetex = True) #for LaTex notation in the Plot
mpl.rc('font', family = 'serif')
import matplotlib.pyplot as plt
import numpy as np
plt.gca().set_aspect('equal', adjustable='box')
plt.style.use(['ggplot','dark_background'])
x=np.arange(-1,1,0.001)
for i in range(1,5,1):
y = 1 - (1 / (1+x**2)**i)
plt.plot(x,y,label=i)
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.savefig('seqn_of_function1.eps', format='eps',
dpi=1000)
plt.legend()
plt.show()
The problem with this code is :
- It only labels 'i', however, I want it to label as $f_i$.
I changed the line in my code as : plt.plot(x,y,label='$f_$',i)
but it gives an "invalid_syntax" error.
Solution
This should work:
import matplotlib as mpl
mpl.rc('text', usetex=True) # for LaTex notation in the Plot
mpl.rc('font', family='serif')
import matplotlib.pyplot as plt
import numpy as np
plt.gca().set_aspect('equal', adjustable='box')
plt.style.use(['ggplot', 'dark_background'])
x = np.arange(-1, 1, 0.001)
for i in range(1, 5, 1):
y = 1 - (1 / (1 + x ** 2) ** i)
plt.plot(x, y, label='$f_{}$'.format(i))
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.legend()
plt.savefig('seqn_of_function1.eps', format='eps', dpi=1000)
plt.show()
Answered By - bene
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.