Histograms with Colors by Values
xxxxxxxxxx
dist = RealDistribution('gaussian', 1)
data = [dist.get_random_element() for _ in range(1000)]
n, x, y = 30, [], []
sdata = sorted(data)
dx = (sdata[-1] - sdata[0])/n
x = [[sdata[0]+dx*i, sdata[0]+dx*(i+1)] for i in range(n)]
i, c = 0, 0
while i < n - 1:
if sdata[0] > x[i+1][1]: y.append(0); i += 1; c = 0
elif sdata[0] > x[i][1]: y.append(c); i += 1; c = 0
else: sdata = sdata[1:]; c += 1
y.append(1000-sum(y))
col = lambda i, y: colormaps.RdBu(int(255*y[i]/max(y)))[:3]
g = Graphics(); p = .02
for i in range(n):
g += polygon([[x[i][0]+p, 0], [x[i][0]+p, y[i]],
[x[i][1]-p, y[i]], [x[i][1]-p, 0]],
color=col(i,y), aspect_ratio=.05, frame=True)
save(g, "SagePlots.png")
show(g)
xxxxxxxxxx
%%r
data <- rnorm(1000)
n <- 30
h <- hist(data, breaks=n, plot=FALSE)
pal <- colorRamp(c("midnightblue", "steelblue", "bisque", "firebrick"))
fcol <- function(x) {rgb(pal((x - min(x)) / diff(range(x))), max=255)}
fn<-'Rplots.png'
png(fn, pointsize=12, bg='whitesmoke')
plot(h, col=fcol(h$density))
dev.off()
xxxxxxxxxx
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
data = np.random.randn(1000)
n = 30
hist, bins = np.histogram(data, bins=n, density=True)
colors = ['midnightblue', 'steelblue', 'bisque', 'firebrick']
cmap = LinearSegmentedColormap.from_list('custom_cmap', colors)
plt.bar(bins[:-1], hist, width=np.diff(bins)*.9,
color=cmap((hist-hist.min())/(hist.max()-hist.min())))
plt.savefig('PythonPlots.png')
plt.show()