The Biotech
The Biotech

Reputation: 27

Matplotlib - can't set the size of my colobar with a broken axis scatter plot

I am trying to set my colorbar to the same lenght as my broken axis scatter plot but I can't seem to make it worked. I have tried to use

divider = make_axes_locatable(ax2)
cax = divider.append_axes("right",size='5%', pad=0.05) 

but I get a super long colorbar. This is my code below

fig, (ax, ax2) = plt.subplots(1, 2, sharey=True, gridspec_kw={'width_ratios': [6, 1]})


scatter = ax.scatter(Coef_fam_cd,Coef_member_cd,c=lenght_cd,cmap='viridis_r')
ax2.scatter(Coef_fam_cd,Coef_member_cd,c=lenght_cd,cmap='viridis_r')
ax.set_xlim(0,5)
ax2.set_xlim(18.9,20)

# hide the spines between ax and ax2
ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax.yaxis.tick_left()
ax.tick_params(labelright=False)
ax2.yaxis.tick_right()

# This looks pretty good, and was fairly painless, but you can get that
# cut-out diagonal lines look with just a bit more work. The important
# thing to know here is that in axes coordinates, which are always
# between 0-1, spine endpoints are at these locations (0,0), (0,1),
# (1,0), and (1,1).  Thus, we just need to put the diagonals in the
# appropriate corners of each of our axes, and so long as we use the
# right transform and disable clipping.

d = .01 # how big to make the diagonal lines in axes coordinates
# arguments to pass plot, just so we don't keep repeating them
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((1-d,1+d), (-d,+d), **kwargs)
ax.plot((1-d,1+d),(1-d,1+d), **kwargs)

kwargs.update(transform=ax2.transAxes)  # switch to the bottom axes
ax2.plot((-d,+d), (1-d,1+d), **kwargs)
ax2.plot((-d,+d), (-d,+d), **kwargs)



cbar = plt.colorbar(scatter)




cbar.ax.tick_params(rotation=90)
cbar.ax.set_ylabel('Number of members in a family')
plt.ylim(0,5)
fig.subplots_adjust(right=1)


ax2.yaxis.set_visible(False)

plt.show()

my graph

thank you for your help.

Upvotes: 0

Views: 102

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40727

I would suggest creating the axes for your colorbar directly in the call to subplots(). More customized layouts could be constructed using GridSpec

x,y,z = np.random.random(size=(3,100))

fig, (ax, ax2, cax) = plt.subplots(1,3, gridspec_kw=dict(width_ratios=[6,1,0.35]))
ax.set_xlim(0,5)
ax2.set_xlim(18.9,20)

# hide the spines between ax and ax2
ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax.yaxis.tick_left()
ax.tick_params(labelright=False)
ax2.yaxis.tick_right()

d = .01 # how big to make the diagonal lines in axes coordinates
# arguments to pass plot, just so we don't keep repeating them
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((1-d,1+d), (-d,+d), **kwargs)
ax.plot((1-d,1+d),(1-d,1+d), **kwargs)

kwargs.update(transform=ax2.transAxes)  # switch to the bottom axes
ax2.plot((-d,+d), (1-d,1+d), **kwargs)
ax2.plot((-d,+d), (-d,+d), **kwargs)

sc = ax.scatter(x,y, c=z)

fig.colorbar(sc, cax=cax)
cax.set_ylabel('Number of members in a family')

enter image description here

Upvotes: 3

Related Questions