【问题标题】:show origin axis (x,y) in matplotlib plot在 matplotlib 图中显示原点轴 (x,y)
【发布时间】:2014-10-30 14:32:02
【问题描述】:

我有以下简单的情节,我想显示原点轴 (x, y)。我已经有了网格,但我需要强调 x、y 轴。

这是我的代码:

x = linspace(0.2,10,100)
plot(x, 1/x)
plot(x, log(x))
axis('equal')
grid()

我看到了this 的问题。接受的答案建议使用“轴脊”,并仅链接到一些示例。然而,这个例子太复杂了,使用了子图。在我的简单示例中,我无法弄清楚如何使用“轴脊”。

【问题讨论】:

    标签: matplotlib ipython ipython-notebook


    【解决方案1】:

    使用subplots 不是太复杂,可能是刺。

    愚蠢,简单的方法:

    %matplotlib inline
    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.linspace(0.2,10,100)
    fig, ax = plt.subplots()
    ax.plot(x, 1/x)
    ax.plot(x, np.log(x))
    ax.set_aspect('equal')
    ax.grid(True, which='both')
    
    ax.axhline(y=0, color='k')
    ax.axvline(x=0, color='k')
    

    我得到:

    (您看不到垂直轴,因为 x 下限为零。)

    使用简单刺的替代方法

    %matplotlib inline
    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.linspace(0.2,10,100)
    fig, ax = plt.subplots()
    ax.plot(x, 1/x)
    ax.plot(x, np.log(x))
    ax.set_aspect('equal')
    ax.grid(True, which='both')
    
    # set the x-spine (see below for more info on `set_position`)
    ax.spines['left'].set_position('zero')
    
    # turn off the right spine/ticks
    ax.spines['right'].set_color('none')
    ax.yaxis.tick_left()
    
    # set the y-spine
    ax.spines['bottom'].set_position('zero')
    
    # turn off the top spine/ticks
    ax.spines['top'].set_color('none')
    ax.xaxis.tick_bottom()
    

    替代使用seaborn(我最喜欢的)

    import numpy as np
    import matplotlib.pyplot as plt
    import seaborn
    seaborn.set(style='ticks')
    
    x = np.linspace(0.2,10,100)
    fig, ax = plt.subplots()
    ax.plot(x, 1/x)
    ax.plot(x, np.log(x))
    ax.set_aspect('equal')
    ax.grid(True, which='both')
    seaborn.despine(ax=ax, offset=0) # the important part here
    

    使用spine的set_position方法

    这是set_position method of spines 的文档:

    脊椎位置由(位置类型,数量)的 2 个元组指定。 职位类型有:

    • 'outward' :将脊椎从数据区域向外放置指定的点数。 (负值指定放置
      脊柱向内。)

    • 'axes' :将脊椎放置在指定的 Axes 坐标处(从 0.0-1.0)。

    • 'data' : 将书脊放置在指定的数据坐标处。

    此外,速记符号定义了一个特殊的位置:

    • 'center' -> ('axes',0.5)
    • “零”->(“数据”,0.0)

    所以你可以在任何地方放置,比如左脊椎:

    ax.spines['left'].set_position((system, poisition))

    其中system 是“向外”、“轴”或“数据”,position 在该坐标系中的位置。

    【讨论】:

      【解决方案2】:

      让我为那些将像我刚才一样搜索它的人回答这个(相当老的)问题。尽管它提出了可行的解决方案,但我认为(仅)提供的答案过于复杂,当涉及到问题中描述的这种简单情况时(注意:此方法要求您指定所有轴端点)。

      我在matplotlib's pyplot 上的one of the first tutorials 中找到了一个简单的工作解决方案。 在创建绘图后添加以下行

      就足够了

      plt.axis([xmin, xmax, ymin, ymax])

      如下例:

      from matplotlib import pyplot as plt
      
      xs = [1,2,3,4,5]
      ys = [3,5,1,2,4]
      
      plt.scatter(xs, ys)
      plt.axis([0,6,0,6])  #this line does the job
      plt.show()
      

      产生以下结果:

      matplotlib plot with axes endpoints specified

      【讨论】:

      • 用户需要强调 x、y 轴,但您的解决方案似乎只裁剪轴。这并没有提供问题的解决方案
      • 这个解决方案帮助了我。授予它剪辑网格,这对于这个特定问题并不理想(我建议出版商编辑它),但它确实演示了如何强制轴从特定数字开始,这正是我正在寻找的。
      猜你喜欢
      • 2022-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 2019-07-31
      • 2021-03-22
      • 2010-11-19
      • 1970-01-01
      相关资源
      最近更新 更多