import seaborn as sns import numpy as np import random import matplotlib.pyplot as plt…
- User
import seaborn as sns import numpy as np import random import matplotlib.pyplot as plt
plt.ioff() # Отключаем автоматическое отображение графиков
#ax.figure.show()
def generate_task(): x_1 = random.randint(-5, 0) x_2 = random.randint(0, 5) y_1 = random.randint(-5, 0) y_2 = random.randint(0, 5) k = (y_1 - y_2) / (x_1 - x_2) b = y_1 - k * x_1 y_1 = k * x_1 + b y_2 = k * x_2 + b task = f’На рисунке изображён график функции: f(x) = kx + b. Найдите F((‘“{” + str(x_1)+’})).' x = np.linspace(-5, 5, 500) y = k*x + b ax = sns.lineplot(x=x, y=y, markers=True, dashes=False) return y_1, task, ax
y_1, task,ax = generate_task()
print(y_1, task) ax.figure Почему фигуры накладываются друг на друга в гугл колаб?
- Assistant
The figures are overlapping because you are generating multiple plots without clearing the previous ones. To fix this, you need to clear the current plot before generating a new one. You can do this by adding
ax.clear()before plotting the line, like this:def generate_task(): x_1 = random.randint(-5, 0) x_2 = random.randint(0, 5) y_1 = random.randint(-5, 0) y_2 = random.randint(0, 5) k = (y_1 - y_2) / (x_1 - x_2) b = y_1 - k * x_1 y_1 = k * x_1 + b y_2 = k * x_2 + b task = f'На рисунке изображён график функции: f(x) = kx + b. Найдите F(({str(x_1)})).' x = np.linspace(-5, 5, 500) y = k*x + b ax.clear() # Clear previous plot ax = sns.lineplot(x=x, y=y, markers=True, dashes=False) return y_1, task, axThis will ensure that only one plot is displayed at a time.