Home > OS >  Adding unique colors for each bar of a multi-histogram grid plot in Python
Adding unique colors for each bar of a multi-histogram grid plot in Python

Time:12-03

I am trying to create a multi-histogram plot in Python using Matplotlib with each class ( bar ) having a unique color.

I was able to achieve the graph but I am unable to get the bar's colors working. Here is my code:

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

def draw_histograms(df, variables, n_rows, n_cols):
    fig=plt.figure() # facecolor='g' changes the background color
    for i, var_name in enumerate(variables):
        ax=fig.add_subplot(n_rows,n_cols,i 1)
        df[var_name].hist(ax=ax)

    fig.tight_layout()
    fig.set_size_inches(20.5, 10.5)
    plt.show()

draw_histograms(df_final, df_final.columns, 2, 3)

What this looks like: Multi-histogram plot

Adding "facecolor" to plt.figure(), changes the background color.

My dataframe:

DF

What I am trying to achieve: For each column in DF, I display a graph (total 6). The 3 bars in each histogram depict the sentiments - positive, negative and neutral. I want to have 3 unique colors to the bars depicting different sentiments across all 6 graphs.

CodePudding user response:

You are drawing a histogram, while you only have 3 values, creating a bar of height 1 for each of these values (or of height 2 when 2 values are very close). It would be clearer to just draw this as a bar plot, which does allow one color per bar. To make things comparable, it could be useful to set the same x-limits on all subplots:

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

def draw_histograms(df, variables, n_rows, n_cols, bar_width=1, xmin=0, xmax=None):
    fig = plt.figure(figsize=(20.5, 10.5))
    for i, var_name in enumerate(variables):
        ax = fig.add_subplot(n_rows, n_cols, i   1)
        # df[var_name].plot.bar(ax=ax, color=['crimson', 'gold', 'limegreen'], rot=0)
        ax.bar(x=df[var_name], height=1, width=bar_width, color=['crimson', 'gold', 'limegreen'])
        ax.set_xlim(xmin, xmax   bar_width)
        for spine in ['right', 'top']:
            ax.spines[spine].set_visible(False)
    fig.tight_layout()
    plt.show()

df = pd.DataFrame([[1186, 181, 1960, 955, 2263, 2633],
                   [664, 171, 463, 723, 381, 697],
                   [570, 152, 336, 544, 269, 492]],
                  index=['negative', 'neutral', 'positve'])
bar_width = 30
xmax = df.max().max()
draw_histograms(df, df.columns, 2, 3, bar_width=bar_width, xmax=xmax)

bar plot with colors per bar

def draw_histograms(df, variables, n_rows, n_cols, ymin=0, ymax=None):
    fig = plt.figure(figsize=(20.5, 10.5))
    for i, var_name in enumerate(variables):
        ax = fig.add_subplot(n_rows, n_cols, i   1)
        df[var_name].plot.bar(ax=ax, color=['crimson', 'gold', 'limegreen'], rot=0)
        ax.set_ylim(ymin, ymax)
        ax.bar_label(ax.containers[0], size=16)
        for spine in ['right', 'top']:
            ax.spines[spine].set_visible(False)
    fig.tight_layout()
    plt.show()

ymax = df.max().max()
draw_histograms(df, df.columns, 2, 3, ymax=ymax * 1.1)

The same information could also be shown using the bar height showing the values, and using the x-values to show the names.

pandas bar plot

  • Related