1、基础用法
plt.plot(x, y,color=' ', linewidth=1.0, linestyle=' ')
plt.show()
2、figure
plt.figure(num=2, figuresize = (3,9))
plt.plot(x,y)
#一张图内可绘制多个曲线
plt.show()
3、设置坐标轴
#坐标轴取值范围
plt.xlim()
plt.ylim()
#坐标轴名称
plt.xlabel()
plt.ylabel()
#修改坐标刻度
plt.xticks()
plt.yticks() Eg: plt.yticks([-2,-1.8,-1,1.22,3],
['really bad', 'bad', 'normal', 'good', 'really good']) #修改刻度及名称
//修改字体[r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'] \ 转字符 \\alpha---> α
###gac= 'get current axis' 获取当前坐标轴信息
ax = plt.gca()
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')
ax.spines['bottom'].set_position(('data',0)) #设置边框及位置
ax.spines['left'].set_position(('data',0))
.spines
设置边框
.set_position
设置边框位置
所有位置: top, bottom, both, default, none
位置所有属性:outward
,axes
(百分比),data
(具体数字)
Code:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3,3,50)
y1 = 4*x+1
y2 = x**3
plt.figure()
plt.plot(x,y1)
plt.plot(x,y2, color = 'red', linewidth = 1.0, linestyle = '--')
plt.xlim((-1,2))
plt.ylim((-2,3))
new_ticks = np.linspace(-1,2,5)
plt.xticks(new_ticks)
plt.yticks([-2,-1.8,-1,1.22,3],[r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$',])
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
plt.show()
4、legend 图例
第一种:
plt.plot(x,y1, label = 'linear line')
plt.plot(x,y2,color = 'red', linewidth = 1.0, linestyle = '--',label = 'triple line')
plt.legend()
第二种:
l1, = plt.plot(x,y1, label = 'linear line') #用变量存储,注意以逗号结尾
l2, =plt.plot(x,y2, color = 'red', linewidth = 1.0, linestyle = '--',label = 'triple line')
plt.legend(loc = 'upper right')
或 plt.legend(handles = [l1,l2], labels = ['up', 'down'], loc = 'best')
loc 参数:'best'
, 'upper right'
,'upper left'
, 'lower left'
,'lower right'
, 'right'
,'center left'
, 'center right'
, 'lower center'
, 'upper center'
,'center'
5、Annotation 标注
第一种:.annotate
"""method 1"""
######################################
x0=2
y0=x0**3
plt.scatter(x0, y0, s=50, color = 'b') #设置散点
plt.plot([x0,x0],[0,y0], 'k--', linewidth = 2.5) #绘制虚线
plt.annotate(r'$x^3=%s$'%y0, #标注内容
xy=(x0,y0), #标注点
xycoords = 'data', #基于数据的值来选位置
xytext = (+30,-30),
textcoords = 'offset points', #标注位置
fontsize = 16,
arrowprops = dict(arrowstyle ='->', connectionstyle = "arc3, rad = .2")) #对标注箭头的设置
第二种:.text
"""method 2"""
########################################
plt.text(-3,10, #标注位置
r'$This\ is\ an\ example.$', #标注内容
fontdict = {'size':16, 'color':'g'}) #设置文本
6、tick能见度
for label in ax.get_xticklabels()+ax.get_yticklabels():
label.set_fontsize(12) #重新调节字体大小
label.set_bbox(dict(facecolor = 'white', #调节box前景色
edgecolor = 'None', #设置边框
alpha = 0.7, #alpha设置透明度
zorder = 2)
2018.08.05 整理于莫烦Python教学视频
https://morvanzhou.github.io/tutorials/data-manipulation/plt/2-1-basic-usage/