Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add padding to the colorbar at the bottom #126

Open
wingedRuslan opened this issue Jul 5, 2019 · 3 comments
Open

Add padding to the colorbar at the bottom #126

wingedRuslan opened this issue Jul 5, 2019 · 3 comments

Comments

@wingedRuslan
Copy link
Collaborator

wingedRuslan commented Jul 5, 2019

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
Copy link
Collaborator Author

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
Copy link
Member

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
Copy link
Member

Here’s some code on setting up a grid rather than a subplot:

grid = gridspec.GridSpec(4, 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants