0

How to build two graphs in one figure from the equations below

  1. y = (x+2)^2
  2. y = sin(x/2)^2

There is my code:

import matplotlib.pyplot as plt
import numpy as np
from math import sin

y = lambda x: sin(x / 2) ** 2
y1 = lambda x: (x + 2) ** 2

fig = plt.subplots()

x = np.linspace(-3, 3, 100)

plt.plot(x, y(x))
plt.plot(x, y1(x))

plt.show()
Mr. T
  • 11,960
  • 10
  • 32
  • 54
George
  • 1
  • You cannot use `math` functions with numpy arrays. Use `np.sin()` instead. See [here](https://stackoverflow.com/questions/48226089/scipy-curve-fit-doesnt-like-math-module) for more information on this problem. – Mr. T Dec 15 '21 at 15:35
  • thank you very much – George Dec 15 '21 at 15:40
  • 1
    FYI, your question lacked detail, namely what your problem was. Obviously, you get an error message - so you should have specified the error message in the question. As you can see, people interpreted your question differently ("Why do I get an error message?", "How do I plot two functions in one graph?", and "How do I plot functions into subplots?"). All three interpretations are valid, so you have to make sure to specify your problem. – Mr. T Dec 15 '21 at 15:49

1 Answers1

0

Use supplots to make 2 Axes in your Figure:

import matplotlib.pyplot as plt
import numpy as np

fig, (ax1,ax2) = plt.subplots(nrows=2)

x = np.linspace(-3, 3, 100)

ax1.plot(x, np.sin(x / 2) ** 2)
ax2.plot(x, (x + 2) ** 2)

enter image description here

Stef
  • 28,728
  • 2
  • 24
  • 52