基础介绍
plot:
:https://blog.csdn.net/xiaotao_1/article/details/79100163
补充:
点型参数marker,设置点的形状 ,markersize设置大小
xticks用法:
xticks()返回了两个对象,一个是刻标(locs),另一个是刻度标签
locs, labels = xticks()
#显示x轴的刻标
xticks( arange(6) )
#显示x轴的刻标以及对应的标签
xticks( arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue') )
rotation代表lable显示的旋转角度。
#设置字体为SimHei显示中文
plt.rcParams['font.sans-serif'] = 'SimHei'
11 #设置正常显示字符
12 plt.rcParams['axes.unicode_minus'] = False
14 #设置线条样式
15 plt.rcParams['lines.linestyle'] = '-.'
16 #设置线条宽度
17 plt.rcParams['lines.linewidth'] = 3
18 #绘制sin曲线
19 plt.plot(x, y, label='$sin(x)$')
plt.grid(True) # 显示背景的网格线
plt.grid(False) # 关闭背景的网格线
ax.grid(color=’r’, linestyle=’-‘, linewidth=2) # 设置背景网格线的样式
legend 显示图例
参数 loc 显示图例位置
‘best’ : 0,
‘upper right’ : 1,
‘upper left’ : 2,
‘lower left’ : 3,
‘lower right’ : 4,
‘right’ : 5,
‘center left’ : 6,
‘center right’ : 7,
‘lower center’ : 8,
‘upper center’ : 9,
‘center’ : 10,
保存照片,edgecolor边框颜色,facecolor背景颜色
plt.savefig(’./t.jpg’,dpi = 100,facecolor = ‘red’,edgecolor = ‘blue’)
设置整个图形的大小
figure生成图例对象 参数对对象初始化
plt.figure(figsize=(12,9))
add_subplot添加子视图 这个在图上图
sub = fig.add_subplot(1,2,1,facecolor = ‘purple’)
subplot(121)数字代表子图位置坐标,表示是一维两列 图在第二个位置
条形图
plt.bar(np.arange(6),height=np.random.randint(3,15,size = 6),width = 0.3)
#正太分布的数据
x = np.random.randn(1000000)
#直方图和条形图,类似的,都是柱状图
_ = plt.hist(x,bins = 500)
x : (n,) array or sequence of (n,) arrays
这个参数是指定每个bin(箱子)分布的数据,对应x轴
bins : integer or array_like, optional
这个参数指定bin(箱子)的个数,也就是总共有几条条状图
rwidth:竖格粗细
##饼图
labels:外围标签,
autopct占百分之几 保留两位小数
explode 爆炸 把选中的那个突出来
shadow:显示阴影
x = [0.2,0.1,0.3,0.4]
labels = ['vivo','oppo','xiaomi','huawei']
_ = plt.pie(x,labels = labels,autopct='%0.2f%%',explode=[0,0.2,0,0],shadow=True)
histogram(a,bins=10,range=None,weights=None,density=False);
a是待统计数据的数组;
bins指定统计的区间个数;
range是一个长度为2的元组,表示统计范围的最小值和最大值,默认值None,表示范围由数据的范围决定
weights为数组的每个元素指定了权值,histogram()会对区间中数组所对应的权值进行求和
density为True时,返回每个区间的概率密度;为False,返回每个区间中元素的个数
a = np.random.rand(100)
np.histogram(a,bins=5,range=(0,1))#在[0,0.2)有28个数,在[0.2,0.4)有18个数,以此类推
(array([28, 18, 17, 19, 18], dtype=int64),
array([ 0. , 0.2, 0.4, 0.6, 0.8, 1. ]))
散点图
plt.scatter(df['Python'],df['En'],s=100,marker='*',c='r')
前两个传去坐标数据,
s是点大小
marker:点的形状
c:点的颜色
##文字的注释箭头
x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x))
# 绝对坐标
# plt.text(0.2,0,s = 'sin(0)=0',fontdict={'fontsize':30})
# 坐标,相对坐标
plt.figtext(0.2,0.5,'sin(0)=0')
# fontproperties = 'KaiTi'
#设置标题
plt.title('正弦波',fontsize=20,color = 'red' )
#显示x轴的标签
plt.xlabel('X')
#y的标签
plt.ylabel('target')
plt.axis([0,10,9,30]#修改横坐标和纵坐标的范围
#对注释进行操作
xy是标注点的坐标
xytext是注释的坐标
arrowprops是注释图形的设置
plt.annotate(s = 'max value',xy = (5,25),xytext = (7,27),
fontsize = 15,color = 'g',arrowprops ={'width':10,'headwidth':20,'headlength':20,'shrink':0.3} )
绘制3维图形
from mpl_toolkits.mplot3d.axes3d import Axes3D
#线形图
fig = plt.figure(figsize = (9,6))
a3 = Axes3D(fig)
x = np.linspace(0,60,1000)
y = np.sin(x)
z = np.cos(x)
a3.plot(x,y,z,linestyle = '-.',marker = '*',markersize = 5)
a3.set_xlabel('X',color = 'red',fontsize = 20)
a3.set_ylabel('Y',color = 'g',fontsize = 20)
a3.set_zlabel('Z',color = 'b',fontsize = 20)
# a3.set_axis_off()#设置之后不显示轴
# 三维图形设置,方法都是类似的
a3.set_zticks([-1,0,1])#设置z轴坐标
# 3d图形查看视图的方向
# a3.view_init(0,0)