Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make the x-axis pass through (0,0) with matplotlib in Python?

How do I make the horizontal axis pass through the origin?

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()
like image 703
Clay Avatar asked May 17 '17 05:05

Clay


People also ask

How do I make X-axis start at 0 in matplotlib?

To show (0,0) on matplotlib graph at the bottom left corner, we can use xlim() and ylim() methods.

How do I spread the X-axis in matplotlib?

To increase the space for X-axis labels in Matplotlib, we can use the spacing variable in subplots_adjust() method's argument.

How do I change the x-axis range in matplotlib?

In matplotlib, to set or get X-axis and Y-axis limits, use xlim() and ylim() methods, accordingly.


1 Answers

Try with:

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()

The output:

enter image description here

like image 198
Tiny.D Avatar answered Nov 15 '22 08:11

Tiny.D