1
import matplotlib.pyplot as plt
import numpy as np

print('''Available options:
1) straight line
2) Parabola
3) Hyperbole
4) Cubic parabola
5) Parabola branches down
6) Negative hyperbole
7) Sinusoid
8) Cosine''')
a = int(input("Select the function graph: "))

fig, ax = plt.subplots()
ax.set_xlabel('x')
ax.set_ylabel('y')
f = int(input("Введите x: "))
if f < 0:
    x = np.linspace(f, abs(f), 100)
else:
    print("x Нужно вводить отрицательный")
ax = plt.gca()
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)



match a:
    case 1:
        y = x + 3
        ax.plot(x, y)
        plt.show()
    case 2:
        y = x**2
        ax.plot(x, y)
        plt.show()
    case 3:
        y = 1 / x
        ax.plot(x, y)
        plt.show()
    case 4:
        y = x**3 + 2*x + 2
        ax.plot(x, y)
        plt.show()
    case 5:
        y = -(x**2)
        ax.plot(x, y)
        plt.show()
    case 6:
        y = -(1 / x)
        ax.plot(x, y)
        plt.show()
    case 7:
        y = np.sin(x)
        ax.plot(x, y)
        plt.show()
    case 8:
        y = np.cos(x)
        ax.plot(x, y)
        plt.show()
    case _:
        print("You have entered a non-existent value")

The graph at the same time should shift from the user's input coordinates. I think it can be done using matplotlib but I don't understand how. At the same time, it is desirable to keep all the graphs that I have, just make them move along the x-axis and y-axis.......................................................................

Ppepe
  • 11
  • 1
  • maybe this issue is useful for you. https://stackoverflow.com/questions/48675320/matplotlib-plotting-on-old-plot – Ryan Feb 25 '23 at 10:38

1 Answers1

0

if you want to keep all the graphs that you have, do not call plt.show() before you finish all ploting.

fig, ax = plt.subplots()
ax.set_xlabel('x')
ax.set_ylabel('y')

ax = plt.gca()
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

y = x + 3
ax.plot(x, y, label='y=x+3')
y = x ** 2
ax.plot(x, y, label='y=x**2')
y = 1 / x
ax.plot(x, y, label='y=1/x')
y = x ** 3 + 2 * x + 2
ax.plot(x, y, label='y=x**3+2*x+2')
y = -(x ** 2)
ax.plot(x, y, label='y=-x**2')
y = -(1 / x)
ax.plot(x, y, label='y=-1/x')
y = np.sin(x)
ax.plot(x, y, label='y=sin(x)')
y = np.cos(x)
ax.plot(x, y, label='y=cos(x)')
plt.legend()
plt.show()

enter image description here

Ryan
  • 189
  • 12