Pandas boxplot: set color and properties for box, median, mean

后端 未结 4 1690
南旧
南旧 2020-12-16 03:10

I have a DataFrame with a MultiIndex:

# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd

# dataframe with dates
dates = pd.DataFrame()
dates[\'2         


        
4条回答
  •  轮回少年
    2020-12-16 04:12

    I just found another solution to plot with much less code directly from pandas (without having to manipulate the matplotlib-object afterwards):

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    
    df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
    ax = df.plot(kind='box',
                 color=dict(boxes='r', whiskers='r', medians='r', caps='r'),
                 boxprops=dict(linestyle='-', linewidth=1.5),
                 flierprops=dict(linestyle='-', linewidth=1.5),
                 medianprops=dict(linestyle='-', linewidth=1.5),
                 whiskerprops=dict(linestyle='-', linewidth=1.5),
                 capprops=dict(linestyle='-', linewidth=1.5),
                 showfliers=False, grid=True, rot=0)
    ax.set_xlabel('Foo')
    ax.set_ylabel('Bar in X')
    plt.show()
    

    yields:

    The only thing I haven't figured out is how to adjust the color of the means when showmeans=True. But in most cases this should be fine..

    Hope it helps!

提交回复
热议问题