Home > Mobile >  Position label of colorbar
Position label of colorbar

Time:08-02

I have this function:

def scatter_diagdistance(x, y) :
    z = abs(y-x)
    fig, ax = plt.subplots(dpi=200)
    sc = ax.scatter(x, y, c=z, s=50, edgecolor='none')
    x_diag = np.arange(min(x*100), max(x*100))/100
    ax.plot(x_diag, x_diag, '-', c="red")
    cbar = fig.colorbar(sc)
    cbar.set_label('Distance from diagonal')
    return(fig)

Which gives me this sort of image: figure generated by the code snippet 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?)

CodePudding user response:

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 enter image description here

  • Related