【问题标题】:How do you make the x-axis pass through (0,0) with matplotlib in Python?如何在 Python 中使用 matplotlib 使 x 轴通过 (0,0)?
【发布时间】:2017-05-17 05:30:28
【问题描述】:

如何让横轴通过原点?

import numpy as np
import matplotlib.pyplot as plt

rateList=[0,0.08,.1,.12,.15,.175,.225,.25,.275,.3,.325,.35]

list1=[-316.8,-424,-2.8,622,658,400,83,16.8,0]
NPV_Profile1=[np.npv(x,list1) for x in rateList]

list2=[-496,-760,84,1050.4,658,400,83,16.8,0]
NPV_Profile2=[np.npv(x,list2) for x in rateList]

plt.plot(rateList,NPV_Profile1,rateList,NPV_Profile2)

plt.show()

【问题讨论】:

    标签: python-3.x matplotlib


    【解决方案1】:

    尝试:

    import numpy as np
    import matplotlib.pyplot as plt
    
    rateList=[0,0.08,.1,.12,.15,.175,.225,.25,.275,.3,.325,.35]
    
    list1=[-316.8,-424,-2.8,622,658,400,83,16.8,0]
    
    NPV_Profile1=[np.npv(x,list1) for x in rateList]
    
    list2=[-496,-760,84,1050.4,658,400,83,16.8,0]
    
    NPV_Profile2=[np.npv(x,list2) for x in rateList]
    
    fig, ax = plt.subplots()
    ax.plot(rateList,NPV_Profile1,rateList,NPV_Profile2)
    
    
    # set the x-spine
    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()
    
    plt.show()
    

    输出:

    【讨论】:

      猜你喜欢
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      相关资源
      最近更新 更多