【Python各个击破】matplotlib

导入

import matplotlib.pyplot as plt
import numpy as np

用法

# 根据x,y数组作图
fig, ax = plt.subplots()             
ax.plot([1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,100], [0.05,0.10,0.15,0.20,0.25,0.30,0.34,0.39,0.43,0.48,0.52,0.56,0.61,0.64,0.68,0.72,0.75,0.78,0.81,0.84,0.87,0.89,0.91,0.93,0.95,0.96,0.98,0.99,0.99,1.00,1.00,1.00,1.00,0.99,0.98,0.97,0.96,0.95,0.93,0.91,0.89,0.86,0.84,0.81,0.78,0.75,0.71,0.68,0.64,0.60,0.56,0.52,0.47,0.43,0.38,0.33,0.29,0.24,0.19,0.14,0.09,0.04,-0.01,-0.06,-0.11,-0.16,-0.21,-0.26,-0.30,-0.35,-0.40,-0.44,-0.49,-0.53,-0.57,-0.61,-0.65,-0.69,-0.72,-0.76,-0.79,-0.82,-0.85,-0.87,-0.89,-0.92,-0.94,-0.95,-0.97,-0.98,-0.99,-0.99,-1.00,-1.00,-1.00,-1.00,-0.99,-0.98,-0.97,-0.96
])
plt.show()                           

在这里插入图片描述

# 多组数据作图
x = np.linspace(-3.14, 3.14, 100)  # Sample data.
plt.subplots(figsize=(5, 2.7), layout='constrained')
plt.plot(x, x, label='linear')
plt.plot(x, np.sin(x), label='sin')
plt.plot(x, np.cos(x), label='cos')
plt.xlabel('x label')
plt.ylabel('y label') 
plt.title("Simple Plot") 
plt.legend()  # Add a legend.
<matplotlib.legend.Legend at 0x7f4aec257220>

在这里插入图片描述

# 统计图
mu, sigma = 0, 12
x = mu + sigma * np.random.randn(200000)
fig, ax = plt.subplots(figsize=(5, 2.7), layout='constrained')
# the histogram of the data
n, bins, patches = ax.hist(x, 50, density=True, facecolor='00', alpha=0.75)

ax.set_xlabel('x')
ax.set_ylabel('p')
ax.set_title('Axes labels and text')
ax.text(-32, 0.03, r'$\mu=0,\ \sigma=12$')
ax.grid(True)

在这里插入图片描述

# 图片
from matplotlib.colors import LogNorm

X, Y = np.meshgrid(np.linspace(-3, 3, 128), np.linspace(-3, 3, 128))
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)

fig, axs = plt.subplots(2, 2, layout='constrained')
pc = axs[0, 0].pcolormesh(X, Y, Z, vmin=-1, vmax=1, cmap='RdBu_r')
fig.colorbar(pc, ax=axs[0, 0])
axs[0, 0].set_title('pcolormesh()')

co = axs[0, 1].contourf(X, Y, Z, levels=np.linspace(-1.25, 1.25, 11))
fig.colorbar(co, ax=axs[0, 1])
axs[0, 1].set_title('contourf()')

pc = axs[1, 0].imshow(Z**2 * 100, cmap='plasma', norm=LogNorm(vmin=0.01, vmax=100))
fig.colorbar(pc, ax=axs[1, 0], extend='both')
axs[1, 0].set_title('imshow() with LogNorm()')

data1, data2, data3 = np.random.randn(3, 100)
pc = axs[1, 1].scatter(data1, data2, c=data3, cmap='RdBu_r')
fig.colorbar(pc, ax=axs[1, 1], extend='both')
fig.colorbar(pc, ax=axs[1, 1], extend='both')
axs[1, 1].set_title('scatter()')
Text(0.5, 1.0, 'scatter()')

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值