python中matplotlib的使用(二)

1、设置刻度、图例和坐标标签字体大小

1)、设置刻度大小

在这里插入图片描述

plt.xticks(fontsize=20)
plt.yticks(fontsize=20)

在这里插入图片描述

2)、设置坐标大小

plt.xlabel(..., fontsize=20)
plt.ylabel(..., fontsize=20)

在这里插入图片描述

3)、设置图例字体大小

plt.legend(..., fontsize=20)

2、添加注释(annotate)

plt.annotate('注释', xy=(2, 1), xytext=(3, 4),color='r',size=15,arrowprops=dict(facecolor='g', shrink=0.05))

在这里插入图片描述
PS:

  • 参数xy是需要加入注释的位置。
  • 参数xytext是注释内容的位置。
  • 参数xycoords 如下:
参数 介绍
figure points points from the lower left of the figure 点在图左下方
figure pixels pixels from the lower left of the figure 图左下角的像素
figure fraction fraction of figure from lower left 左下角数字部分
axes points points from lower left corner of axes 从左下角点的坐标
axes pixels pixels from lower left corner of axes 从左下角的像素坐标
axes fraction fraction of axes from lower left 左下角部分
data use the coordinate system of the object being annotated(default) 使用的坐标系统被注释的对象(默认)
polar(theta,r) if not native ‘data’ coordinates t
  • 参数color设置字体颜色。
  • 参数extcoords 设置注释文字偏移量:
参数 坐标系
‘figure points’ 距离图形左下角的点数量
‘figure pixels’ 距离图形左下角的像素数量
‘figure fraction’ 0,0 是图形左下角,1,1 是右上角
‘axes points’ 距离轴域左下角的点数量
‘axes pixels’ 距离轴域左下角的像素数量
‘axes fraction’ 0,0 是轴域左下角,1,1 是右上角
‘data’ 使用轴域数据坐标系
  • 参数size设置注释字体大小。
  • 参数verticalalignment:垂直对齐方式 ,参数:[ ‘center’ | ‘top’ | ‘bottom’ | ‘baseline’ ]
  • 参数horizontalalignment:水平对齐方式 ,参数:[ ‘center’ | ‘right’ | ‘left’ ]
  • 参数arrowprops中有关参数是箭头的相关信息,类型为dict:
    使用方式:
 arrowprops=dict(facecolor='g', shrink=0.05)
参数 介绍
width the width of the arrow in points 点箭头的宽度
headwidth the width of the base of the arrow head in points 在点的箭头底座的宽度
headlength the length of the arrow head in points 点箭头的长度
shrink fraction of total length to ‘shrink’ from both ends 总长度为分数“缩水”从两端
facecolor 箭头颜色
  • 参数bbox是有关注释边框的有关参数dict类:
    使用方式:
  bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='k',lw=1 ,alpha=0.5) 
参数 介绍
boxstyle 方框外形
facecolor(简写fc) 背景颜色
edgecolor(简写ec) 边框线条颜色
edgewidth 边框线条大小

**PS:**常用的方框外形有:

  1. square
  2. sawtooth
  3. roundtooth
  4. raarow
  5. larrow
  6. round
  7. round4

3、文本

plt.text(6, 5, "test", size=50, rotation=30.,ha="center", va="center",bbox=dict(boxstyle="round",ec=(1., 0.5, 0.5),fc=(1., 0.8, 0.8),))

在这里插入图片描述

  • 参数x,y:表示坐标值上的值 。
  • 参数color设置字体颜色。
  • 参数fontsize设置字体大小 。
  • 参数verticalalignment:垂直对齐方式 ,参数:[ ‘center’| ‘top’ | ‘bottom’ | ‘baseline’ ]
  • 参数horizontalalignment:水平对齐方式 ,参数:[‘center’ | ‘right’ | ‘left’ ]
  • 参数xycoords选择指定的坐标轴系统,参数相关内容同上。
  • 参数bbox给标题增加外框 ,参数相关内容同上。

代码参考:

https://github.com/ZhangJiangtao-0108/pythonmatplotlib_example.py文件

发布了9 篇原创文章 · 获赞 2 · 访问量 99

猜你喜欢

转载自blog.csdn.net/jocker_775065019/article/details/104866492