matplotlib学习之图例,标注,透明度

legend 标注

定义的图像

import matplotlib as plt
import numpy as np

x = np.linspace(-3, 3, 50)

y1 = 2*x + 1

y2 = x**2



plt.figure()

# set x limits

plt.xlim((-1, 2))

plt.ylim((-2, 3))



# set new sticks

new_sticks = np.linspace(-1, 2, 5)

plt.xticks(new_sticks)

# set tick labels

plt.yticks([-2, -1.8, -1, 1.22, 3],

           [r'$really\ bad$', r'$bad$', r'$normal$', r'$god$', r'$really\ good$'])

首先我们设置两条线的类型等信息(蓝色实线与红色虚线).
legend将要显示的信息来自于上面代码中的 label. 所以我们只需要简单写下一下代码, plt 就能自动的为我们添加图例.

1, = plt.plot(x, y1, label='linear line')

l2, = plt.plot(x, y2, color='red', linewidth=1.0, linestyle='--', label='square line')



plt.legend(loc='upper right')

参数 loc=’upper right’表示图例右上角

  • 调整位置和参数

    如果我们想单独修改之前的 label 信息, 给不同类型的线条设置图例信息. 我们可以在 plt.legend 输入更多参数. 如果以下面这种形式添加 legend, 我们需要确保, 在上面的代码 plt.plot(x, y2, label=’linear line’) 和 plt.plot(x, y1, label=’square line’) 中有用变量 l1 和 l2 分别存储起来. 而且需要注意的是 l1, l2,要以逗号结尾, 因为plt.plot() 返回的是一个列表.

 plt.legend(handles=[l1, l2], labels=['up', 'down'],  loc='best')

其中’loc’参数有多种,’best’表示自动分配最佳位置,其余的如下:
‘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,

annotation标注

  • 画基本图
x = np.linspace(-3, 3, 50)

y = 2*x + 1



plt.figure(num=1, figsize=(8, 5),)

plt.plot(x, y,)



ax = plt.gca()

ax.spines['right'].set_color('none')

ax.spines['top'].set_color('none')

ax.spines['top'].set_color('none')

ax.xaxis.set_ticks_position('bottom')

ax.spines['bottom'].set_position(('data', 0))

ax.yaxis.set_ticks_position('left')

ax.spines['left'].set_position(('data', 0))

这里写图片描述
然后标注出点(x0, y0)的位置信息. 用plt.plot([x0, x0,], [0, y0,], ‘k–’, linewidth=2.5) 画出一条垂直于x轴的虚线.

x0 = 1

y0 = 2*x0 + 1

plt.plot([x0, x0,], [0, y0,], 'k--', linewidth=2.5)

plt.scatter([x0, ], [y0, ], s=50, color='b')

这里写图片描述

  • 添加注释 annotate
plt.annotate(r'$2x+1=%s$' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30),

             textcoords='offset points', fontsize=16,

             arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2"))

其中参数xycoords=’data’ 是说基于数据的值来选位置, xytext=(+30, -30) 和 textcoords=’offset points’ 对于标注位置的描述 和 xy 偏差值, arrowprops是对图中箭头类型的一些设置.

这里写图片描述

  • 添加注释 text
plt.text(-3.7, 3, r'$This\ is\ the\ some\ text. \mu\ \sigma_i\ \alpha_t$',

         fontdict={'size': 16, 'color': 'r'})



plt.show()

其中-3.7, 3,是选取text的位置, 空格需要用到转字符\ ,fontdict设置文本字体

tick能见度

当图片中的内容较多,相互遮盖时,我们可以通过设置相关内容的透明度来使图片更易于观察,也即是通过本节中的bbox参数设置来调节图像信息

  • 生成图形
import matplotlib.pyplot as plt

import numpy as np



x = np.linspace(-3, 3, 50)

y = 0.1*x



plt.figure()

plt.plot(x, y, linewidth=10, zorder=1)      # set zorder for ordering the plot in plt 2.0.2 or higher

plt.ylim(-2, 2)

ax = plt.gca()

ax.spines['right'].set_color('none')

ax.spines['top'].set_color('none')

ax.spines['top'].set_color('none')

ax.xaxis.set_ticks_position('bottom')

ax.spines['bottom'].set_position(('data', 0))

ax.yaxis.set_ticks_position('left')

ax.spines['left'].set_position(('data', 0))

这里写图片描述

  • 调整坐标,能见度
    然后对被遮挡的图像调节相关透明度,本例中设置 x轴 和 y轴 的刻度数字进行透明度设置
for label in ax.get_xticklabels() + ax.get_yticklabels():

    label.set_fontsize(12)

    # set zorder for ordering the plot in plt 2.0.2 or higher

    label.set_bbox(dict(facecolor='white', edgecolor='none', alpha=0.8, zorder=2))

plt.show()

其中label.set_fontsize(12)重新调节字体大小,bbox设置目的内容的透明度相关参,facecolor调节 box 前景色,edgecolor 设置边框, 本处设置边框为无,alpha设置透明度. 最终结果如下:
这里写图片描述

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值