欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 【Python各个击破】matplotlib

【Python各个击破】matplotlib

2025/5/9 11:12:02 来源:https://blog.csdn.net/bear_miao/article/details/143377245  浏览:    关键词:【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 LogNormX, 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()')

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词