Matplotlib/seaborn histogram using different colors for grouped bins

前端 未结 4 1378
谎友^
谎友^ 2021-01-12 16:09

I have this code, using a pandas df:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os


path_to = \'Data\\\\2017-04\\\\Mon         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-01-12 16:12

    A more readable general solution without using the cmap as you want just 3 colors for specific intervals.

    n, bins, patches = plt.hist(df1['Average'], 30)
    
    for c, p in zip(bins, patches):
        if c > 0 and c < 4:
            plt.setp(p, 'facecolor', 'green')
        elif c >= 4 and c < 14  :
            plt.setp(p, 'facecolor', 'blue')
        else c>=14:
            plt.setp(p, 'facecolor', 'yellow')
    
    plt.show()
    

提交回复
热议问题