Adding suffix to labels on x-axis

I need to plot some values referred to time, with specific labels. You can think of my data like the sample below:

ID     Time     Clock
260    21 hours 20:30 # this should be referred to yesterday
127    21 hours 20:30
7      5 hours  12:30
10     6 hours  11:30
8      6 hours  11:30
... ... ...
62  NaN NaN
82  NaN NaN

Time refers to time ago looking at the current one (approx UK 17:40), while Clock should be the ‘watch time’, i.e., the current one (approx. 17:40) minus the number of hours given by the Time column. In Time the sorting should be from the highest to the lowest, similarly to Clock. The column types are both objects. In a plot, should I have the ID on the vertical axis and Clock on the x-axis. This means on the x-axis should I have clock time, looking at Clock column. For example, 48:00 would mean 48 hours ago. I would like to have, instead of 20:30 or 48:00, the following labels, for example: 20 hours ago, 48 hours ago.

import matplotlib.pyplot as plt


plt.scatter(df['Clock'], df['ID'])
plt.show()

However, I need some help to add a suffix (i.e., hours ago) or replace time’s labels on x-axis.

Answer

Currently you are passing the values from the Clock column to be plotted on the x-axis and since these are strings, matplotlib interprets them as a categorical variable. If you are okay with this, then each of the x-ticks will be spaced equally apart regardless of their value (but we can sort the DataFrame before passing it), and then to get the desired tick labels you can take the portion of the string before the colon symbol (for example '20' from the string '20:30') and add the string ' hours ago' to that string to get '20 hours ago'. Then pass each of these strings as labels to the plt.xticks method, along with the original ticks in the Clock column.

import matplotlib.pyplot as plt
import pandas as pd

## recreate the data with times unsorted 
df = pd.DataFrame({
    'ID':[260,127,7,10,8],
    'Time':['21 hours','21 hours','5 hours','6 hours','6 hours'],
    'Clock':['20:30','20:30','12:30','11:30','11:30',]
    })

df_sorted = df.sort_values(by='Clock',ascending=False)
plt.scatter(df_sorted['Clock'], df_sorted['ID'])

## avoid plotting duplicate clock ticks over itself
df_clock_ticks = df_sorted.drop_duplicates(subset='Clock')

## take the number of hours before the colon, and then add hours ago
plt.xticks(ticks=df_clock_ticks['Clock'], labels=df_clock_ticks['Clock'].str.split(':').str[0] + ' hours ago')
plt.show()

enter image description here