【macos】matplotlib绘图


def plot_certified_accuracy_per_sigma_best_model(outfile: str, title: str, max_radius: float,
                            methods: List[Line]=None, label='Ours', methods_base: List[Line]=None, label_base='Baseline', radius_step: float = 0.01, upper_bounds=False, sigmas=[0.25]) -> None:
    color = ['b', 'orange', 'g', 'r']

    ## patch for macos
    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)

    for it, sigma in enumerate(sigmas):
        methods_sigma = [method for method in methods if '{:.2f}'.format(sigma) in method.quantity.data_file_path]
        accuracies_cert_ours, radii = _get_accuracies_at_radii(methods_sigma, 0, max_radius, radius_step)
        accuracies_cert_ours = np.nan_to_num(accuracies_cert_ours, -1)
        ax.plot(radii, accuracies_cert_ours[accuracies_cert_ours[:,0].argmax(), :], color[it], label='{}|$\sigma = {:.2f}$'.format(label, sigma))
        # plt.plot(radii, accuracies_cert_ours[accuracies_cert_ours[:,0].argmax(), :], color[it], label='{}|$\sigma = {:.2f}$'.format(label, sigma))
    for it, sigma in enumerate(sigmas):
        methods_sigma_base = [method for method in methods_base if '{:.2f}'.format(sigma) in method.quantity.data_file_path]
        accuracies_cert_ours, radii = _get_accuracies_at_radii(methods_sigma_base, 0, max_radius, radius_step)
        accuracies_cert_ours = np.nan_to_num(accuracies_cert_ours, -1)
        ax.plot(radii, accuracies_cert_ours[accuracies_cert_ours[:,0].argmax(), :], color[it], dashes=[2, 2], label='{}|$\sigma = {:.2f}$'.format(label_base, sigma))
        # plt.plot(radii, accuracies_cert_ours[accuracies_cert_ours[:,0].argmax(), :], color[it], dashes=[2, 2], label='{}|$\sigma = {:.2f}$'.format(label_base, sigma))


    # plt.ylim((0, 1))
    # plt.xlim((0, max_radius))
    # plt.tick_params(labelsize=14)
    # plt.xlabel("$\ell_2$ radius", fontsize=16)
    # plt.ylabel("Certified Accuracy", fontsize=16)
    # plt.gca().xaxis.set_major_locator(plt.MultipleLocator(0.5))
    # plt.legend(loc='upper right', fontsize=16)
    # plt.tight_layout()
    # plt.savefig(outfile + ".pdf")
    # plt.title(title, fontsize=20)
    # plt.tight_layout()
    # plt.savefig(outfile + ".png", dpi=300)
    # plt.close()


    ax.set_ylim(0, 1)
    ax.set_xlim(0, max_radius)
    ax.tick_params(labelsize=14)
    ax.set_xlabel("$\ell_2$ radius", fontsize=16)
    ax.set_ylabel("Certified Accuracy", fontsize=16)
    ax.xaxis.set_major_locator(plt.MultipleLocator(0.5))
    ax.legend(loc='upper right', fontsize=16)
    plt.tight_layout()
    fig.savefig(outfile + ".png", dpi=300)
    plt.show()
    # plt.close()
    print("saved!", outfile)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值