I would like the plot to show, from left to right on the x axis, an increasing line. So, the x axis needs to be in this order:
J H G C A B E F D I K L
Then the line would be increasing from left to right.
df1 = pd.DataFrame({'Col1': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'],
'Col2': [0, 1, -1, 4, 2, 3, -2 , -3, 5, -5, 6, 7]
})
df1.sort_values('Col2', inplace=True)
df1.reset_index(drop=True, inplace=True)
df1.index.astype(str) + '_' + df1.iloc[:, 0]
plt.plot(df1.Col1, df1.Col2);
I've even tried adding the index number (after sorting) as a prefix to the x axis but it still sorts inaccurately (goes from 0 to 10 to 11 to 1 etc).
plt.plot(df1.index.astype(str) + '_' + df1.iloc[:, 0], df1.Col2);
Does anyone know how to prevent the x axis from sorting alphabetically using matpotlib 2.1.2? The issue is that I am using matplotlib 2.1.2 and I cannot update to a more recent version due to a corporate firewall issue.
Adding some additional context in case it helps.
This is a function I am attempting to write from an Udemy course. Unfortunately, the instructor doesn't hasn't responded to my question. (I can provide a strong negative review for this provider... lol)
In any case, I want to use this function - so how can I adjust thus function AND prevent it from sorting the x axis alphabetically?
# Write a funtion that plots by WoE
def plot_by_woe(df_WoE, rotation_of_x_axis_labels=0):
x = np.array(df_WoE.iloc[:, 0].apply(str))
y = df_WoE['WoE']
plt.figure(figsize= (18,6))
plt.plot(x, y, marker='o', linestyle = '--', color = 'k')
plt.xlabel(df_WoE.columns[0])
plt.ylabel('WoE')
plt.title(str('WoE by ' + df_WoE.columns[0]))
plt.xticks(rotation = rotation_of_x_axis_labels)