import matplotlib.pyplot as plt
import numpy as np

data_no_load = {
    "U_in": [300, 270, 240, 210, 180, 150, 120, 90, 60, 30],  # мВ
    "U_out": [8.2, 7.6, 7.0, 6.3, 5.6, 4.9, 4.2, 3.6, 3.0, 1.8],  # В
}
data_with_load = {
    "U_in": [500, 450, 400, 350, 300, 250, 200, 150, 100],  # мВ
    "U_out": [7.2, 6.8, 6.4, 6.0, 5.6, 5.0, 4.2, 3.4, 2.5],  # В
}
data_with_feedback = {
    "U_in": [1300, 1170, 1040, 910, 780, 650, 520, 390, 260],  # мВ
    "U_out": [6.6, 6.5, 6.0, 5.8, 5.0, 4.4, 3.8, 3.0, 1.5],  # В
}

fig, ax = plt.subplots(figsize=(10, 6))

x_no_load = np.array(data_no_load["U_in"])
y_no_load = np.array(data_no_load["U_out"])
z_no_load = np.polyfit(x_no_load, y_no_load, 2)
p_no_load = np.poly1d(z_no_load)
ax.plot(x_no_load, y_no_load, 'o', label="Без нагрузки", color="blue")
ax.plot(x_no_load, p_no_load(x_no_load), '-', color="blue")

x_with_load = np.array(data_with_load["U_in"])
y_with_load = np.array(data_with_load["U_out"])
z_with_load = np.polyfit(x_with_load, y_with_load, 2)
p_with_load = np.poly1d(z_with_load)
ax.plot(x_with_load, y_with_load, 'o', label="С нагрузкой", color="green")
ax.plot(x_with_load, p_with_load(x_with_load), '-', color="green")

x_with_feedback = np.array(data_with_feedback["U_in"])
y_with_feedback = np.array(data_with_feedback["U_out"])
z_with_feedback = np.polyfit(x_with_feedback, y_with_feedback, 2)
p_with_feedback = np.poly1d(z_with_feedback)
ax.plot(x_with_feedback, y_with_feedback, 'o', label="С нагрузкой и ООС", color="red")
ax.plot(x_with_feedback, p_with_feedback(x_with_feedback), '-', color="red")

ax.set_title("Амплитудные характеристики усилителя", fontsize=16)
ax.set_xlabel("Входное напряжение $2U_{вх}$, мВ", fontsize=14)
ax.set_ylabel("Выходное напряжение $2U_{вых}$, В", fontsize=14)
ax.legend(fontsize=12)
ax.grid(True)

ax.set_xlim(left=0)
ax.set_ylim(bottom=0)

plt.savefig('images/AX.png', dpi=300)