【问题标题】:how to properly set the axis in matlplotlib如何在 matplotlib 中正确设置轴
【发布时间】:2020-07-27 21:19:08
【问题描述】:

我想绘制一个散点图以使用 matplotlib 可视化我的结果:

    plt.subplot(1, 2, 1)
    plt.scatter(source_weights, target_weights)
    plt.xlabel('Source Weights', fontsize=13, fontweight='bold')
    plt.ylabel('Target Weights', fontsize=13, fontweight='bold')
    plt.xticks(fontsize=12, fontweight='bold')
    plt.yticks(fontsize=12, fontweight='bold')
    plt.axis('equal')
    plt.axis('square')
    y_lim = np.max(np.abs(target_weights))
    x_lim = np.max(np.abs(source_weights))
    lim = max(x_lim, y_lim)
    _ = plt.plot([-1.1 * lim, 1.1 * lim], [-1.1 * lim, 1.1 * lim])

    # plot bias difference
    plt.subplot(1, 2, 2)
    plt.scatter(source_bias, target_bias)
    plt.xlabel('Source Bias', fontsize=13, fontweight='bold')
    plt.ylabel('Target Bias', fontsize=13, fontweight='bold')
    plt.xticks(fontsize=12, fontweight='bold')
    plt.yticks(fontsize=12, fontweight='bold')
    plt.axis('equal')
    plt.axis('square')
    y_lim = np.max(np.abs(target_bias))
    x_lim = np.max(np.abs(source_bias))
    lim = max(x_lim, y_lim)
    _ = plt.plot([-1.1 * lim, 1.1 * lim], [-1.1 * lim, 1.1 * lim])

但我发现有时轴的标量堆叠在一起,就像在源偏差中这样:

有没有什么方法可以在不改变字体大小的情况下解决这个问题,比如在轴上设置更少的刻度。顺便说一下,我仍然想要 x 轴和 y 轴(1:1 正方形)的相同缩放比例。

【问题讨论】:

    标签: python matplotlib data-visualization


    【解决方案1】:

    您可以旋转xticks

    plt.xticks(rotation=45, horizontalalignment='right')
    

    【讨论】:

    • 谢谢!这有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    相关资源
    最近更新 更多