代码之家  ›  专栏  ›  技术社区  ›  Aaron

matplotlib正在忽略locator\u params nticks命令

  •  2
  • Aaron  · 技术社区  · 8 年前

    ax0 = pyplot.gca()
    cmap = pyplot.cm.get_cmap('RdYlBu', 9)
    vmin=0.75
    vmax=5.25
    sc = pyplot.scatter(X[idx, 0], X[idx, 1], c=colors[idx], vmin=vmin,
                        vmax=vmax, s=30, cmap=cmap)
    
    pyplot.colorbar(sc, ticks=[1.0, 2.0, 3.0, 4.0, 5.0])
    ax0.locator_params(tight=True, nticks=4)
    ax0.set_ylim([-1.0, 1.0])
    ax0.set_xlim([-1.0, 1.0])
    ax0.axis('equal')
    pyplot.show()
    

    这是生成的图像

    scatter plot

    1 回复  |  直到 8 年前
        1
  •  1
  •   ImportanceOfBeingErnest    8 年前

    第一 如 locator_params 文档:

    ax.locator_params(tight=True, nbins=4)

    关键字参数命名为 nbins nticks

    此外,您可能希望将其设置为两个轴:

    ax0.locator_params(which="both", tight=True, nbins=4)
    

    梯度方向数 不设置滴答声的数量,但设置最大箱子数,因此结果可能小于4个箱子。

    ax0.axis('equal') 在设置限制后设置。您可能需要设置方面, ax0.set_aspect('equal') .

    import matplotlib.pyplot as plt
    import numpy as np
    
    X =np.random.randn(30,2)
    colors= np.random.rand(30)*5
    ax0 = plt.gca()
    ax0.set_aspect('equal')
    cmap = plt.cm.get_cmap('RdYlBu', 9)
    
    sc = plt.scatter(X[:, 0], X[:, 1], c=colors, vmin=0.75,
                        vmax=5.25, s=30, cmap=cmap)
    
    plt.colorbar(sc, ticks=[1.0, 2.0, 3.0, 4.0, 5.0])
    ax0.locator_params(which="both", tight=True, nbins=4)
    ax0.set_ylim([-1.0, 1.0])
    ax0.set_xlim([-1.0, 1.0])
    
    plt.show()
    

    enter image description here