-1

I have a dataframe of hourly data for 3 years. I made a monthly boxplot of said hourly data for all 3 years combined. Now i would like to color each box with respect to the number in the RSM list. Each month has a designated number in the RSM list, that I would like to color from green (the minimum number in RSM list) to orange (the max number in RSM list). Here is my code so far:

RSM = [0.23, 0.26, 0.29, 0.42, 0.4, 0.39, 0.29, 0.29, 0.30, 0.31, 0.35, 0.30]
ax2 = df.boxplot(column=['PEF-MFE'], by='month', showfliers=False, patch_artist=True)
ax2.set_title('') 

Monthly boxplot

Andraxyz
  • 33
  • 6

1 Answers1

0

You could put a weight in the rgb values for the colors. Boxplot can receive a RGB tuple with 3 floats varying from 0.0 to 1.0.

The higher the value in RSM the closest to 1.0 is its Green component.

Orange is something between (255/255, 130/255, 0) and (255/255, 170/255, 0). In the code above you can set the "orangeness" that you like.

Here's the example:



    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.patches import PathPatch

    RSM = [0.23, 0.26, 0.29, 0.42, 0.4, 0.39, 0.29, 0.29, 0.30, 0.31, 0.35, 0.30]
    up = max(RSM)
    down = min(RSM)

    df = pd.read_csv('teste.csv', sep=';')

    greeness = [(RSM[i]-down)/up for i in range(len(RSM))]
    lowest_orange = 0.2 # the higher this value more close to green is the lowest value

    m =['Jan','Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

    bp_dict = df.boxplot(column=['PEF-MFE'],
        by="month",layout=(4,1),figsize=(6,8),
        return_type='both',
        patch_artist = True,
    )

    colors = [(1-(greeness[i] + lowest_orange),(RSM[i])/up,0) for i in range(len(RSM))]
    for row_key, (ax,row) in bp_dict.iteritems():
        ax.set_xlabel('')
        for i,box in enumerate(row['boxes']):
            box.set_facecolor(colors[i])
        ax.set_xticklabels(m)

    plt.show()

And the result:

Result

  • I just found out that pandas boxplot is different from matplotlibs boxplot, so that the set facecolor argument doesnt work. I found something about it here: https://stackoverflow.com/questions/50963960/change-color-of-individual-boxes-in-pandas-boxplot-subplots ... I managed to change a color of a specific box now, but i still need to map the colors and add a legend – Andraxyz Dec 17 '18 at 00:24
  • So, this example: https://stackoverflow.com/questions/50963960/change-color-of-individual-boxes-in-pandas-boxplot-subplots worked to change the colors properly for you? If you could change the colors list and the column names in this example the problem is solved? – Josivan Medeiros Dec 17 '18 at 00:45
  • Ok, now I think its pretty close to what you mean. I added the month names as the xticks with "ax.set_xticklabels(m)". Checkout if it works for you, I tested it with a csv file and worked. – Josivan Medeiros Dec 17 '18 at 01:36
  • Damn, it worked, thank you! Do you mind me asking, what was I doing wrong so that I couldnt change the color of a single box? – Andraxyz Dec 17 '18 at 01:48
  • Did you change the colors list? Maybe was it. – Josivan Medeiros Dec 17 '18 at 03:30