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()
3
  • You cannot use math functions with numpy arrays. Use np.sin() instead. See here for more information on this problem. Commented Dec 15, 2021 at 15:35
  • thank you very much Commented Dec 15, 2021 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. Commented Dec 15, 2021 at 15:49

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.