Bigger color-palette in matplotlib for SciPy's dendrogram (Python)

做~自己de王妃 提交于 2019-12-08 17:50:35

Most matplotlib colormaps will give you a value given a value between 0 and 1. For example,

import matplotlib.pyplot as plt
import numpy as np
print [plt.cm.Greens(i) for i in np.linspace(0, 1, 5)]

will print

[(0.9686274528503418, 0.98823529481887817, 0.96078431606292725, 1.0),
 (0.77922338878407194, 0.91323337695177864, 0.75180316742728737, 1.0),
 (0.45176470875740049, 0.76708959481295413, 0.46120723030146432, 1.0),
 (0.13402538141783546, 0.54232989970375511, 0.26828144368003398, 1.0),
 (0.0, 0.26666668057441711, 0.10588235408067703, 1.0)]

So you no longer need to be restricted to values provided to you. Just choose a colormap, and get a color from that colormap depending upon some fraction. For example, in your code, you could consider,

for xs, ys in zip(icoord, dcoord):
    color = plt.cm.Spectral( ys/6.0 )
    plt.plot(xs, ys,  color)

or something to that effect. I am unsure how exactly you want to display your colors, but I am sure you can modify your code very easily for achieving any color combinations you want ...

Another thing you can try is

N = D_dendro["color_list"]
colorList = [ plt.cm.Spectral( float(i)/(N-1) )  for i in range(N)]

and pass on that colorList.

Play around a bit ...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!