画图常用命令

画图常用命令

绘制多图/调整图幅大小

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-1,1,50)
y1 = x ** 2 
y2 = x * 2
#这个是第一个figure对象,下面的内容都会在第一个figure中显示
plt.figure()
plt.plot(x,y1)
#这里第二个figure对象
plt.figure(num = 3,figsize = (10,5))
plt.plot(x,y2)
plt.show()

更改坐标范围

#在plt.show()之前添加
plt.xlim((0,2))
plt.ylim((-2,2))

plot的可选参数

颜色(color),点型(marker),线型(linestyle)

具体形式 fmt = '[color][marker][line]'

例如:

plot(x, y, 'bo-')  # 蓝色圆点实线
plot(x,y,color='green', marker='o', linestyle='dashed', linewidth=1, markersize=6)

也可以对关键字参数color赋十六进制的RGB字符串如 color=’#900302’

常见颜色参数
character color
b
g 绿
r
c 蓝绿(cyan)
m 洋红(magenta)
y
k
w

颜色代码查询

常见线型(line)/点型(marker)参数
字符 描述
‘-’ 实线
‘–’ 虚线
‘-.’ 点线
‘:’ 点虚线
‘.’
‘,’ 像素
‘o’ 圆形
‘v’ 朝下的三角形
‘^’ 朝上的三角形
‘<’ 朝左的三角形
‘>’ 朝右的三角形
‘1’ tri_down marker
‘2’ tri_up marker
‘3’ tri_left marker
‘4’ tri_right marker
‘s’ 正方形
‘p’ 五角形
‘*’ 星型
‘h’ 1号六角形
‘H’ 2号六角形
‘+’ +号标记
‘x’ x号标记
‘D’ 钻石形
‘d’ 小版钻石形
‘|’ 垂直线型
‘_’ 水平线型

分别设置多条线的线型&点型(如果你调参要求多的话。。要是只改一般参数,在每组后边用‘bo-’这种缩写格式就好了):

lines = plt.plot(x, y, x, ym1, x, ym2, 'o')  
#设置线的属性
plt.setp(lines[0], linewidth=1)  
plt.setp(lines[1], linewidth=2)  
plt.setp(lines[2], linestyle='-',marker='^',markersize=4)  

设置标签/坐标轴名称/图的标题

plt.legend(['1', '2','3'],loc='upper right',fontsize=40)
plt.xlabel("X轴")
plt.ylabel("Y轴") 
plt.title('标题') 

标签有几条线就写几个,[]可以换成()

位置:一般情况下,loc属性设置为’best’就足够应付了(直接写0也可以,loc=0)。

legend( handles=(line1, line2, line3),
       labels=('label1', 'label2', 'label3'),
       'upper right')
shadow = True 设置图例是否有阴影
The *loc* location codes are:
          '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

猜你喜欢

转载自blog.csdn.net/qq_45268474/article/details/107965070