python函数画图 python如何画函数图像( 五 )


plt.plot(y,xx)
4、plt.xlim设置x轴或者y轴刻度范围

plt.xlim(0,1000)#设置x轴刻度范围,从0~1000#lim为极限,范围
plt.ylim(0,20)# 设置y轴刻度的范围 , 从0~20
5、plt.xticks():设置x轴刻度的表现方式
fig2 = plt.figure(num='fig222222', figsize=(6, 3), dpi=75, facecolor='#FFFFFF', edgecolor='#FF0000')
plt.plot(x,y2)
plt.xticks(np.linspace(0,1000,15,endpoint=True))# 设置x轴刻度
plt.yticks(np.linspace(0,20,10,endpoint=True))
结果
6、ax2.set_title('xxx')设置标题,画图
#产生[1,2,3,...,9]的序列
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(221)
#设置标题
ax1.set_title('Scatter Plot1')
plt.xlabel('M')
plt.ylabel('N')
ax2 = fig.add_subplot(222)
ax2.set_title('Scatter Plot2clf')
#设置X轴标签
plt.xlabel('X')#设置X/Y轴标签是在对应的figure后进行操作才对应到该figure
#设置Y轴标签
plt.ylabel('Y')
#画散点图
ax1.scatter(x,y,c = 'r',marker = 'o')#可以看出画散点图是在对figure进行操作
ax2.scatter(x,y,c = 'b',marker = 'x')
#设置图标
plt.legend('show picture x1 ')
#显示所画的图
plt.show()
结果
7、plt.hist()绘制直方图(可以将高斯函数这些画出来)
绘图都可以调用matplotlib.pyplot库来进行,其中的hist函数可以直接绘制直方图
调用方式:
n, bins, patches = plt.hist(arr, bins=10, normed=0, facecolor='black', edgecolor='black',alpha=1,histtype='bar')
hist的参数非常多,但常用的就这六个,只有第一个是必须的,后面四个可选
arr: 需要计算直方图的一维数组
bins: 直方图的柱数,可选项,默认为10
normed: 是否将得到的直方图向量归一化 。默认为0
facecolor: 直方图颜色
edgecolor: 直方图边框颜色
alpha: 透明度
histtype: 直方图类型,‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’
返回值 :
n: 直方图向量,是否归一化由参数normed设定
bins: 返回各个bin的区间范围
patches: 返回每个bin里面包含的数据 , 是一个list
from skimage import data
import matplotlib.pyplot as plt
img=data.camera()
plt.figure("hist")
arr=img.flatten()
n, bins, patches = plt.hist(arr, bins=256, normed=1,edgecolor='None',facecolor='red')
plt.show()
例:
mu, sigma = 0, .1
s = np.random.normal(loc=mu, scale=sigma, size=1000)
a,b,c = plt.hist(s, bins=3)
print("a: ",a)
print("b: ",b)
print("c: ",c)
plt.show()
结果:
a:[ 85. 720. 195.]#每个柱子的值
b:[-0.36109509 -0.13573180.089631490.31499478]#每个柱的区间范围
c:a list of 3 Patch objects#总共多少柱子
8、ax1.scatter(x,y,c = 'r',marker = 'o')
使用注意:确定了figure就一定要确定象限,然后用scatter,或者不确定象限,直接使用plt.scatter
x = np.arange(1,10)
y = x
fig = plt.figure()
a=plt.subplot()#默认为一个象限
# a=fig.add_subplot(222)
a.scatter(x,y,c='r',marker='o')
plt.show()
结果
x = np.arange(1,10)
y = x
plt.scatter(x,y,c='r',marker='o')
plt.show()
结果
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(1,10)
y = x
plt.figure()
plt.scatter(x,y,c='r',marker='o')
plt.show()
结果
文章知识点与官方知识档案匹配
Python入门技能树基础语法函数
211242 人正在系统学习中
打开CSDN APP,看更多技术内容
plt的一些函数的使用_班花i的博客_plt函数
plt.函数 Fwuyi的博客 6513 1plt.figure( )函数:创建画布 2plt.plot(x, y, format_string, label="图例名"):绘制点和线, 并控制样式 。其中x是x轴数据,y是y轴数据,xy一般是列表和数组 。format_string 是字符串的格式包括线...

推荐阅读