0

I'd like to show the mean value in each boxplot. How can I add them?

I used the next code and I had this message:

TypeError: cannot convert the series to <class 'float'>

Thanks!

data_of=pd.read_csv(filename,header=0)

ax=plt.subplots(figsize=(20,8))
meanprops=dict(marker='D', markeredgecolor='black',markerfacecolor='black',markersize=10)
flierprops=dict(marker='.', markeredgecolor='black',markerfacecolor='white',markersize=10)

ax = sns.boxplot(x='MES', y='Finos', data=data_of, hue='ANO',linewidth=1,palette="Spectral_r",saturation=1,width=0.8,dodge=True
                 ,showfliers=True,showmeans=True, meanprops=meanprops,flierprops=flierprops)

means = pd.DataFrame(data_of.groupby(['MES', 'ANO'])['Finos'].mean())
means.reset_index(inplace=True)

n = 0
for MES in means['MES'].unique():
    valor = float(means.loc[(means.MES == MES) & (means.ANO == 'Yes')]['Finos'])
    plt.text(n+0.1, valor, "{:.2f}".format(valor))

    valor = float(means.loc[(means.MES == MES) & (means.ANO == 'No')]['Finos'])
    plt.text(n-0.3, valor, "{:.2f}".format(valor))

    n += 1

enter image description here

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
bramil
  • 1
  • 2
  • 1
    (1) please add some sample data, so that one can easily reproduce your issue; (2) aren't the diamond shapes in your diagram the mean? – Roy2012 Jul 20 '20 at 16:11
  • Add code, **the entire error**, and **data** as text, not screenshots because [Stack Overflow Discourages Screenshots](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). It is likely the question will be down-voted and closed. You are discouraging assistance because no one wants to retype your data or code, and screenshots are often illegible. [edit] the question and add text. – Trenton McKinney Jul 20 '20 at 16:30
  • Please [create a reproducible copy of the DataFrame with `df.head(20).to_clipboard(sep=',')`](https://stackoverflow.com/questions/52413246/how-to-provide-a-copy-of-your-dataframe-with-to-clipboard), **[edit] your question**, and paste the clipboard into a code block or include synthetic data: [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Trenton McKinney Jul 20 '20 at 16:30
  • `valor = float(means.loc[(means.MES == MES) & (means.ANO == 'Yes')]['Finos']) plt.text(n+0.1, valor, "{:.2f}".format(valor))` is not how to convert column type in pandas. [Change data type of columns in Pandas](https://stackoverflow.com/questions/15891038/change-data-type-of-columns-in-pandas) – Trenton McKinney Jul 20 '20 at 16:34

0 Answers0