matplotlib python学习笔记

2 篇文章 0 订阅
1 篇文章 0 订阅
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.gridspec as gridspec
from matplotlib import animation

# x = np.linspace(-3, 3, 50)
# y1 = x * x + 1
# y2 = 2 * x + 1
#
# plt.figure(num=3, figsize=(8, 5))
# plt.plot(x, y2, color='g', linewidth=0.8)
# plt.plot(x, y1, color='r', linewidth=1.0, linestyle='--')
#
# plt.xlim(-2, 2)
# plt.ylim(-6, 8)
# plt.xlabel('I am X')
# plt.ylabel('I am Y')
#
# new_ticks = np.linspace(-2, 3, 11)  # 设置刻度范围
# plt.xticks(new_ticks)
#
# ax = plt.gca()  # get current axis
# ax.spines['right'].set_color('none')  # 将上方刻度设置为透明
# ax.spines['top'].set_color('none')
# ax.xaxis.set_ticks_position('bottom')  # 将底部设置为主刻度
# ax.yaxis.set_ticks_position('left')
# ax.spines['bottom'].set_position(('data', 0))  # 设置X的起点
# ax.spines['left'].set_position(('data', 0))
#
# l1, = plt.plot(x, y2, label='up')
# l2, = plt.plot(x, y1, color='r', linewidth=1.0, linestyle='--')
# plt.legend(handles=[l1, l2], labels=['aaa', 'bbb'], loc='best')  # handles处理线条,labels给线条命名


# 添加annotation
# x0 = 1
# y0 = 2 * x0 + 1
# plt.scatter(x0, y0, s=50, color='b')  # 绘制散点图s:size
# plt.plot([x0, x0], [y0, 0], 'k--', lw=2.5)  # 'k--'黑色--,
#
# # method 1
# 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'))  # 百度
#
# # method 2
# plt.text(-3.7, 3, r'$This is the some text.\mu\sigma_i\alpha_t$',
#          fontdict={'size': 16, 'color': 'r'})
#
# # 将标注可视化
# for label in ax.get_xticklabels() + ax.get_yticklabels():
#     label.set_fontsize = (12)
#     label.set_bbox(dict(facecolor='w', edgecolor='None', alpha=0.7))  # background box,alpha为透明度


# 绘制散点图
# n = 1024
# X = np.random.normal(0, 1, n)  # 中位数:0 方差:1 个数:n
# Y = np.random.normal(0, 1, n)
# T = np.arctan2(Y, X) #优化颜色
#
# plt.scatter(X, Y, s=75, c=T, alpha=0.5)
#
# plt.xlim(-1.5, 1.5)
# plt.ylim(-1.5, 1.5)
#
# plt.xticks(())  # 取消刻度
# plt.yticks(())

# 绘制柱状图
# n = 12
# X = np.arange(n)
# Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1, n)  # 均匀分布12个值于0.5-1
# Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1, n)
#
# plt.bar(X, +Y1, facecolor='k', edgecolor='g')  # +-改变朝向
# plt.bar(X, -Y2, facecolor='r', edgecolor='y')
#
# for x, y in zip(X, Y1):
#     # ha:horizontal alignment--横向对齐
#     plt.text(x + 0.4, y + 0.05, '%.2f' % y, ha='center', va='bottom')
#
# for x, y in zip(X, Y2):
#     plt.text(x + 0.4, -y - 0.05, '%.2f' % y, ha='center', va='top')

# 绘制等高线
# def f(x, y):
#     # the height function
#     return (1 - x / 2 + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)  # 计算高度值
#
#
# n = 256
# x = np.linspace(-3, 3, n)
# y = np.linspace(-3, 3, n)
# X, Y = np.meshgrid(x, y)
#
# # use plt.contourf to fill contours
# # X,Y and value for(X,Y) point
# plt.contourf(X, Y, f(X, Y), 8, alpha=0.75, cmap=plt.cm.hot)
# # use plt.contour to add contour lines
# C = plt.contour(X, Y, f(X, Y), 8, colors='black', linewidth=.5)
# # adding label
# plt.clabel(C,inline=True,fontsize=10)

# 打印图片
# a=np.array([0.31,0.32,0.33,0.34,0.35,0.36
#             ,0.37,0.38,0.39]).reshape(3,3)
# plt.imshow(a,interpolation='nearest',cmap='bone',origin='lower')
# plt.colorbar(shrink=0.9)

# 3D图形绘制
# fig = plt.figure()
# ax = Axes3D(fig)
# X = np.arange(-4, 4, 0.25)
# Y = np.arange(-4, 4, 0.25)
# X, Y = np.meshgrid(X, Y)
# R = np.sqrt(X ** 2 + Y ** 2)
# Z = np.sin(R)
#
# ax.plot_surface(X, Y, Z, cstride=1, rstride=1, cmap=plt.get_cmap('rainbow'))
#
# ax.contourf(X, Y, Z, zdir='x ', offset=-2, cmap='rainbow')  #将图形延一方向压缩为等高线

# subplot多合一显示

# plt.figure()
#
# plt.subplot(2, 1, 1)  # 两行一列
# plt.plot([0, 1], [0, 1])
#
# plt.subplot(2, 3, 4)  # 两行三列
# plt.plot([0, 1], [0, 2])
#
# plt.subplot(2, 3, 5)
# plt.plot([0, 1], [0, 3])
#
# plt.subplot(2, 3, 6)
# plt.plot([0, 1], [0, 4])

# subplot分格显示:
# 1.subplot2grid
# plt.figure()
# ax = plt.subplot2grid((3, 3), (0, 0), colspan=3, rowspan=2)  # 三行三列,起点,列,行
# ax.plot([1,2],[1,2])
# ax.set_title('ax1_title')

# 2.gridspec
# plt.figure()
# gs = gridspec.GridSpec(3, 3)
# ax1 = plt.subplot(gs[0, :]) # 按照索引方式占位
# ax2 = plt.subplot(gs[1, :2])
# ax3 = plt.subplot(gs[1:, 2])
# ax4 = plt.subplot(gs[-1, 0])
# ax5 = plt.subplot(gs[-1, 1])

# 3.最简单

# fig,((ax11,ax12),(ax21,ax22))=plt.subplots(2,2,sharex=True,sharey=True)
# ax11.scatter([1,2],[2,2])

# 图中图

# fig = plt.figure()
# x = [1, 2, 3, 4, 5, 6, 7]
# y = [1, 3, 4, 2, 5, 8, 6]
#
# left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
# ax1 = fig.add_axes([left, bottom, width, height])
#
# ax1.plot(x, y, 'r')
# ax1.set_xlabel('x')
# ax1.set_ylabel('y')
# ax1.set_title('title')
#
# left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
# ax2 = fig.add_axes([left, bottom, width, height])
#
# ax2.plot(y, x, 'b')
# ax2.set_xlabel('x')
# ax2.set_ylabel('y')
# ax2.set_title('title inside 1')
#
# plt.axes([.6, 0.2, 0.25, 0.25])
# plt.plot(y[::-1], x, 'g')
# plt.xlabel('x')
# plt.ylabel('y')
# plt.title('title inside 2')
#
#  主次坐标轴
#
# x = np.arange(0, 10, 0.1)
# y1 = 0.05 * x ** 2
# y2 = -1 * y1
#
# fig, ax1 = plt.subplots()
# ax2 = ax1.twinx()  # 镜像
# ax1.plot(x, y1, 'g-')
# ax2.plot(x, y2, 'b--')
#
# ax1.set_xlabel('X data')
# ax1.set_ylabel('Y1',color='g')
# ax2.set_ylabel('Y2',color='b')

# 动画效果

# fig, ax = plt.subplots()
# x = np.arange(0, 2 * np.pi, 0.01)
# line, = ax.plot(x, np.sin(x))
#
#
# def animate(i):
#     line.set_ydata(np.sin(x + i / 100))
#     return line,
#
#
# def init():
#     line.set_ydata(np.sin(x + 100))
#     return line,
#
#
# ani = animation.FuncAnimation(fig=fig, func=animate, frames=100, init_func=init(), interval=10, blit=False)

# plt.show()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值