import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 50) # 范围是(-1,1);个数是50
y1 = 2 * x + 1
y2 = x ** 2
plt.figure() # 定义一个图像窗口
# plt.figure(num=3, figsize=(8, 5),) # figure定义一个图像窗口:编号为3;大小为(8, 5)
l1, = plt.plot(x, y1, linewidth=10, zorder=1, label='linear line')
l2, = plt.plot(x, y2, color='red', linewidth=1.0,
linestyle='--', label='square line')
plt.plot([0.5, 0.5, ], [0, 2, ], 'k--', linewidth=1.) # 画出一条垂直于x轴的虚线
plt.scatter([0.5, ], [2, ], s=50, color='b') # 画出一个点
plt.annotate( # 添加注释 annotate
r'$2x+1=%s$' % 2,
xy=(0.5, 2),
# 基于数据的值来选位置
xycoords='data',
# 标注位置的描述 和 xy 偏差值
xytext=(+30, -30), textcoords='offset points',
fontsize=16,
# 对图中箭头类型的一些设置
arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2"),
)
plt.text( # 添加注释 text
-1., 1,
r'$This\ is\ the\ some\ text. \mu\ \sigma_i\ \alpha_t$',
fontdict={'size': 10, 'color': 'r'})
plt.xlim((-1, 2)) # 坐标轴范围----------------------------------------------------
plt.ylim((-2, 3))
new_ticks = np.linspace(-1, 2, 5)
plt.xticks(new_ticks) # 轴刻度
plt.yticks( # 轴刻度
[-2, -1.8, -0.8, 0.8, 3],
[r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])
ax = plt.gca() # 获取当前坐标轴信息------------------------------------------------
ax.spines['right'].set_color('none') # 使用.set_color设置边框颜色:默认白色
ax.spines['top'].set_color('none') # 使用.set_color设置边框颜色:默认白色
# ax.spines['left'].set_color('none')
# ax.spines['bottom'].set_color('none')
ax.xaxis.set_ticks_position('bottom') # 激活x轴刻度
ax.spines['bottom'].set_position(('data', 0)) # 设置边框位置
ax.yaxis.set_ticks_position('left') # 激活y轴刻度
ax.spines['left'].set_position(('data', 0)) # 设置边框位置
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(12)
# 在 plt 2.0.2 或更高的版本中, 设置 zorder 给 plot 在 z 轴方向排序
label.set_bbox(dict(facecolor='white', # 前景色
edgecolor='None', # 设置边框
alpha=0.7, # 设置透明度
zorder=2)) # 设置 zorder 给 plot 在 z 轴方向排序
# plt.xlabel('I am x')
# plt.ylabel('I am y')
# plt.legend(loc='upper right') # 表示图例将添加在图中的右上角
plt.legend(handles=[l1, l2], labels=['up', 'down'], loc='best')
plt.show()
matplotlib基础合并
最新推荐文章于 2024-06-18 10:48:08 发布