我嘗試添加colorbar
我networkx
繪製matplotlib ax
從1
範圍(最亮)和3
(即最黑暗的)檢查出的線W /下面cmap
。我試圖結合很多PyData
功能。如何使用`seaborn`調色板將`colorbar`添加到`networkx`? (Python 3中)
如何使用seaborn調色板在networkx圖上添加顏色條類型功能?
# Set up Graph
DF_adj = pd.DataFrame(np.array(
[[1, 0, 1, 1],
[0, 1, 1, 0],
[1, 1, 1, 1],
[1, 0, 1, 1] ]), columns=['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'], index=['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'])
G = nx.Graph(DF_adj.as_matrix())
G = nx.relabel_nodes(G, dict(zip(range(4), ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'])))
# Color mapping
color_palette = sns.cubehelix_palette(3)
cmap = {k:color_palette[v-1] for k,v in zip(['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'],[2, 1, 3, 2])}
# Draw
nx.draw(G, node_color=[cmap[node] for node in G.nodes()], with_labels=True)
在這方面,它們都使用matplotlib
調色板:http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut3.html我甚至嘗試將它們轉換爲一個ListedColormap
對象,但沒有奏效。
這並不適用於我的情況無論是B/C matplotlib顏色映射工作:Seaborn regplot with colorbar?
同爲http://matplotlib.org/examples/pylab_examples/colorbar_tick_labelling_demo.html
這是我得到的最接近,但它沒有工作,我得到了一個自動縮放Nonetype:How do I use seaborns color_palette as a colormap in matplotlib?
不'nx.draw'回報藝術家創造的? – tacaswell
'nx.draw'不會返回任何東西(至少在本例中不是這樣),這就是爲什麼我會回答我的答案。 – lanery