3

I have a dataframe with ordered values with ordered index

     PERFORMER_ID   Number_of_Activities    colors
0        535    24  darkgreen
1        1FD    20  darkgreen
2        4F8    14  darkgreen
3        A8D    14  darkgreen
4        57D    11  darkgreen
5        D8B    10  darkgreen
6        064    9   darkgreen
7        D9F    9   darkgreen
8        684    9   darkgreen
9        F87    9   darkgreen
10       5A8    -1  red
11       5A6    -1  red
12       5A1    -1  red
13       59B    -1  red
14       59A    -1  red
15       587    -1  red
16       581    -1  red
17       578    -1  red
18       574    -1  red
19       7C3    -1  red

I'm trying to diverging plot with the above data

# Draw plot
import matplotlib.patches as patches

plt.figure(figsize=(10,12), dpi= 80)
plt.hlines(y=data.index, xmin=0, xmax=data.Number_of_Activities, color=data.colors, alpha=0.4, linewidth=1)
plt.scatter(data.Number_of_Activities, data.index, color=data.colors, s=600, alpha=0.6)

for x, y, tex in zip(data.Number_of_Activities, data.index, data.Number_of_Activities):
    t = plt.text(x, y, abs(tex), horizontalalignment='center', 
                 verticalalignment='center', fontdict={'color':'white'})
    
plt.yticks(data.index, data.PERFORMER_ID)
plt.xticks(fontsize=12)



# # Add Patches
# p1 = patches.Rectangle((-2.0, -1), width=.3, height=3, alpha=.2, facecolor='red')
# p2 = patches.Rectangle((1.5, 27), width=.8, height=5, alpha=.2, facecolor='green')
# plt.gca().add_patch(p1)
# plt.gca().add_patch(p2)

# Decorate
plt.title('Performers with Most & Less Activities', fontdict={'size':12})
plt.grid(linestyle='--', alpha=0.5)
plt.show()

enter image description here

I got the plot in reverse order. How do I make it plot with the given order or how can I reverse the values in the above plot (The bottom 10 to up and top 10 to bottom)?

hrokr
  • 3,276
  • 3
  • 21
  • 39
Ailurophile
  • 2,552
  • 7
  • 21
  • 46
  • 1
    I'm not sure what the order in the group should be, but to see it in reverse, try this. `data.sort_index(ascending=False, ignore_index=True, inplace=True)` – r-beginners Jul 31 '21 at 07:38
  • it was sorted with `Number_of_Activities`, using `data.sort_values(by="Number_of_Activities", ascending=False, inplace=True)` and this is the required order that I want to plot – Ailurophile Jul 31 '21 at 07:50
  • `data.sort_index(ascending=False, ignore_index=True, inplace=True)` This worked for. Thank You @r-beginners – Ailurophile Jul 31 '21 at 08:01
  • @r-beginners Please do post it as the answer, I would be happy to accept the answer... – Ailurophile Aug 05 '21 at 16:40

1 Answers1

1

The easiest way to deal with this is to reorder and re-index the data.

data.sort_index(ascending=False, ignore_index=True, inplace=True)

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • 1
    @Pluviophile Thank you for the opportunity to respond. – r-beginners Aug 06 '21 at 00:34
  • [How to plot “Trace Explorer” in Python?](https://stackoverflow.com/questions/68608271/how-to-plot-trace-explorer-in-python), [How to calculate the Precedence Matrix in Python?](https://stackoverflow.com/questions/68669929/how-to-calculate-the-precedence-matrix-in-python?noredirect=1#comment121359606_68669929) just in case if you are interested to solve... – Ailurophile Aug 06 '21 at 02:58
  • 1
    Your question the other day made me realize for the first time that such a package exists. I don't think there is as far as I know. So I think that answer is the best. – r-beginners Aug 06 '21 at 03:19