Q:

Format pd.Interval categories when plotting

I have similar question like this one : question
Referring the code from above post.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

my_list = [1,2,3,4,5,7,8,9,11,23,56,78,3,3,5,7,9,12]

new_list = pd.Series(my_list)

df1 = pd.DataFrame({'Range1':new_list.value_counts().index, 'Range2':new_list.value_counts().values})

df1.sort_values(by=["Range1"],inplace=True)

df2 = df1.groupby(pd.cut(df1["Range1"], [0,1,2,3,4,5,6,7,8,9,10,11,df1['Range1'].max()])).sum()

objects = df2['Range2'].index

y_pos = np.arange(len(df2['Range2'].index))

but want the following sequence on x-axis:
Expected output:
(00,01] (01,02] (02,03] (03,04]......

Any help in getting the expected output?

A:

It isn't straightforward, but it is doable. You will need to format the left and right intervals separately.
l = df2.index.categories.left.map("{:02d}".format)
r = df2.index.categories.right.map("{:02d}".format)

plt.bar(range(len(df2)), df2['Range2'].values, tick_label='('+l+', '+r+']')
plt.xticks(fontsize=6)

plt.show()

Where,
print('('+l+', '+r+']')
Index(['(00, 01]', '(01, 02]', '(02, 03]', '(03, 04]', '(04, 05]', '(05, 06]',
       '(06, 07]', '(07, 08]', '(08, 09]', '(09, 10]', '(10, 11]', '(11, 78]'],
      dtype='object')

You may have to change the brackets depending on whether your intervals are closed on the left, on the right, or neither.

