OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

how to label mean value on seaborn boxplot?

  • Thread starter Thread starter joo
  • Start date Start date
J

joo

Guest
i made a code for boxplot in seaborn and I want to label mean value of the boxplot, not showmeans option, the "number".

enter image description here

Code:
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
import matplotlib
import math



data = {
    'GROUP': ['AAA1', 'AAA1', 'AAA1', 'AAA1'],
    'NAME': [1, 2, 3, 4],
    'QTY': [56, 43, 55, 59],
    'TEM0001': ['A52', 'B52', 'A22', 'A22'],
    'TEM0002': ['B52', 'C32', 'B56', 'B56'],
    'TEM0003': ['C52', 'D54', 'C32', 'C4'],
    'TEM0004': ['D54', 'E43', 'D33', 'D33'],
    'TEM0005': ['E43', 'T56', 'E44', 'E44'],
}

df = pd.DataFrame(data)
df

column_indices = range(3,10) 
fig, axes = plt.subplots(math.ceil( len(column_indices)/4), 4, figsize=(10,8))
axes = axes.flatten() 

for i, col_idx in enumerate(column_indices):
    df_selected = df.iloc[:, [2, col_idx]]
    bp = sns.boxplot(data=df_selected, x=df_selected.columns[1], y='QTY', hue=df_selected.columns[1], ax=axes[i])
    m1= df_selected.groupby(df_selected.columns[1])['QTY'].median().values
    
for j in range(len(column_indices), len(axes)):
    axes[j].axis('off')


plt.tight_layout()
plt.show()

labeling mean value for each boxplot.
<p>i made a code for boxplot in seaborn and I want to label mean value of the boxplot, not showmeans option, the "number".</p>
<p><a href="https://i.sstatic.net/kpU6tPb8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kpU6tPb8.png" alt="enter image description here" /></a></p>
<pre><code>
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
import matplotlib
import math



data = {
'GROUP': ['AAA1', 'AAA1', 'AAA1', 'AAA1'],
'NAME': [1, 2, 3, 4],
'QTY': [56, 43, 55, 59],
'TEM0001': ['A52', 'B52', 'A22', 'A22'],
'TEM0002': ['B52', 'C32', 'B56', 'B56'],
'TEM0003': ['C52', 'D54', 'C32', 'C4'],
'TEM0004': ['D54', 'E43', 'D33', 'D33'],
'TEM0005': ['E43', 'T56', 'E44', 'E44'],
}

df = pd.DataFrame(data)
df

column_indices = range(3,10)
fig, axes = plt.subplots(math.ceil( len(column_indices)/4), 4, figsize=(10,8))
axes = axes.flatten()

for i, col_idx in enumerate(column_indices):
df_selected = df.iloc[:, [2, col_idx]]
bp = sns.boxplot(data=df_selected, x=df_selected.columns[1], y='QTY', hue=df_selected.columns[1], ax=axes)
m1= df_selected.groupby(df_selected.columns[1])['QTY'].median().values

for j in range(len(column_indices), len(axes)):
axes[j].axis('off')


plt.tight_layout()
plt.show()
</code></pre>
<p>labeling mean value for each boxplot.</p>
 

Latest posts

Top