Barplot colored according a colormap?
Seaborn barplot
is great for this, example:
ax = sns.barplot("size", y="total_bill", data=tips, palette="Blues_d")
To obtain a barplot with the bars colored according to a colormap you can use the color
argument of bar(x,y, color=colors)
, where colors
is a list of length number of bars, containing all the colors. I.e. the i
th entry in that list is the color for the i
th bar.
In order to create this list from the colormap, you need to call the colormap with the respective value.
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np
clist = [(0, "red"), (0.125, "red"), (0.25, "orange"), (0.5, "green"),
(0.7, "green"), (0.75, "blue"), (1, "blue")]
rvb = mcolors.LinearSegmentedColormap.from_list("", clist)
N = 60
x = np.arange(N).astype(float)
y = np.random.uniform(0, 5, size=(N,))
plt.bar(x,y, color=rvb(x/N))
plt.show()