WhitakerLab / scona

Code to analyse structural covariance brain networks using python.
https://whitakerlab.github.io/scona/
MIT License
68 stars 33 forks source link

Add padding to the colorbar at the bottom #126

Open wingedRuslan opened 5 years ago

wingedRuslan commented 5 years ago

Hi @KirstieJane,

I tried different things to make space below the colorbar but without any positive results :( image

# create figure and axes for the nodes and edges plotting
fig, ax = plt.subplots(figsize=(11,15))

nx.draw_networkx_nodes(... , ax=ax)       # plot nodes on the ax-axes
nx.draw_networkx_edges(... , ax=ax)       # plot esges on the ax-axes

Now to draw colorbar, we need create a new axes and create colobar on this axes

# add new axes to below the existed one (ax)
ax2 = fig.add_axes([0.25,0.05,0.5,0.02], xticklabels=[], yticklabels=[])

# create colorbar
cb = mpl.colorbar.ColorbarBase(ax2, cmap="hot_r",
                                norm=norm,
                                ticks=ticks,
                                format='%.2f',
                                orientation='horizontal')
# set label 
cb.set_label(measure, size=20)
# set the size of ticks
ax2.tick_params(labelsize=20)

1) Increase the size of the figure. Even though it is not a good solution, it does not work. It simply makes the figure with bigger height (no space at the bottom added) 2) plt.tight_layout() - shows warning that it can not be applied to the new axes (still no space) /home/pilot/anaconda3/lib/python3.6/site-packages/matplotlib/figure.py:1999: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect. warnings.warn("This figure includes Axes that are not compatible "
3) plt.subplots_adjust(bottom=0.2) - this "lifts up" the main plot, but not the colorbar (so the space between the main plot and colorbar increases) 4) axes.margins() - does not change anything

I might miss something obvious though...

Without adding spacing below, it does not make sense to commit, right?

wingedRuslan commented 5 years ago

create subplot for the brain plotting

fig, ax = plt.subplots(figsize=(9,12))

# And then loop through each node and add it in order
for node in node_order:
    aaa = nx.draw_networkx_nodes(H,                                  # A networkx graph
                            pos=pos,                           # A dict with nodes as keys and positions as values.
                            node_color=colors_list[node],       # color for each node
                            nodelist=[node],                   # Draw only this one node
                            ax=ax)

nx.draw_networkx_edges(G2,   # plotting for thresholded edges G2
                        pos=pos,
                        edgelist=edge_list_G2,
                        edge_color = "lightgrey",
                        ax=ax)

add subplot - basically adds an Axes to the figure as part of a subplot arrangement.

ax2 = fig.add_subplot(15, 1, 15, xticklabels=[], yticklabels=[])

vmin = min(nodal_measures[measure].values)
vmax = max(nodal_measures[measure].values)
norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax)

# ticks = [round(vmin,2), round((vmin+vmax)/2,2), round(vmax,2)]
ticks = [vmin, (vmin+vmax)/2, vmax]

cb = mpl.colorbar.ColorbarBase(ax2, cmap="hot_r",
                                norm=norm,
                                ticks=ticks,
                                format='%.2f',
                                orientation='horizontal')

cb.set_label(measure, size=20)
ax2.tick_params(labelsize=20)

plt.tight_layout()

plt.subplots_adjust(top=1)

sns.despine(top=True, right=True, left=True, bottom=True)

fig.savefig("CCC", bbox_inches=0, dpi=100)
KirstieJane commented 5 years ago

Try adding a grid. Subplots are really hard to adjust and control if you go outside of what they’re designed to do (which is have multiple similar sized plots).

KirstieJane commented 5 years ago

Here’s some code on setting up a grid rather than a subplot: https://github.com/WhitakerLab/scona/blob/1e70f46ec3f5ebc595cb75837bb5ea235ec2b149/scona/scripts/make_figures.py#L1890