matplotlib速记

本文将带你快速掌握matplotlib库,从基本图表到高级定制,包括设置图表样式、添加图例、调整坐标轴等,助你轻松制作专业图表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值