1

I'm looking to apply a diverging colour palette to a matplotlib.pyplot.hist()that uses each column in a panda dataFrame but I'm a bit lost.

Here's the code (with some reproducible data) I have so far:

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

sns.set(font_scale = 0.6)

df = pd.DataFrame([[5, 5, 1.5], [1.8, 2.2, 1], [4.7, 3.6, 2],[3.4, 2.1, 1.8],[3.8, 2, 0.5], 
[3.6, 3.5, 2.5]], columns=['overall', 'astro', 'fake'])


df.hist()
plt.tight_layout()
plt.show()

Ideally I would get to a similar point as the colour palette in the graph below from this question that I had no luck finding a solution in. Any help/resources, much appreciated.

enter image description here

gzpach
  • 65
  • 6

1 Answers1

1

You could iterate through the generated axes, and then iterate through their bars to asign new colors:

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

sns.set(font_scale = 0.6)

df = pd.DataFrame([[5, 5, 1.5], [1.8, 2.2, 1], [4.7, 3.6, 2], [3.4, 2.1, 1.8], [3.8, 2, 0.5], [3.6, 3.5, 2.5]],
                  columns=['overall', 'astro', 'fake'])
axes = df.hist()
cmap = plt.cm.Spectral_r
for ax in axes.ravel():
    if len(ax.containers) > 0:
        num_bars = len(ax.containers[0])
        for bar, val in zip(ax.containers[0], np.linspace(0, 1, num_bars)):
            bar.set_color(cmap(val))
plt.tight_layout()
plt.show()

example plot

To only get a subset of the color range, instead of the full range (np.linspace(0, 1, num_bars)), a smaller range could be used:

cmap = plt.cm.RdYlBu_r
for ax in axes.ravel():
    if len(ax.containers) > 0:
        num_bars = len(ax.containers[0])
        for bar, val in zip(ax.containers[0], np.linspace(0.2, 0.8, num_bars)):
            bar.set_color(cmap(val))
JohanC
  • 71,591
  • 8
  • 33
  • 66
  • fantastic - thanks. The only adjustment I made was to introduce additional bins but that is purely cosmetic. – gzpach Jan 07 '21 at 13:09