Matplotlib/seaborn histogram using different colors for grouped bins

前端 未结 4 1411
谎友^
谎友^ 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:30

    Solution:

    N, bins, patches = plt.hist(df1['Average'], 30)
    
    cmap = plt.get_cmap('jet')
    low = cmap(0.5)
    medium =cmap(0.2)
    high = cmap(0.7)
    
    
    for i in range(0,3):
        patches[i].set_facecolor(low)
    for i in range(4,13):
        patches[i].set_facecolor(medium)
    for i in range(14,30):
        patches[i].set_facecolor(high)
    
    plt.xlabel("Watt Hours", fontsize=16)  
    plt.ylabel("Households", fontsize=16)
    plt.xticks(fontsize=14)  
    plt.yticks(fontsize=14)
    ax = plt.subplot(111)  
    ax.spines["top"].set_visible(False)  
    ax.spines["right"].set_visible(False)
    
    plt.show()
    

    output:

提交回复
热议问题