首页 > 编程语言 >python: 绘制数学函数

python: 绘制数学函数

时间:2022-08-22 00:12:42浏览次数:72  
标签:set 函数 python spines plt position np ax 绘制

 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

相关文章

  • mysql6/视图/触发器/事务/四种隔离级别/事务日志/mvcc/内置函数/存储过程/索引/索引的
    视图触发器事务事务处理四种隔离级别事务日志MVCC内置函数存储过程索引索引的意义慢查询优化查询索引模拟视图1.什么是视图?视图是类似于临时表,由sql......
  • C++中函数指针使用
    类成员函数指针(memberfunctionpointer),是C++语言的一类指针数据类型,用于存储一个指定类具有给定的形参列表与返回值类型的成员函数的访问信息。一般我们是不会使用的,都......
  • 常用函数
    MySQL函数官网:https://dev.mysql.com/doc/refman/5.7/en/常用函数--==============常用函数================--数学运算SELECTABS(-8)SELECTCEILING(9.4)......
  • 系统学Python(四)字符串
    今天我们来学习字符串。python中的字符串字面量由单引号或双引号括起。str1='hello'str2="hello"#两种写法效果一样print(str1)print(str2)print(type(str1),t......
  • 2022-08-18 MySQL常用函数
    MySQL常用函数聚合函数count:计数。count(*)≈count(1)>count(主键)count(*):MySQL对count(*)底层优化,count(0)。count(1)count(主键)count(字段)min:最小值max:最......
  • 2022.8.21 四大函数式接口与Stream流式计算
    12、四大函数式接口(重点)   函数接口:只有一个方法的接口    @FunctionalInterface publicinterfaceRunnable{     publicabstractvoidrun(......
  • Python_08While循环
    while循环Python提供了While和for循环,(在Python中没有do..while循环)如果使用 while 循环,给定的判断条件为true时执循环体,否则退出循环体。1#在Python中没有do...whi......
  • Python-09函数基础、形参、实参
    Python3函数函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如print......
  • python Day 01
    主要内容:1.有关变量的基本定义及组成2.语法:print与id3.变量的创建规则与规范4.数据类型:numbers(数字型)与字符串str*自我介绍的代码运行 图片展示:     ......
  • Python入门系列(六)一篇学会python函数
    函数函数是只在调用时运行的代码块。defmy_function():print("Hellofromafunction")my_function()信息可以作为参数传递到函数中。defmy_function(fname):......