[Python] Maplotlib BoxPlot Facecolor by quartile

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    I am using the example from the docs (with a slightly modified data structure):

    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd

    data = pd.DataFrame({"peaches":[1,4,7,5,8,2,5,7], "oranges":[2,7,9,1,9,2,7,3], "tomato":[3,8,5,4,5,6,7,1]})
    labels = ['peaches', 'oranges', 'tomatoes']
    colors = ['peachpuff', 'orange', 'tomato']

    fig, ax = plt.subplots()
    ax.set_ylabel('fruit weight (g)')

    bplot = ax.boxplot(data,
    patch_artist=True, # fill with color
    tick_labels=labels) # will be used to label x-ticks

    plt.show()


    What I need to do is color the top part of each box (I.E. median to Q3) with one shade of blue, and the bottom part (median to Q2) with a deeper shade of blue (ignore the colors given in the example).

    I think this is achieved by iterating through and adding a rectangle using ax.add.patch(plt.Rectangle).

    But as a relative beginner in this area I can't work out how to iterate through the data and the boxplot. Please can anyone assist?

    Continue reading...

Compartilhe esta Página