程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

matplotlib绘图结构整理

发布于2020-02-26 10:55     阅读(675)     评论(0)     点赞(17)     收藏(1)


matplotlib绘图进阶

1.matplotlib结构

1:Figure画布

2: Asex

3:图形结构

  • title
  • xLabel
  • yLabel
  • tick
  • legend
  • grid
  • Text
  • Annotation
  • mark
  • line
  • 。。。。

2.画图的流程

  1. 创建Figure画图对象
  2. 创建子图fig1, Asex对象
  3. 调用相应的函数绘制图形
  4. 设置标题title
  5. 设置图例legend
  6. 设置x、y坐标轴
  7. 设置网格grid
  8. 文本Text、注释annotation、刻度tick
  9. plt.show()显示图形,绘制结束。

画图设置及详细实现

一、创建图形及创建子图

1、图形的创建

f = plt.figure(num=2, figsize=(10,10), dpi=80, facecolor='pink', edgecolor='green', frameon=True)

# print(plt.step(f)) 可以查看到底有多少参数可以设置
  • 1
  • 2
  • 3

这里返回的f指的是总的图像,也就是Figure对象

  • num:图像编号或名称,数字为编号 ,字符串为名称
  • figsize:指定figure的宽和高,单位为英寸;
  • dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80      1英寸等于2.5cm,A4纸是 21*30cm的纸张
  • facecolor:背景颜色
  • edgecolor:边框颜色
  • frameon:是否显示边框

====================================

2、子图的创建

(1)借助plt创建

fig1 = plt.subplot(1,2,1)
  • 1

(2)借助总图所返回的对象创建

fig2 = f.add_subplot(1,2,2)
  • 1

(3)借助gridspec()

G = matplotlib.gridspec.GridSpec(3, 3)

fig1 = f.add_subplot(G[0,:], facecolor='red')
fig2=f.add_subplot(G[1,0:2],facecolor='pink')
fig3=f.add_subplot(G[1,2],facecolor='blue')
fig4=f.add_subplot(G[2,0],facecolor='grey')
fig5=f.add_subplot(G[2,1:3],facecolor='green')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

(4)利用Axes对象创建子图,并实现子图随意排列

fig3 = f.add_axes([0.1, 0.1, 0.5, 0.5], facecolor='grey')

fig4 = plt.axes([0.2, 0.2, 0.4, 0.4], facecolor='green')

# 四个参数分别对应[x, y, width, height], 前面两个是以左下角作为基准的,后面两个数字指的是相对于整个figure对象的宽度和高度。
  • 1
  • 2
  • 3
  • 4
  • 5

==================================

二、图例、标题、坐标标题、网格、图像的确定——五大核心模块

1、与坐标轴相关的设置

(1)坐标轴显示范围:

fig1.axis([xmin, xmax, ymin, ymax])

plt.axis([xmin, xmax, ymin, ymax])
  • 1
  • 2
  • 3

(2)分别设置x、y轴的显示范围

fig1.set_xlim(2, 4)
fig1.set_ylim(2, 4)

plt.xlim(2, 4)
plt.ylim(2, 4)
  • 1
  • 2
  • 3
  • 4
  • 5

(3)设置刻度

fig1.set)yticks([-1, -1/2, 0, 1/2, 1])
fig1.xaxis.set_ticks([1,2,3,4,5,6,7,8])  # 先获取x轴,再对其进行调整

# 上面2种方法只能设置数字刻度

plt.xticks([0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi], ['a', 'b', 'c', 'd'], fontsize='xx-large')
plt.yticks(...)
#上面这种方法可以将对应的数字用字符串替代显示
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

如果需要显示π、α等一些特殊的数学符号, 则可以通过下面的方式来完成

plt.xticks([0, np.pi/2, np.pi], ['$0$', '&\pi/2$', '$\pi$])
# 该方法称之为LaTeX语法
  • 1
  • 2

(4)坐标轴的移动和显示

坐标轴其实总共有上下左右4条。

(a)设置坐标轴不显示

z = plt.gca()  # 获得坐标轴句柄

z.spines['top'].set_color('none') # 设置为none则不显示

z1 = fig1.axes  # 获得坐标轴对象
z1.spines['right'].set_color('none')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

(b)设置刻度显示在那一条坐标轴旁边

z.xaxis.set_ticks_position('top')

z1.yaxis.set_ticks_position('right')
  • 1
  • 2
  • 3

(5)设置主、次坐标轴

f = plt.figure()

fig2 = f.add_subplot(1,2,2)

line2, = plt.plot([1,2,3,4,5],[2,4,6,8,10], label='第一条直线)  # 画图

plt.title('两条直线综合')

fig2.legend(loc=2)

fig2.set_xlabel('横坐标')

fig2.set_ylabel('纵坐标1')

# 上面的是主坐标, 下面绘制次坐标

fig3 = fig2.twinx()  # fig3就是fig2次坐标轴

line3=plt.plot([6,7,8,9,10],label='第二条直线')

fig3.legend(loc=1)

fig3.set_ylabel('纵坐标二')

fig3.grid()

plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

2、与图像线条相关的设置

(1)关闭抗锯齿

line1.set_antialiased(False)
  • 1

(2)线条样式的设置----4种方法

方法一:直接在画线的时候通过“字符串参数”加以指定

line1, = fig1.plot(x, y, 'r-')
  • 1

方法二:直接在划线的时候通过参数指定,如下:

line1, = fig1.plot(x, y, color='red', linewidth=3, linestyle='--')

# linewidth,linestyle可以简写为lw,ls
  • 1
  • 2
  • 3

方法三:通过line.set_xxxx()的方式加以设置,如下:

line1, = fig1.plot(x, y1)

line1.set_color('black')

line1.set_linestyle('-')

line1.set_alpha(0.3)

line1.set_linewidth(8)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

方法四:通过plt.setp(line,xxxx)方法去设置

plt.setp(line1, color='black;, linewidth=8, linestyle='-', alpha=0.3)
  • 1

(3)常见的属性设值

常见的颜色:——除了使用字母,还可以使用十六进制的字符串表示,比如“#008000”,这样任意一种颜色均可以使用了。

'b'          blue

'g'          green

'r'          red

'c'          cyan(青色)

'm'          magenta(品红)

'y'          yellow

'k'          black

'w'          white

常见的线型:

'-'          solid line style

'--'         dashed line style

'-.'         dash-dot line style

':'          dotted line style

常见的点标记marker:

'.'       point marker

','       pixel marker

'o'       circle marker

'v'       triangle_down marker

'^'       triangle_up marker

'<'       triangle_leftmarker

'>'       triangle_rightmarker

'1'       tri_down marker

'2'       tri_up marker

'3'       tri_left marker

'4'       tri_right marker

's'       square marker

'p'       pentagon marker

'*'       star marker

'h'       hexagon1 marker

'H'       hexagon2 marker

'+'       plus marker

'x'       x marker

'D'       diamond marker

'd'       thin_diamond marker

'|'       vline marker

'_'      hline

3、与图例相关的设置

(1)设置图例的几种方法

1、在绘图的时候设置label,调用legend函数就能自动显示图例

2、直接在legend函数上设置(这种方法比较推荐,在主次坐标轴中也能正常显示)

plt.legend([line2, line3],['a','b'],loc='upper left')
# 第一个参数代表图形对象,第二个参数分别代表各自的标签(也就是label)
  • 1
  • 2

(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

(3)legend函数的其他参数

facecolor=‘green’,

edgecolor=‘black’

frameon=True,

shadow=True,

framealpha=0.5,

fontsize=‘xx-large’

。。。。

4、标题title的相关设置

(1)2种设置标题的方式

1、
fig1.set_title(‘title’)

2、
plt.title(‘title’)

(2)其他参数设置

loc: {‘center’, ‘left’, ‘right’},设置标题显示的位置

pad:float 设置标题距离图像上边缘有多远

fontsize: 设置字体大小

color=‘red’ 设置字体颜色

5、与网格grid相关的设置

which={‘major’,‘minor’,‘both’} #在win10下设置了没有用,minor设置完是白板,不知道是不是系统问题

axis=‘x’,       只显示哪一个轴或者是两者

color=‘r’,     颜色

linestyle=’-’,   线型

linewidth=2   线粗


三、文本(Text)以及注释(Annotation)——图形标注的两种方式

1、文本Text

两种设置方式

fig.text(x, y, s)

plt.text(x, y, s)
  • 1
  • 2
  • 3

其中,x,y表示的是文本插入的位置,s表示的是文本的内容,还有一些常见的可选参数如下:

Alpha=0.5

color=‘green’,

backgroundcolor=‘yellow’

fontsize=15,(可以简写为size)

fontstyle=20(可以简写为style)

rotation=‘60’,表示文本旋转多少度

fontweight=‘bold’,(可以简写为weight)取值可以是(normal bold heavy light ultrabold ultralight)

family=‘serif’ 字体

linespace=1.5  字体行距

horizontalalignment=‘center’ x参数表示的位置(也可以简写为ha)取值可以是(center right left)

verticalalignment=‘center’  y参数表示的位置(简写为va)取值可以是(center top bottom baseline)

multialignment=‘center’ 多行位置控制 取值可以是(center right left)

注意:最后三个参数负责布局的控制

(1)设置文本边框----通过bbox来完成

bbox_props =dict(boxstyle="rarrow", fc="red", ec="0.5",alpha=0.9,pad=0.6)
  • 1

1、
plt.text(参数列表,bbox=bbox_props) 通过设定bbox参数来完成,

2、
text_01=plt.text(参数列表) #首先返回文本对象

text_01.set_bbox(bbox_props)
  • 1

注意:bbox接收的参数必须是字典类型。

(2)注释annotate

两种设置方法

1、
fig.annotate(s, xy=(1,2), xytext=(3,6))

2、
plt.annotate(s, xy=(1,2), xytext=(3,6))

其中,xy表示的是注释插入的位置,s表示的是文本的内容,xytext注释文本的位置

其他一些常用的属性:

bbox:设置注释文字的边框,同上面的文本text是一样的。

arrowprops : 箭头样式设置,必须是字典类型的

width   the width of the arrow in points

headwidth   the width of the base of the arrowhead in points

headlength  the length of the arrow head in points

shrink       fraction of total length to 'shrink’from both ends

color  设置颜色

arrowstyle  设置箭头样式

connectionstyle设置箭头和文本连接的样式

connectionstyle的取值可以为(angle  angle3    arc  arc3 bar )

arrowstyle 的取值可以为(’-’   ‘->’  ‘-[’   ‘|-|’  ‘-|>’  ‘<-’  ‘<->’  ‘<|-’ ‘<|-|>’   ‘fancy’  ‘simple’  ‘wedge’)

发布了26 篇原创文章 · 获赞 3 · 访问量 2279


所属网站分类: 技术文章 > 博客

作者:加班是一种习惯

链接:https://www.pythonheidong.com/blog/article/233862/21571950993386794966/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

17 0
收藏该文
已收藏

评论内容:(最多支持255个字符)