python: 绘制数学函数
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()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现