When I draw a line chart, the lines are confused

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import os
data = pd.read_excel('./image.xlsx', engine='openpyxl', dtype=float)
x = data['Frequency_processed']
y = data['Z']
y_min = np.argmin(y)
y_max = np.argmax(y)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(x, y)

ax.plot(x[y_min], y[y_min], 'ks')
ax.plot(x[y_max], y[y_max], 'gs')

show_max = x[y_max]
plt.annotate(show_max, xytext=(x[y_max], y[y_max]), xy=(x[y_max], y[y_max]))
show_mix = x[y_min]
plt.annotate(show_mix, xytext=(x[y_min], y[y_min]), xy=(x[y_min], y[y_min]))

plt.ylim(1, 10000)

python 3.9.0 pandas 1.1.4 matplotlib 3.3.3 numpy 1.19.4

Lines are drawn exactly in the order they are given. If it goes ‘backwards’, that’s because your data does. If you don’t want it to plot that way, you will have to sort your data in the manner you wish.