plt.scatter()函数
先用plt.scatter()绘制一个简单的散点图
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1, 1, 10)
y = 2*x+1
plt.scatter(y, x)
plt.show()
运行结果如图
np.random.normal(loc=,scale=,size=)
功能是从正态分布中抽取随机样本
参数
loc: 正态分布的均值,就是这个正态分布的均值,比如loc=0说明这是一个以Y轴为对称轴的正态分布。
scale: 正态分布的标准差,scale越打正态分布图像越胖矮,反之越高瘦
size: 输出的值赋在shape中,默认为None
plt.scatter(X, Y, s=, c=, alpha=)
X,Y是目标散点的坐标,s是散点的面积,c是散点的颜色,alpha是指散点的透明度
plt.xticks(())
代表把所有的X轴设置为None
import matplotlib.pyplot as plt
import numpy as np
n = 1024
X = np.random.normal(0, 1, n)
Y = np.random.normal(0, 1, n)
T = np.arctan2(Y, X)
plt.scatter(X, Y, s=75, c=T, alpha=0.5)
plt.xlim((-1.5, 1.5))
plt.ylim((-1.5, 1.5))
plt.xticks(())
plt.yticks(())
plt.show()
运行结果
plt.annotate(r'$2x+1=%s$' % y0, xy=(x0, y0), xycoords=, xytext=(+30, -30),textcoords=, fontsize=, arrowprops=dict(arrowstyle='',connectionstyle=','))
'$2x+1=%s$'是要打印的字符串,
xy(x0,y0)是指向点的坐标,
xyxcoords参数有figure points;figure pixels;figure fraction;axes pixels;data等等分别代表剪头的位置。
xytext()是打印文字的位置+30,-30的意思是相对于指向点的坐标的位置X轴加30的像素距离
Y轴减30的像素距离的位置。
textcoords 用来设置字体的颜色
arrowprops 箭头参数,参数的类型为字典dict。
plt.text(x, y, r'$This\ is\ the\ some\ text.\ \mu\ \sigma_i\ \alpha_t$', fontdict={'size': , 'color': ''})
用来打印字符串,
x,y是指定打印的起始位置
\空格用来转义打印空格,\sigma_i 打印下标为i的希腊字母
fontdict分别设置字体的粗细和颜色,变量类型为字典
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1, 1, 10)
y1 = 2*x + 1
plt.figure(num=3, figsize=(8, 5)) # num = 3 表示设置当前的名字,figsize = (8, 5)代表长和宽
plt.plot(x, y1)
ax = plt.gca() # 获得当前坐标轴,存储到ax中
ax.spines['right'].set_color('none') # 将右边的轴设置为空
ax.spines['top'].set_color('none') # 将顶部的轴设置为空
ax.xaxis.set_ticks_position('bottom') # 用来把X的刻度放在下方的轴
ax.yaxis.set_ticks_position('left') # 用来把Y的刻度放在左边的轴
ax.spines['bottom'].set_position(('data', 0)) # 将X轴移动到Y轴0刻度的位置
ax.spines['left'].set_position(('data', 0)) # 将Y轴移动到X轴0刻度的位置
x0 = 0.5
y0 = 2*x0+1
plt.scatter(x0, y0, s=50, color='b')
plt.plot([x0, x0], [y0, 0], 'k--', lw=2.5)
plt.annotate(r'$2x+1=%s$' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=.2'))
plt.text(-1.3, 1, r'$This\ is\ the\ some\ text.\ \mu\ \sigma_i\ \alpha_t$', fontdict={'size': 16, 'color': 'r'})
plt.show()
运行结果为