144

How can I plot the following 3 functions (i.e. sin, cos and the addition), on the domain t, in the same figure?

from numpy import *
import math
import matplotlib.pyplot as plt

t = linspace(0, 2*math.pi, 400)

a = sin(t)
b = cos(t)
c = a + b
Srivatsan
  • 8,657
  • 10
  • 51
  • 82
user3277335
  • 1,797
  • 2
  • 14
  • 16

4 Answers4

229

To plot multiple graphs on the same figure you will have to do:

from numpy import *
import math
import matplotlib.pyplot as plt

t = linspace(0, 2*math.pi, 400)
a = sin(t)
b = cos(t)
c = a + b

plt.plot(t, a, 'r') # plotting t, a separately 
plt.plot(t, b, 'b') # plotting t, b separately 
plt.plot(t, c, 'g') # plotting t, c separately 
plt.show()

enter image description here

Srivatsan
  • 8,657
  • 10
  • 51
  • 82
  • Okay, now how do you clear the plots if you want to plot the next one on a new graph? – NoName Dec 14 '19 at 17:53
  • @NoName: What do you mean by "clear the plots"? Do you want to close the plotting window and plot a new one? – Srivatsan Dec 16 '19 at 01:27
  • Nevermind, the default figure is automatically cleared after calling `plt.show()` so plotting again won't plot on the same graph. – NoName Dec 16 '19 at 05:24
  • how can we customize the height, width? Default size in my case is too small. `figsize` not working – Nikhil VJ Jun 09 '21 at 04:32
  • 1
    @NikhilVJ: `fig = plt.figure(figsize=(10,10)), ax = fig.add_subplot(111), ax.plot(a,b,'r'), fig.show()`, this should work in principle. – Srivatsan Jun 10 '21 at 05:56
66

Perhaps a more pythonic way of doing so.

from numpy import *
import math
import matplotlib.pyplot as plt

t = linspace(0,2*math.pi,400)
a = sin(t)
b = cos(t)
c = a + b

plt.plot(t, a, t, b, t, c)
plt.show()

enter image description here

Jash Shah
  • 1,856
  • 3
  • 18
  • 36
  • 1
    Can you also change the colors of each plot using this method? If so, how? – nbro Oct 11 '17 at 20:16
  • 6
    You can specify the colors by changing the `plt.plot()` part as: `plt.plot(t, a, 'b', t, b, 'g', t, c, 'y')`. You can specify the colors based on the colors given in this link: https://matplotlib.org/users/colors.html – Jash Shah Oct 20 '17 at 08:50
  • can't we use `ion()` in pyplot for this too right, rather than plot everything in a single line? – toing_toing Dec 03 '18 at 16:37
  • @toing_toing Sorry I am not too aware of `ion()`. Can you illustrate with an example? – Jash Shah Jan 15 '19 at 10:57
  • 4
    how is this "more pythonic" than the accepted answer ? "Explicit is better than implicit." and readability supports the accepted answer imho (I admit this is subjective though). The quick understanding of your dense plt.plot() call is enabled by the use of a similar "t" variable for the three curves, which isn't systematic. That being said, I think it's interesting to know this version exists. – Blupon Oct 04 '19 at 11:47
7

Just use the function plot as follows

figure()
...
plot(t, a)
plot(t, b)
plot(t, c)
nbro
  • 13,796
  • 25
  • 99
  • 185
leeladam
  • 1,698
  • 10
  • 15
0

If you want to work with figure, I give an example where you want to plot multiple ROC curves in the same figure:

from matplotlib import pyplot as plt
plt.figure()
for item in range(0, 10, 1): 
    plt.plot(fpr[item], tpr[item])
plt.show()
Linh
  • 3
  • 3