• matplotlib设置y轴刻度范围【已解决】


    用matplotlib绘制个一个图,但是y轴刻度过大,因为AUC本身最大值是1,所以现在需要修改y轴刻度

    上图的代码如下
     

    1. import matplotlib.pyplot as plt
    2. import numpy as np
    3. # 假设你的数据范围是0.51
    4. y_ticks_range = np.arange(0.5, 1.1, 0.1)
    5. # 示例数据
    6. communication_rounds = list(range(0, 21)[::2])
    7. auc_values = np.random.uniform(0.5, 1, len(communication_rounds))
    8. # 绘图
    9. plt.plot(communication_rounds, auc_values)
    10. plt.xticks(communication_rounds, fontsize=15)
    11. plt.ylabel("AUC", fontsize=15)
    12. plt.yticks(y_ticks_range, fontsize=15)
    13. plt.xlabel("Communication Rounds", fontsize=15)
    14. plt.title('(a)Breast Cancer', weight='bold', y=-0.5, fontsize=15)
    15. plt.show()

    只要限制一下范围就可以了,加入以下代码
     

    plt.ylim(0.5, 1)  # 设置 y 轴范围

    大功告成,y轴最大刻度已经变成了1

    整体代码如下所示:
     

    1. import matplotlib.pyplot as plt
    2. import numpy as np
    3. # 假设你的数据范围是0.51
    4. y_ticks_range = np.arange(0.5, 1.1, 0.1)
    5. # 示例数据
    6. communication_rounds = list(range(0, 21)[::2])
    7. auc_values = np.random.uniform(0.5, 1, len(communication_rounds))
    8. # 绘图
    9. plt.plot(communication_rounds, auc_values)
    10. plt.xticks(communication_rounds, fontsize=15)
    11. plt.ylabel("AUC", fontsize=15)
    12. plt.yticks(y_ticks_range, fontsize=15)
    13. plt.xlabel("Communication Rounds", fontsize=15)
    14. plt.ylim(0.5, 1) # 设置 y 轴范围
    15. plt.title('(a)Breast Cancer', weight='bold', y=-0.5, fontsize=15)
    16. plt.show()

  • 相关阅读:
    antd vue 自定义侧边栏svg图标、并改变图标颜色
    代码随想录二刷 | 数组 | 总结篇
    day77:注解、自定义注解、元注解
    电脑系统重装后音频驱动程序怎么修复
    Linux之部署前后端分离项目
    字符串表达式计算(a+b/(a-b))的思路与实践
    03 LVS+Keepalived群集
    深度学习调参指南
    弱鸡记录一道模拟题
    WSL上部署ELK日志系统​
  • 原文地址:https://blog.csdn.net/qq_43604183/article/details/134550208