0

I use this tips https://python-graph-gallery.com/58-show-number-of-observation-on-violinplot/ to add Number of observation on a violon plot.

Here is m code:

# Calculate number of obs per group & median to position labels
medians = dataset.groupby([x_attrib])[y_attrib].median().values
nobs = dataset[x_attrib].value_counts().values
nobs = [str(x) for x in nobs.tolist()]
#nobs = ["Nb: " + i for i in nobs]
nobs = [i for i in nobs]

# Add it to the plot
pos = range(len(nobs))
for tick,label in zip(pos,ax.get_xticklabels()):
   ax.text(pos[tick], medians[tick] + 0.03, nobs[tick], horizontalalignment='center', size='x-large', color='black', weight='semibold')

I plot variable with these value counts:

0    355
1    174
2    36
-1   19
3    15
4    5
...

As you can see on the plot, for -1 value: real count is 19 and the plot return 355 (count for 0 value)

enter image description here

How can i modify the code to get a good plot please? Thanks a lot. Theo

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
Theo75
  • 477
  • 4
  • 14
  • Welcome to Stack Overflow! Please take a moment to read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). You need to provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) that includes a toy dataset (refer to [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)) – Diziet Asahi Mar 22 '20 at 13:01
  • Note that addressing a list or array in Python using index `-1` gives the last element. See e.g. [this post](https://stackoverflow.com/questions/11367902/negative-list-index) – JohanC Mar 24 '20 at 19:44
  • Hi JohanC, in fact my problem is only for alphabatical variables values, it's right for number variables values. First i calculate observations number by alphabatical group: dataset.groupby(['COD_GRB']).size().values => that give me this output order [ 31, 171, 118, 99, 56, 58, 77]. On a plot it's not an ascending sort from A to Z: i have this output (from left to right): EC, PR, TE, SC, TC, LI, DI. I want the same order for grouby and x axe plot. Thanks a lot. Theo – Theo75 Mar 25 '20 at 08:30

0 Answers0