1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 # 100 linearly spaced numbers 5 x = np.linspace(-5,5,100) 6 7 # the function, which is y = x^2 here 8 y = x**2 9 10 # setting the axes at the centre 11 fig = plt.figure() 12 ax = fig.add_subplot(1, 1, 1) 13 ax.spines['left'].set_position('center') 14 ax.spines['bottom'].set_position('zero') 15 ax.spines['right'].set_color('none') 16 ax.spines['top'].set_color('none') 17 ax.xaxis.set_ticks_position('bottom') 18 ax.yaxis.set_ticks_position('left') 19 20 # plot the function 21 plt.plot(x,y, 'r') 22 23 # show the plot 24 plt.show()
1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 # 100 linearly spaced numbers 5 x = np.linspace(-5,5,100) 6 7 # the function, which is y = x^3 here 8 y = x**3 9 10 # setting the axes at the centre 11 fig = plt.figure() 12 ax = fig.add_subplot(1, 1, 1) 13 ax.spines['left'].set_position('center') 14 ax.spines['bottom'].set_position('center') 15 ax.spines['right'].set_color('none') 16 ax.spines['top'].set_color('none') 17 ax.xaxis.set_ticks_position('bottom') 18 ax.yaxis.set_ticks_position('left') 19 20 # plot the function 21 plt.plot(x,y, 'g') 22 23 # show the plot 24 plt.show()
1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 # 100 linearly spaced numbers 5 x = np.linspace(-np.pi,np.pi,100) 6 7 # the function, which is y = sin(x) here 8 y = np.sin(x) 9 10 # setting the axes at the centre 11 fig = plt.figure() 12 ax = fig.add_subplot(1, 1, 1) 13 ax.spines['left'].set_position('center') 14 ax.spines['bottom'].set_position('center') 15 ax.spines['right'].set_color('none') 16 ax.spines['top'].set_color('none') 17 ax.xaxis.set_ticks_position('bottom') 18 ax.yaxis.set_ticks_position('left') 19 20 # plot the function 21 plt.plot(x,y, 'b') 22 23 # show the plot 24 plt.show()
1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 # 100 linearly spaced numbers 5 x = np.linspace(-np.pi,np.pi,100) 6 7 # the function, which is y = sin(x) here 8 y = np.sin(x) 9 10 # setting the axes at the centre 11 fig = plt.figure() 12 ax = fig.add_subplot(1, 1, 1) 13 ax.spines['left'].set_position('center') 14 ax.spines['bottom'].set_position('center') 15 ax.spines['right'].set_color('none') 16 ax.spines['top'].set_color('none') 17 ax.xaxis.set_ticks_position('bottom') 18 ax.yaxis.set_ticks_position('left') 19 20 # plot the functions 21 plt.plot(x,y, 'b', label='y=sin(x)') 22 plt.plot(x,2*y, 'c', label='y=2sin(x)') 23 plt.plot(x,3*y, 'r', label='y=3sin(x)') 24 25 plt.legend(loc='upper left') 26 27 # show the plot 28 plt.show()
1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 # 100 linearly spaced numbers 5 x = np.linspace(-np.pi,np.pi,100) 6 7 # the functions, which are y = sin(x) and z = cos(x) here 8 y = np.sin(x) 9 z = np.cos(x) 10 11 # setting the axes at the centre 12 fig = plt.figure() 13 ax = fig.add_subplot(1, 1, 1) 14 ax.spines['left'].set_position('center') 15 ax.spines['bottom'].set_position('center') 16 ax.spines['right'].set_color('none') 17 ax.spines['top'].set_color('none') 18 ax.xaxis.set_ticks_position('bottom') 19 ax.yaxis.set_ticks_position('left') 20 21 # plot the functions 22 plt.plot(x,y, 'c', label='y=sin(x)') 23 plt.plot(x,z, 'm', label='y=cos(x)') 24 25 plt.legend(loc='upper left') 26 27 # show the plot 28 plt.show()
1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 # 100 linearly spaced numbers 5 x = np.linspace(-2,2,100) 6 7 # the function, which is y = e^x here 8 y = np.exp(x) 9 10 # setting the axes at the centre 11 fig = plt.figure() 12 ax = fig.add_subplot(1, 1, 1) 13 ax.spines['left'].set_position('center') 14 ax.spines['bottom'].set_position('zero') 15 ax.spines['right'].set_color('none') 16 ax.spines['top'].set_color('none') 17 ax.xaxis.set_ticks_position('bottom') 18 ax.yaxis.set_ticks_position('left') 19 20 # plot the function 21 plt.plot(x,y, 'y', label='y=e^x') 22 plt.legend(loc='upper left') 23 24 # show the plot 25 plt.show()标签:set,函数,python,spines,plt,position,np,ax,绘制 From: https://www.cnblogs.com/ZY-LunarCrater/p/16611490.html