0

df:

      USER_ID  EventNum
0        1390        17
1        4452        15
2         995        14
3         532        14
4        3281        14
...       ...       ...
5897     4971         1
5898     2637         1
5899      792         1
5900     5622         1
5901        1         1

[5902 rows x 2 columns]

I want to plot a figure, using USER_ID as X-axis, EventNum as Y-axis. To avoid cluttering up the axis, I sample USER_ID values at fixed interval as xticks like this:

[1390, 4899, 4062, 366, 5001, 3383, 5003, 446, 2879, 3220, 4006, 4595, 1713, 2649, 2291, 5647, 2040, 5468, 3719, 4198, 5622]

I do like this, but it seems xticks values are not placed as their sorted order(xticks), instead they are placed by the increasing numeric order(see the figure below).

xticks = [1390, 4899, 4062, 366, 5001, 3383, 5003, 446, 2879, 3220, 4006, 4595, 1713, 2649, 2291, 5647, 2040, 5468, 3719, 4198, 5622]

ax = df.plot(x='USER_ID', y=['EventNum'], use_index=False, rot=270)
ax.set_xticks(xticks)
ax.set_xlabel('User ID')
ax.set_ylabel('Event Number')

enter image description here

How can I fix this?

l4rmbr
  • 1,227
  • 7
  • 22
  • this answer should help: https://stackoverflow.com/a/39545860/15521392. You need to set them values as labels, while defining the ticks as a range – Rabinzel Jan 05 '23 at 08:34

1 Answers1

0

I figured it out, just replace the line

ax.set_xticks(xticks)

with this

ax.set_xticks(pos, xticks)

where pos is the corresponding position of the xticks value.

In my case, they are respectively like below:

pos: [0, 295, 590, 885, 1180, 1475, 1770, 2065, 2360, 2655, 2950, 3245, 3540, 3835, 4130, 4425, 4720, 5015, 5310, 5605, 5900]
xticks: [1390, 4899, 4062, 366, 5001, 3383, 5003, 446, 2879, 3220, 4006, 4595, 1713, 2649, 2291, 5647, 2040, 5468, 3719, 4198, 5622]

enter image description here

l4rmbr
  • 1,227
  • 7
  • 22