【问题标题】:change matplotlib axis settings更改 matplotlib 轴设置
【发布时间】:2010-11-27 02:55:55
【问题描述】:

如何控制 pyplot 绘图的轴设置。我已经完成了

    pylab.plot(*self.plot_generator(low, high))

    pylab.show()

我得到了这就是我想要的

但我希望 x 轴位于 0 而不是底部。我该怎么做?

【问题讨论】:

    标签: python matplotlib scipy


    【解决方案1】:
    # create some data
    x = np.linspace(-np.pi,np.pi,100)
    y = np.cos(2.5*x)
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(x,y, mfc='orange', mec='orange', marker='.')
    
    # using 'spines', new in Matplotlib 1.0
    ax.spines['left'].set_position('zero')
    ax.spines['right'].set_color('none')
    ax.spines['bottom'].set_position('zero')
    ax.spines['top'].set_color('none')
    ax.spines['left'].set_smart_bounds(True)
    ax.spines['bottom'].set_smart_bounds(True)
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
    
    ax.axhline(linewidth=2, color='blue')
    ax.axvline(linewidth=2, color='blue')
    show()
    

    【讨论】:

    • 这看起来像我需要的,还有@Joe Kington 的评论
    • 您可以在此处找到有关如何在这方面使用脊椎的更一般说明:matplotlib.org/examples/pylab_examples/…
    • 您能解释一下为什么这里使用ax.spines。 AFAIK,所有方法set_positionset_color 都是matplotlib.spines.Spine 类对象的成员。这令人困惑。
    • 没错。 ax.spines 是一个 OrderedDict,它包含四个 Spine 对象:'left''right''top''bottom'
    • 这里不需要axhlineaxvline 命令。如果想更改坐标轴的线宽或颜色,最好更改坐标轴属性。
    【解决方案2】:

    将 x 轴的起点设置为 0:

    pylab.xlim(xmin=0)
    

    将 y 轴的起点设置为 0:

    pylab.ylim(ymin=0)
    

    pylab.plot 调用之后添加这些行之一(或者如果您愿意,可以同时添加)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多