matplotlib 基本用法

点击参考官网使用

在这里插入图片描述
执行结果:
在这里插入图片描述

设置图片大小 figure savefig figsize dpi

在这里插入图片描述
figsize: 指定figure的宽和高,单位为英寸, 1英寸等于2.5cm,
dpi: 指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80, A4 纸是 21*30cm的纸张
执行结果:
在这里插入图片描述

调整x或者y轴上的刻度 xticks

在这里插入图片描述

调整X或者Y轴上的刻度 xticks

在这里插入图片描述
执行结果:
在这里插入图片描述

设置中文 fontproperties font_manager

在这里插入图片描述
执行结果:
在这里插入图片描述

给图像添加描述信息 xlabel, ylabel title

在这里插入图片描述
在这里插入图片描述

自定义绘制图形的风格

在这里插入图片描述

为每条线添加图例 legend

在这里插入图片描述
在这里插入图片描述

xlabel、ylabel、title

import matplotlib.pyplot as plt
import numpy as np
'''figure和axes的声明可以省略(就是选用默认情况)'''
plt.plot([1,2,3,4])
# 如果使用中文设置需要另外添加参数,如果没有则不要使用中文,否则会乱码
plt.xlabel('some x numbers')  # 设置x坐标轴下的标签
plt.ylabel('some y numbers')  # 设置y坐标轴下的标题
plt.title('a straight line')  # 设置标题

输出结果:

在这里插入图片描述

plt.plot([1,2,3,4], [1,4,9,16], 'r+')
plt.axis([0, 5, 0, 30]) # 设置坐标轴 0,6 是设置x坐标轴的起点和终点,0,20是设置y坐标轴的起点和终点

在这里插入图片描述

t = np.arange(0, 20,1)
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')  # 绘制多条线 

在这里插入图片描述

def f(t):
    return np.exp(-t) * np.cos(2*np.pi*t)

t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)

plt.subplot(222) # nrows, ncols, index,即几行,几列,在第几个位置上显示
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'r-')

plt.subplot(223)
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')

在这里插入图片描述

plt.figure(1)                # the first figure 第一个图片,也就是指定要操作的是那个图片下的subplot、plot 
# 两行一列即总共两个图,上下关系,现在要操作图1中的第一个子图
plt.subplot(211)             # the first subplot in the first figure 第一个图片中的第一个子图 
plt.plot([1, 2, 3])
# 操作图1中的第二个小图
plt.subplot(212)             # the second subplot in the first figure
plt.plot([4, 5, 6])

在这里插入图片描述

# 操作图2
plt.figure(2)                # a second figure图二有一个子图
plt.plot([4, 5, 6])          # creates a subplot(111) by default

在这里插入图片描述

# 再次操作图1
plt.figure(1)                # figure 1 current; subplot(212) still current
plt.subplot(211)             # make subplot(211) in figure1 current
# 给图1中的第一个小图添加一个标题
plt.title('Easy as 1, 2, 3') # subplot 211 title
plt.plot([1, 2, 3])

在这里插入图片描述

fig, axes = plt.subplots(2, 3) # 创建一个两行三列的子图,返回一个figure即图片,axes是个列表,列表中的每一个元素代表一个子图

在这里插入图片描述

print('fig\n', fig, '\naxes\n', axes)
输出结果为:
fig
 Figure(432x288) 
axes
 [[<AxesSubplot:> <AxesSubplot:> <AxesSubplot:>]
 [<AxesSubplot:> <AxesSubplot:> <AxesSubplot:>]]

官方解释

You can clear the current figure with clf() and the current axes with cla().

使用clf()清除当前图片(figure),使用cla()清除当前axes 即.cla() 清除之前的设置

If you are making lots of figures, you need to be aware of one more thing: the memory required for a figure is not completely released until the figure is explicitly closed with close(). Deleting all references to the figure, and/or using the window manager to kill the window in which the figure appears on the screen, is not enough, because pyplot maintains internal references until close() is calle

如果有多个figures需要注意内存,因为如果不执行close()方法,删除所有的figure,而仅关闭figure窗口,每个figure是不会完全释放

# Fixing random state for reproducibility
np.random.seed(19680801)

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)

# the histogram of the data
n, bins, patches = plt.hist(x, 50, density=1, stacked=True, facecolor='g', alpha=0.75)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$') # 在坐标为(60, 0.025)的位置写上注释
plt.axis([40, 160, 0, 0.03])
plt.grid(True) # 显示网格 
plt.show()

在这里插入图片描述

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)

# 添加注释, 注释内容为local max',箭头起始位置在坐标点(2, 1)处,注释的起始位置在坐标点(3, 1.5)处
# 箭头使用黑色,缩放0.05
plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
            arrowprops=dict(facecolor='black', shrink=0.05),
            )
plt.ylim(-2,2)
plt.show()

在这里插入图片描述

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=.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35)
# hspace 设置垂直间距,wspace设置水平间距
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=.5, wspace=.5)
plt.show()

在这里插入图片描述

统计图的详细使用请点击

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值