0

I give the below MWE to describe the kind of customization I would like to apply to my figure.

My current df looks like this:

df = pd.DataFrame(
    {'feature': ['abc','bcd','abd','dax','cax','bax','def','deg','abe','cde'],
 'score': [0.7732,0.8412,0.8626,0.8705,0.8811,0.8851,0.8884,0.8922,0.8934,0.8949]}
)

df
 feature    score
0   abc     0.7732
1   bcd     0.8412
2   abd     0.8626
3   dax     0.8705
4   cax     0.8811
5   bax     0.8851
6   def     0.8884
7   deg     0.8922
8   abe     0.8934
9   cde     0.8949

To plot the score (y-axis) against feature I proceed like so:

m = df.T

m
    0   1   2   3   4   5   6   7   8   9
feature     abc     bcd     abd     dax     cax     bax     def     deg     abe     cde
score     0.7732    0.8412  0.8626  0.8705  0.8811  0.8851  0.8884  0.8922  0.8934  0.8949

k_feat = sorted(m.keys())
avg = [m[k]["score"] for k in k_feat]

plt.plot(k_feat, avg, color="blue",  marker="o")

To obtain:

enter image description here

As it now, the x-axis is labelled with indices of the feature. How do I modify this to get x-axis labelled with the corresponding feature name instead (i.e. 0 -> abc, 1 -> bcd, 2 -> abd ....)?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Amina Umar
  • 502
  • 1
  • 9
  • The correct way is `ax = df.plot(x='feature', xticks=df.index, marker='o', legend=False)` [code and plot](https://i.stack.imgur.com/epfiw.png) – Trenton McKinney Jun 21 '23 at 15:57

2 Answers2

1

If you use matplotlibs object-oriented API its easier to customize the figures...

import matplotlib.pyplot as plt

f, ax = plt.subplots()
ax.plot(k_feat, avg, color="blue",  marker="o")
ax.set_xlabel("the x label")
ax.set_ylabel("the y label")

ax.set_xticks(...)
ax.set_yticks(...)
ax.set_title(...)
...

See matplotlib docs for more details: https://matplotlib.org/stable/users/explain/api_interfaces.html

raphael
  • 2,159
  • 17
  • 20
  • Based on @white's answer, this problem is easily solved using the implicit "pyplot" interface. – jared Jun 21 '23 at 14:37
  • Also, this is not currently an answer to the question. You may want to update it. – jared Jun 21 '23 at 14:38
1

You can set your xticks labels with plt.xticks, documented here

import numpy as np


m = df.T
k_feat = sorted(m.keys())
avg = [m[k]["score"] for k in k_feat]

plt.xticks(ticks=np.arange(len(df)), labels=df['feature'])
# ticks are the numeric values, labels is the text displayed

plt.plot(k_feat, avg, color="blue",  marker="o")

Result:
xticks as requested

white
  • 601
  • 3
  • 8