How to stop `colorbar` from reshaping `networkx` plot? (Python 3)


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).

<strong>How can I make my colorbar thinner and not alter my original networkx graph?</strong>

My code below with colorbar courtesy of <a href="https://stackoverflow.com/users/5285918/lanery" rel="nofollow">https://stackoverflow.com/users/5285918/lanery</a> 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))

<a href="https://i.stack.imgur.com/g3vG8.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/g3vG8.png" data-original="https://i.stack.imgur.com/g3vG8.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>


Easiest way should be to plot the colorbar on its own axis and play around with the [left, bottom, width, height] parameters.

cbaxes = fig.add_axes([0.9, 0.1, 0.015, 0.8]) sm = plt.cm.ScalarMappable(cmap=ListedColormap(color_palette), norm=plt.Normalize(vmin=0, vmax=3)) sm._A = [] plt.colorbar(sm, cax=cbaxes, ticks=range(4))

Source: <a href="https://stackoverflow.com/questions/13310594/positioning-the-colorbar" rel="nofollow">positioning the colorbar</a>


  • filter and unfilter in dplyr
  • Preprocessing csv files to use with tflearn
  • R do something after a warning (like tryCatch a warning, then edit an object)
  • ANOVA on multiple responses, by multiple groups NOT part of formula
  • dynamic cast not working for non polymorphic base class?
  • How to do “(df1 & not df2)” dataframe merge in pandas?
  • iPython Notebook not printing Dataframe as table
  • How to remove just the index name and not the content in Pandas multiindex data frame
  • Pandas - find nearest dates between two DataFrames without loop
  • Replace any string in columns with 1
  • How to make Plotly chart with year mapped to line color and months on x-axis
  • What's a fast (non-loop) way to apply a dict to a ndarray (meaning use elements as keys and rep
  • Transpose table then set and rename index
  • How to load Q library with Require.js?
  • How to filter on year and quarter in pandas
  • Color time-series based on column values in pandas
  • Find 5 consecutive numbers in numpy array by row, ignore duplicates
  • Put value at centre of bins for histogram
  • Primefaces :radioButton inside a ui:repeat
  • Private IP address in reserved subnet range
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • Make new pandas columns based on pipe-delimited column with possible repeats
  • Breaking out column by groups in Pandas
  • Unable to get column index with table.getColumn method using custom table Model
  • xtable package: Skipping some rows in the output
  • Error when parsing timestamp with pandas read_csv
  • script to move all files from one location to another location
  • Adding custom controls to a full screen movie
  • Rearranging Cells in UITableView Bug & Saving Changes
  • retrieve vertices with no linked edge in arangodb
  • Benchmarking RAM performance - UWP and C#
  • Error creating VM instance in Google Compute Engine
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • reshape alternating columns in less time and using less memory