Skip to content

Instantly share code, notes, and snippets.

@basavyr
Created December 16, 2025 08:10
Show Gist options
  • Select an option

  • Save basavyr/4cd06a564b3be8d4f47c927262e5da88 to your computer and use it in GitHub Desktop.

Select an option

Save basavyr/4cd06a564b3be8d4f47c927262e5da88 to your computer and use it in GitHub Desktop.
Use Matplotlib annotations with relative percentages as alignment

How to set plot annotations via plt.text() using percentages for the x-y location within the figure.

fig, ax = plt.subplots()

    # plt.plot(phi_1s, T1s, '-ro', label=r'$T_1-1axis$')
    plt.plot(phi_1s, T2s(x1_const), '-bo', label=r'$T_2-1axis$')
    plt.plot(phi_1s, T2s(5), '-ro', label=r'$T_2-1axis$')
    plt.plot(phi_1s, T2s(10), '-ko', label=r'$T_2-1axis$')
    plt.xlabel(r'$\varphi_1$')
    plt.ylabel(r'$T_k$')

    # plt.ylim((-20, 20))
    ax.legend(loc='best')
    fig.tight_layout()
    plt.text(0.20, 0.20, r'$x_1$=' + f' {x1_const}', horizontalalignment='center',
             verticalalignment='center', transform=ax.transAxes, fontsize=11)
    plt.savefig('plots/T1-Axis.pdf', bbox_inches='tight', dpi=300)
    plt.close()

Full code available here.

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