Matplotlib学习笔记

Matplotlib学习笔记

Matplotlib is a library for making 2D plots of arrays in Python


入门

Pyplot tutorial

import matplotlib.pyplot as plt
import numpy as np

x_data = np.arange(0, 5, 0.2)
y_data = np.arange(0, 6, 0.24)

plt.figure('窗口名称')  # figure的窗口名称

plt.subplot(221)  # 分成2x2,占用第一个,即第一行第一列的子图
plt.title('name')  # 表的名称
plt.plot(x_data)
plt.xlabel('x axis')  # x轴的名称
plt.ylabel('y axis')  # y轴的名称
plt.grid(True)  # 窗格

plt.subplot(222)  # 分成2x2,占用第二个,即第一行第二列的子图
line, = plt.plot(x_data, linewidth=12.0)
line.set_antialiased(False)  # 关闭对象的锯齿效果,默认打开锯齿效果
plt.xlabel('x axis')
plt.ylabel('y axis')

plt.subplot(223)
plt.axis([0, 10, 0, 10])  # 设置轴的行程
plt.text(1, 8, r'$\mu=100,\ \sigma=15$')
line, = plt.plot(x_data, y_data, '-', linewidth=12.0)
line.set_antialiased(True)  # 关闭对象的锯齿效果,默认打开锯齿效果
plt.xlabel('x axis', fontsize=14, color='red')
plt.ylabel('y axis')

ax = plt.subplot(224)
t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2 * np.pi * t)
line, = plt.plot(t, s, lw=2)

# 第一个参数是注释的内容
# xy设置箭头尖的坐标
# xytext设置注释内容显示的起始位置
# arrowprops 用来设置箭头
# arrowprops=dict(facecolor='red', shrink=0.05, headlength=10, headwidth=10, width=5),
# facecolor 设置箭头的颜色
# headlength 箭头的头的长度
# headwidth 箭头的头的宽度
# width 箭身的宽度,即箭头后面的那根
plt.annotate('local max', xy=(2, 1), xytext=(2.5, 1.5),
             arrowprops=dict(facecolor='red', shrink=0.05, headlength=10, headwidth=10, width=5), )

plt.ylim(-2, 2)  # y行程轴限制
plt.show()

print('##############我是分割线#################')

结果:

8E085F45-D9D5-4DBD-A85A-E988B19F7C03.png
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)  # seed()方法改变随机数生成器的种子

# 模拟数据
# 高斯分布(Gaussian Distribution)的概率密度函数
# loc:float
# 此概率分布的均值(对应着整个分布的中心centre)
# scale:float
# 此概率分布的标准差(对应于分布的宽度,scale越大越矮胖,scale越小,越瘦高)
# size:int or tuple of ints
# 输出的shape,默认为None,只输出一个值

y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))

plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)

plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')
plt.grid(True)

# symmetric log
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthreshy=0.01)
plt.title('symlog')
plt.grid(True)
plt.show()
图片1
import matplotlib.pyplot as plt
import numpy as np

xdata = np.arange(0, 5, 0.2)
ydata = np.arange(0, 6, 0.24)

plt.figure('图表形状区分')

plt.subplot(521)
plt.plot(xdata, ydata, 'r--')
plt.subplot(522)
plt.plot(xdata, ydata, 'r--')
##############################################
plt.subplot(523)
plt.plot(xdata, ydata, 'bs')

plt.subplot(524)
plt.plot(xdata, ydata, 'gs')

##############################################
plt.subplot(525)
plt.plot(xdata, ydata, 'b^')

plt.subplot(526)
plt.plot(xdata, ydata, 'g^')
##############################################
plt.subplot(527)
plt.plot(xdata, ydata, 'bo')

plt.subplot(528)
plt.plot(xdata, ydata, 'go')

plt.show()
图片2

Image tutorial

Customizing Location of Subplot Using GridSpec

ax = plt.subplot2grid((2, 2), (1, 1))  # plt.subplot(223)
plt.show()

结果:


图片3
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
图片4
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1))
图片5

Tight Layout guide

进阶

高级

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容