# sphinx_gallery_thumbnail_number = 3
import matplotlib.pyplot as plt
import numpy as np
#实验一 空白图
#最简单的figure创建方式,空白figure
if 0:
fig = plt.figure()
#定义居中标题
fig.suptitle('No axes on this figure')
plt.show()
#实验二 绘制多条曲线
#产生从0到2均匀分布的100个浮点nparray
if 0:
x = np.linspace(0, 2, 100)
plt.plot(x, x, label='linear')
plt.plot(x, x**2, label='quadratic')
plt.plot(x, x**3, label='cubic')
#设置x轴,y轴标签
plt.xlabel('x label')
plt.ylabel('y label')
#图表标题和曲线说明
plt.title("Simple Plot")
plt.legend()
plt.show()
#实验三 多张图
#重画一张figure,有2*2四张图表
if 0:
fig, ax = plt.subplots(2, 2)
#产生4组满足正态分布的100长度的数组
data1, data2, data3, data4 = np.random.randn(4, 100)
#第一行第一列绘制散点正态分布图,标记用x形
#第二行第一列绘制散点正态分布图,标记用o形
ax[0][0].plot(data1, data2, marker='x')
ax[1][0].plot(data1, data2, marker='o')
#第一行第二列绘制sin曲线图
x = np.arange(0, 10, 0.2)
y = np.sin(x)
ax[0][1].plot(x, y)
#第二行第二列绘制红色圆点的折线
ax[1][1].plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')
#横坐标0~6,纵坐标0~20
plt.axis([0, 6, 0, 20])
plt.show()
#实验四 散点图
if 0:
data = {'a': np.arange(50), #0~50共计50个整数
'c': np.random.randint(0, 50, 50), #50个0~50的随机数
'd': np.random.randn(50)} #50个正态分布的数值
data['b'] = data['a'] + 10 * np.random.randn(50)
data['d'] = np.abs(data['d']) * 100
#数据源是字典data,目录a为横坐标,b为纵坐标,c为颜色,s为大小
plt.scatter('a', 'b', c='c', s='d', data=data)
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()
#实验五 同一组xy数组绘制不同类型图
if 0:
names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]
plt.figure(1, figsize=(9, 3))
plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)
plt.plot(names, values)
plt.suptitle('Categorical Plotting')
plt.show()
#实验六 查询图表可设置的属性
if 0:
lines = plt.plot([1, 2, 3])
print(plt.setp(lines))
plt.show()
#plt.clf() #删除figure
#plt.cla() #删除axes
#plt.close() #删除图像包括内存
#实验七 添加文本
if 0:
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
#facecolor方块颜色,alpha透明度
n, bins, patches = plt.hist(x, 50, density=1, facecolor='g', alpha=0.75)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
#在(60,0.25)坐标位置插入μ=100,σ=15
plt.text(60, .025, r'$\mu=100,\ \sigma=15$',fontsize=14, color='red')
plt.axis([40, 160, 0, 0.03])
#显示格点
plt.grid(True)
plt.show()
#实验八 绘制标注
if 0:
ax = plt.subplot(111)
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表示标注的位置
plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
arrowprops=dict(facecolor='black', shrink=0.05),
)
plt.ylim(-2, 2)
plt.show()
#实验九 对数坐标系
if 0:
from matplotlib.ticker import NullFormatter # useful for `logit` scale
# Fixing random state for reproducibility
np.random.seed(19680801)
# make up some data in the interval ]0, 1[
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))
# plot with various axes scales
plt.figure(1)
# linear
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)
# log
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)
# logit
plt.subplot(224)
plt.plot(x, y)
plt.yscale('logit')
plt.title('logit')
plt.grid(True)
# Format the minor tick labels of the y-axis into empty strings with
# `NullFormatter`, to avoid cumbering the axis with too many labels.
plt.gca().yaxis.set_minor_formatter(NullFormatter())
# Adjust the subplot layout, because the logit one may take more space
# than usual, due to y-tick labels like "1 - 10^{-3}"
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,
wspace=0.35)
plt.show()
matplotlib官方例程实验 一
最新推荐文章于 2024-08-13 11:02:40 发布