matplotlib速记

import matplotlib.pyplot as plt
import  numpy as np
x = np.linspace(-3, 3, 50)
y = np.cos(x)
plt.figure(num=3, figsize=(8, 5))
y1 = np.sin(x)
l1, = plt.plot(x, y, label='linear line')  # 返回列表
l2, = plt.plot(x, y1, color='red', linewidth=2.0, linestyle='--', label='square line')
# plt.legend(loc='upper right')  # 右上方绘制图例
plt.legend(handles=[l1, l2], labels=['up', 'down'],  loc='best')  # 单独设置图例
# 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,
plt.xlim((0, 2))
plt.ylim((0, 3))
plt.xlabel('hello')
plt.ylabel('number')
# plt.yticks([-2, -1.8, -1, 1.22, 3],[r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])
ax = plt.gca()  # 获取当前坐标轴位置信息
ax.spines['right'].set_color('red')
ax.spines['top'].set_color('none')
x = 1
plt.plot([x, x,], [x, y[x],], 'k--', linewidth=2.5)
# set dot styles
plt.scatter([x,], [y[x]], s=50, color='b')
plt.show()


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 = 1
y0 = 2*x0 + 1
plt.plot([x0, x0,], [0, y0,], 'k--', linewidth=2.5)
# set dot styles
plt.scatter([x0, ], [y0, ], s=50, color='b')
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"))
plt.text(-3.7, 3, r'$This\ is\ the\ some\ text. \mu\ \sigma_i\ \alpha_t$',
         fontdict={'size': 16, 'color': 'r'})
plt.show()

猜你喜欢

转载自blog.csdn.net/weixin_36926779/article/details/81202491
今日推荐