【发布时间】:2016-09-21 22:26:00
【问题描述】:
我正在尝试从1(最轻)和3(最暗)的范围内将colorbar 添加到我的networkx 绘制的matplotlib ax [查看带有/ cmap 下面]。我正在尝试结合很多PyData 功能。
如何使用 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不返回创作的艺术家吗? -
nx.draw没有返回任何东西(至少在这种情况下没有),这就是我回答的原因。
标签: python matplotlib colors networkx seaborn