Reputation: 127
I am trying to get this chart to show the x axis starting at G and ending with A (left to right). The Udemy instructor wrote this exact same code and generated the desired results - my x axis seems backwards....and of course, the instructor has not addressed the question.
def plot_by_woe(df, rotation_of_x_axis_labels = 0):
x = df.iloc[:, 0].apply(str)
y = df['WoE']
plt.figure(figsize = (18,6))
plt.plot(x, y, marker='o', linestyle = '--', color = 'k')
plt.xlabel(df.columns[0])
plt.ylabel('Weight of Evidence')
plt.title(str('Weight of Evidence by ' + df.columns[0]))
plt.xticks(rotation = rotation_of_x_axis_labels)
df = pd.DataFrame(
[['G',-1.113459],
['F',-0.975440],
['E',-0.678267],
['D',-0.391843],
['C',-0.049503],
['B',0.358476],
['A',1.107830]],
columns = ['grade', 'WoE'])
df
plot_by_woe(df)
Upvotes: 0
Views: 1149
Reputation: 127
Apparently, there is a bug in the matplotlib version I am using ('2.1.2' and earlier)
How to prevent alphabetical sorting for python bars with matplotlib?
I have tried a bunch of workarounds but haven't found a solution yet. At least we have an explanation.
Upvotes: 0
Reputation: 71
All in all, here's my code:
def plot_by_woe(df, rotation_of_x_axis_labels = 0):
x = df.iloc[:, 0].apply(str)
y = df['WoE']
plt.figure(figsize = (18,6))
plt.plot(x, y, marker='o', linestyle = '--', color = 'k')
plt.xlabel(df.columns[0])
plt.ylabel('Weight of Evidence')
plt.title(str('Weight of Evidence by ' + df.columns[0]))
plt.xticks(rotation = rotation_of_x_axis_labels)
df = pd.DataFrame(
[['G',-1.113459],
['F',-0.975440],
['E',-0.678267],
['D',-0.391843],
['C',-0.049503],
['B',0.358476],
['A',1.107830]],
columns = ['grade', 'WoE'])
plot_by_woe(df)
Upvotes: 2
Reputation: 71
As far as I understood, you want to get the following graph, right?
If so, then your function seems to work well, just call it at the end of your code:
plot_by_woe(df)
Upvotes: 2