【问题标题】:Prevent axes from cutting off dots in matplotlib scatter plots防止轴切断 matplotlib 散点图中的点
【发布时间】:2022-01-14 22:25:45
【问题描述】:
import matplotlib.pyplot as plt


x = [1, 2, 3, 4]
y = [0, 1, 7, 2]

plt.scatter(x, y, color='red')
plt.title('number of iterations')
plt.xlim([1, 4])
plt.ylim([1, 8])

如果要绘制此数据,则轴上的点会被部分切除。有没有办法防止这种情况(即可以将点绘制在轴的顶部)?

【问题讨论】:

    标签: python matplotlib scatter-plot


    【解决方案1】:

    clip_on 属性设置为False 允许您超越坐标轴,但默认情况下坐标轴将位于顶部。比如脚本

    x = [1, 2, 3, 4]
    y = [0, 1, 7, 2]
    
    plt.scatter(x, y, color="red", clip_on=False)
    plt.title('number of iterations')
    plt.xlim([1, 4])
    plt.ylim([1, 8])
    

    产生以下结果。

    请注意,轴“穿过”点。如果您希望点位于轴/标签之上,则需要更改默认的zorder。比如脚本

    x = [1, 2, 3, 4]
    y = [0, 1, 7, 2]
    
    plt.scatter(x, y, color="red", clip_on=False, zorder = 10)
    plt.title('number of iterations')
    plt.xlim([1, 4])
    plt.ylim([1, 8])
    

    产量

    注意:任何zorder 值 3 或更大的值都可以在这里使用

    【讨论】:

    • @tdy 感谢您的关注
    【解决方案2】:

    clip_on 设置为False

    plt.scatter(x, y, color='red', clip_on=False)
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 2014-09-18
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 2013-12-31
      • 2021-05-08
      • 2020-07-22
      相关资源
      最近更新 更多