I am trying to change the colorbar
on my networkx
plot. The bar gets extra wide and also smooshes my original networkx
plot (left) when I add the colorbar
on there (right).
How can I make my colorbar
thinner and not alter my original networkx
graph?
My code below with colorbar
courtesy of https://stackoverflow.com/users/5285918/lanery but used w/ a larger network.
# 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
fig, ax = plt.subplots()
nx.draw(G, node_color=[cmap[node] for node in G.nodes()], with_labels=True, ax=ax)
sm = plt.cm.ScalarMappable(cmap=ListedColormap(color_palette),
norm=plt.Normalize(vmin=0, vmax=3))
sm._A = []
fig.colorbar(sm, ticks=range(1,4))