Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

enter image description here

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.

Post a Comment for "Format Pd.interval Categories When Plotting"