I have this function:
JavaScript
x
10
10
1
def scatter_diagdistance(x, y) :
2
z = abs(y-x)
3
fig, ax = plt.subplots(dpi=200)
4
sc = ax.scatter(x, y, c=z, s=50, edgecolor='none')
5
x_diag = np.arange(min(x*100), max(x*100))/100
6
ax.plot(x_diag, x_diag, '-', c="red")
7
cbar = fig.colorbar(sc)
8
cbar.set_label('Distance from diagonal')
9
return(fig)
10
Which gives me this sort of image:
How can I position the “Distance from diagonal” to the left of the colorbar?
(Also, is there a cleaner way to plot the diagonal over a scatter plot like this?)
Advertisement
Answer
one way to do it is to use the text as the label for the secondary y-axis. That will keep the text before the colorbar. Also, you can draw a line for the diagonal. The code (without your data) is shown below. If you use transform=ax.transAxes
details, the coordinates are interpreted as axes coordinates
JavaScript
1
8
1
fig, ax = plt.subplots(dpi=200)
2
ax2 = ax.twinx() ##Create secondary axis
3
ax2.set_yticks([]) ##No ticks for the secondary axis
4
sc = ax.scatter(0.5, 0.5, c=1, s=50, edgecolor='none')
5
ax2.set_ylabel('Distance from diagonal') ##Label for secondary axis
6
ax.plot([0, 1], [0, 1], '-', c="red", transform=ax.transAxes) #Line from 0 to 1
7
cbar = fig.colorbar(sc)
8
Plot