Is my line plot visual accurate?

I’m currently working on the univariate model for predicting car prices

Other details:download (1)
And the figure above is the visualization of the result.
How do I make it look less weird?

I’ll appreciate any advice.

Whenever i see back and forth zigzag lines, it tells to me sort the x-axis values.
Or, you can set marker to "" empty string and increase markersize which turns the default line plot into a scatter plot


Setting marker to "" or increasing markersize didn’t work. I don’t know how to sort the x-axis values because the x-axis is the key of a dictionary.

Could you share your code behind this plot?
What does “didn’t work” mean?
Also is my interpretation of “weird” being the zigzag lines correct or something else?

def knn_train_test(df, train_col, target_col):
#shuffling the data
shuffled_index = np.random.permutation(df.index)
shuffled_df = df.reindex(shuffled_index)
#shuffled_df = shuffled_df.drop([0], axis=0)

`#spliting the dataset into train and test set`
`split_value = int(len(shuffled_df)/2)`
`train_set = shuffled_df.iloc[0:split_value]`
`test_set = shuffled_df.iloc[split_value:]`

`k_values = [1, 3, 5, 7, 9]`
`k_rmses = {}`

`#training and predicting`
`for k in k_values:`
 `   knn = KNeighborsRegressor(n_neighbors=k)`
  `[[train_col]], train_set[target_col])`
   ` predictions = knn.predict(test_set[[train_col]])`
    `mse = mean_squared_error(test_set[target_col], predictions)`
    `rmse = np.sqrt(mse)`
    `k_rmses[k] = rmse`
`return k_rmses`

# training all the numeric columns in normalized_cars using knn_train_test function
target_cols = normalized_cars.columns.drop('price')
k_rmses_target = {}
for cols in target_cols:
rmse_vals = knn_train_test(normalized_cars, cols, 'price')

import matplotlib.pyplot as plt
%matplotlib inline

for k,v in sorted(k_rmses_target.items()):
x = list(v.keys())
y = list(v.values())

`plt.plot(x,y, "", markersize=20)`
`plt.xlabel('k value')`

I meant didn’t work as the zigzag lines didn’t change
Yes, my weird meant the zigzag lines

This chunk tells me that x-axis values are a list, since you did list() over the keys.
How do you sort a list then?

I made a mistake with what i said about marker. What i mean’t was change the linestyle so it’s not visible anymore. Then there won’t be zigzag. (if you don’t want to sort and still show lines)

Sorting it would affect the values on y axis

I don’t have many of the variables in the pasted code above.

Are you able to produce a minimal reproducible example to replicate the zigzag here?
(Like directly defining the x and y values right before plotting, with the plotting code) so I can try to fix that and return similar but edited code.

The figure above is what the k_rmses_target dictionary looks like

The figure above contains the values of x, the values of y and the keys and values of the inner dictionary of k_rmses_target dictionary.

I see x: [1 3 9 5 7].
Do you think something is wrong with this?
Can you guess how does this relate to the zigzag on the graph?
How can you verify the effect of x on how the graph looks? (Set up a small example with your own x, y and plot it, change the x and see results.).

I’m sorry for the late response. I ran the notebook locally and it was resolved. Thank you for helping

I am having the same problem. How did this end up getting resolved?