legend标注 annotation标注 tick 能见度使用

# coding=utf-8
# https://blog.csdn.net/changzoe/article/details/78841152
"""
1. legend  标注
2. annotation 标注
3. tick 能见度
"""
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

x = np.linspace(-3, 3, 50)
y1 = 2 * x + 1
y2 = x ** 2

plt.figure(figsize=(9, 6))

# 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 就能自动的为我们添加图例.
l1, = plt.plot(x, y1, label='linear line')
l2, = plt.plot(x, y2, color='red', linewidth=1.0, linestyle='--', label='square line')

# legend 标注
# 参数 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() 返回的是一个列表.
# 其中’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,

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

plt.grid()
plt.show()
# ##################################### 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))
# plt.show()

# 然后标注出点(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')
# plt.show()

# 添加注释 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是对图中箭头类型的一些设置.
# plt.show()

# 添加注释 text
# 其中-3.7, 3,是选取text的位置, 空格需要用到转字符\ ,fontdict设置文本字体
plt.text(-3.7, 3, r'$This\ is\ the\ some\ text. \mu\ \sigma_i\ \alpha_t$',
         fontdict={'size': 16, 'color': 'r'})
plt.show()


# tick能见度
# 当图片中的内容较多,相互遮盖时,我们可以通过设置相关内容的透明度来使图片更易于观察,也即是通过本节中的bbox参数设置来调节图像信息
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))

# plt.show()

# 调整坐标,能见度
# 然后对被遮挡的图像调节相关透明度,本例中设置 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设置透明度
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mtj66

看心情

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值