0

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 :

  1. This code gives the following output but cannot save the .eps file with the labels (plt.savefig('seqn_of_function1.eps', format='eps', dpi=1000)) enter image description here
  1. 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.

MFerguson
  • 1,739
  • 9
  • 17
  • 30
sigma
  • 227
  • 3
  • 13
  • Have you tried using `plt.plot(x,y,label='f_{}'.format(i))`? (Instead of `plt.plot(x,y,label='$f_$',i)`) – bene Sep 02 '18 at 07:21
  • @bene....this gives an error. – sigma Sep 02 '18 at 07:22
  • also how to save the .eps file with the labels? My code doesn't do that.... – sigma Sep 02 '18 at 07:28
  • I'm looking into it. The labels won't be saved because you save the figure before you create the labes (`plt.legend()`) -> first calls `plt.legend()` and then `plt.savefig(...)` – bene Sep 02 '18 at 07:37
  • @bene...Thank you so much....it fixed.....well what about the previous problem..??to create label with $f_n$ – sigma Sep 02 '18 at 07:39
  • Using `plt.plot(x, y, label='$f_{}$'.format(i))` works for me – bene Sep 02 '18 at 07:40

2 Answers2

1

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()
bene
  • 798
  • 7
  • 19
  • Thanks a lot....Can you tell me how to create the label on each functions rather on a side of the plot....?? – sigma Sep 02 '18 at 07:43
  • Sorry, no.. . But you can check the docu: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.legend.html – bene Sep 02 '18 at 07:44
  • 1
    Maybe this is a help: https://stackoverflow.com/questions/16992038/inline-labels-in-matplotlib – bene Sep 02 '18 at 07:52
0

you can use fString to display your labels as follow:

plt.plot(x, y, label=f'$f_{i}$')

which value of i changes during run-time