Based on the data in the Table , an ordered schedule of boxes with a mustache was constructed. However, when adding the function of the average (line 11), the graph shifts, why?

import pandas as pd import matplotlib.pyplot as plt df = pd.read_excel(r'C:\Users\cinem\Desktop\нир\3-6.xlsx') r = df.pivot_table(columns='town_name', index='cld_year', values='относительная заболеваемость на 1000 чел.', aggfunc='mean', fill_value=0) aq = r.median().sort_values().index ae = r.mean().sort_values().index r[aq].plot.box(rot=90, figsize=(16,10)) r[ae].mean().plot(kind='line', color='orange', rot=90) plt.ylabel('Относительная заболеваемость на 1000 чел.') plt.xlabel('город') plt.title('Относительная заболеваемость на 1000 человек в городах РФ') plt.tight_layout() plt.show() 

Medium Without average

    1 answer 1

     r = df.pivot_table(columns='town_name', index='cld_year', values='относительная заболеваемость', aggfunc='mean', fill_value=0) idx = r.mean().sort_values().index r = r[idx] fig, ax = plt.subplots(figsize=(16,10)) r.plot.box(rot=90, ax=ax) ax2 = ax.twinx() ax2.plot(ax.get_xticks(), r.mean(), color='orange') ax2.plot(ax.get_xticks(), r.median(), color='green') ax2.set_ylim(ax.get_ylim()) plt.legend(['mean','median']) 

    enter image description here