亲宝软件园·资讯

展开

python Matplotlib绘图

獜洛橙 人气:0

绘制直线图,确定x范围和y的范围

代码:

import matplotlib.pyplot as plt
import numpy as np
 
xpoints = np.array([0, 6]) #0-6的范围
ypoints = np.array([0, 100]) #0-100的范围
 
plt.plot(xpoints, ypoints)
plt.show()

绘制两个点的坐标,用实心圆来标记点

代码:

import matplotlib.pyplot as plt
import numpy as np
 
xpoints = np.array([1, 8])
ypoints = np.array([3, 10])
 
plt.plot(xpoints, ypoints, 'o')
plt.show()

绘制折线图,根据对应的x,y坐标

代码:

import matplotlib.pyplot as plt
import numpy as np
 
xpoints = np.array([1, 2, 6, 8])
ypoints = np.array([3, 8, 1, 10])
 
plt.plot(xpoints, ypoints)
plt.show()

注意点,在不指定x坐标的时候,只有y坐标,那么x坐标随y坐标改变而改变,y连续,x的默认范围是【0,1】,y不连续,x的范围是从0开始的每次增加1的整数

y连续:

代码:

import matplotlib.pyplot as plt
import numpy as np
 
ypoints = np.array([3, 10])
 
plt.plot(ypoints)
plt.show()

y不连续 :

代码:

import matplotlib.pyplot as plt
import numpy as np
 
ypoints = np.array([3, 8, 1, 10, 5, 7])
 
plt.plot(ypoints)
plt.show()

绘制正弦函数和余弦函数曲线图

代码:

import matplotlib.pyplot as plt
import numpy as np
 
x = np.arange(0,4*np.pi,0.1)   # 开始,停止,步频
y = np.sin(x)
z = np.cos(x)
plt.plot(x,y,x,z)
plt.show()

加载全部内容

相关教程
猜你喜欢
用户评论