Home > Software design >  How to set the x-axis order with pandas using matplotlib 2.1.2
How to set the x-axis order with pandas using matplotlib 2.1.2

Time:10-05

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);

enter image description here

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);

enter image description here

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)

CodePudding user response:

I am not exactly sure why this worked....but here is something I did that works.

df = df_temp[['grade','WoE']]
df

# grade WoE
# 0 G   -1.113459
# 1 F   -0.975440
# 2 E   -0.678267
# 3 D   -0.391843
# 4 C   -0.049503
# 5 B   0.358476
# 6 A   1.107830

fig = plt.figure(figsize = (18,6))
ax = fig.add_subplot(111)
ax.plot(np.arange(len(df.grade)), df.WoE, color='k', marker='o', linestyle='dashed')
ax.set_xticks(range(df.grade.count()))
_ = ax.set_xticklabels(df.grade)

enter image description here

And updated function:

# 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']
    fig = plt.figure(figsize = (18,6))
    ax = fig.add_subplot(111)
    ax.plot(np.arange(len(df_WoE.iloc[:, 0])), df_WoE.WoE, color='k', marker='o', linestyle='dashed')
    ax.set_xticks(range(df_WoE.iloc[:, 0].count()))
    ax.set_xticklabels(df_WoE.iloc[:, 0])
    plt.xticks(rotation = rotation_of_x_axis_labels)
  • Related