文章目录
一、简介:
1.安装
pip install matplotlib
2.import as
import matplotlib as mpl
import matplotlib.pyplot as plt
三、图的元素设置
总汇
功能 | 函数 | 参数 |
---|---|---|
显示图片 | %matplot inline、%matplot notebook与plt.show() | null |
标签-图片 | plt.title() | 一般是字符串 |
标签-x坐标轴 | plt.xlabel() | |
标签-y坐标轴 | plt.ylabel() | |
网格 | plt.grid() | 必选参数True,可选参数color |
边界-x轴 | plt.xlim() | 元组类型或是列表类型 |
边界-y轴 | plt.ylim() | |
刻度-x轴 | plt.xticks() | 列表类型,range(),np.arang() |
刻度-y轴 | plt.yticks() | |
图例 | plt.legend() |
设定整个画布的尺寸
设定整个画布的尺寸(产生一个新画布)
# 英寸
plt.figure(figsize=(3, 3))
"""
产生两个画布
"""
plt.figure(figsize=(3, 3))
plt.plot([0,1,2,3])
plt.figure(figsize=(3, 3))
plt.plot([3,2,1,0])
显示出来
【%matplot inline
、%matplot notebook
与plt.show()
】:
解释:
- 这些都是用来显示绘制的图像的,只用打一个就行了。
plt.show()
是各IDE通用的。%matplot inline
是专在Jupyter中用来自动显示图像的。将图像以静态方式显示。%matplot notebook
是专在Jupyter中用来自动显示图像的。将图像以动态方式显示。
例如:
- Jupyter notebook的%matplot notebook:
- Jupyter notebook的%matplot inline:
- 其他IDE,如IDLE:
工具Jupyter Notebook安装:
pip install jupyter
保存的图像分辨率
# 图片像素
matplotlib.rcParams['figure.figsize']
# 分辨率
matplotlib.rcParams['savefig.dpi']
# 保存图片,并指定分辨率
plt.savefig('plot123_2.png',dpi=200)
标题文字
区域 | 函数 |
---|---|
图片 | plt.title() |
x坐标轴 | plt.xlabel() |
y坐标轴 | plt.ylabel() |
from matplotlib import pyplot as plt
x = [5,2,7]
y = [2,16,4]
plt.plot(x,y)
#图片的标题
plt.title('Image Title')
#坐标轴X轴
plt.xlabel('X axis')
#坐标轴Y轴
plt.ylabel('Y axis')
plt.show()
显示中文标题
将上这些
#coding=utf-8
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
#更改字体
mpl.rcParams['axes.unicode_minus'] = False
#解决更改字体导致显示不出负号的后遗症
图例
plt.legend()
网格
from matplotlib import pyplot as plt
x = [5,8,10]
y = [12,16,6]
plt.plot(x,y)
#显示网格
#必选参数True,可选参数color
plt.grid(True,color='blue')
plt.show()
边界
限定数据显示的部分:a~b,包括b的
from matplotlib import pyplot as plt
x = [5,8,10]
y = [12,16,6]
plt.plot(x,y)
# 方式一:元组类型
plt.xlim((4,12))
# 方式二:列表类型
plt.ylim([5,20])
plt.show()
刻度
注意:plt.plot(x, y)
要放在plt.xticks()
前面才有用。
import numpy as np
from matplotlib import pyplot as plt
x = [5, 8, 10]
y = [12, 16, 6]
plt.plot(x, y)
plt.xticks(range(4,12)) # 列表类型
plt.yticks(np.arange(5, 20, 1.5)) # numpy的ndarray类型
# 内置的range()函数生成的整数数列也行,plt.yticks(range(3,20))
# 但range()只能生成整数数列,而不如np.arange()可以生成浮点数数据
plt.show()
只是标注轴上的刻度,并不能代替边界,不想见到的数据还是会出现
import numpy as np
from matplotlib import pyplot as plt
x = [5,8,10]
y = [12,16,6]
plt.plot(x,y)
plt.xticks([7,8,9,10,11])
plt.yticks(np.arange(5,20,1.5))
plt.show()
刻度精度
设置刻度的小数位数即精度
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.ticker as ticker
x = [5, 8, 10]
y = [12, 16, 6]
plt.plot(x, y)
plt.xticks(range(4,12)) # 列表类型
plt.yticks(np.arange(5, 20, 1.5)) # numpy的ndarray类型
plt.gca().xaxis.set_major_formatter(ticker.FormatStrFormatter('%.3f')) # x轴
plt.gca().yaxis.set_major_formatter(ticker.FormatStrFormatter('%.3f')) # y轴
plt.show()
刻度密度问题
方法1:直接少填几个显示出来的刻度xticks
就行
import numpy as np
from matplotlib import pyplot as plt
x = [5, 8, 10]
y = [12, 16, 6]
plt.plot(x, y)
plt.xticks([4, 8, 12]) # 列表类型
plt.yticks(np.arange(5, 20, 1.5)) # numpy的ndarray类型
plt.show()
方法2:
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.ticker as ticker
x = [5, 8, 10]
y = [12, 16, 6]
plt.plot(x, y)
plt.xticks(range(4,13)) # 列表类型
plt.yticks(np.arange(5, 20, 1.5)) # numpy的ndarray类型
tick_spacing = 4
plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
plt.show()
绘图风格
- 使用
plt.style.use('')
设定风格: - 使用
plt.style.available
显示库中的风格:如ggplot
、fivethirtyeight
、dark_background
、grayscale
绘制图片
import numpy as np
import matplotlib.pyplot as plt
# 设定整个画布的尺寸
plt.figure(figsize=(7, 7))
data = np.random.randint(0, 256, (10, 10, 3))
# 绘制10*10像素的
plt.subplot(4, 2, 1)
plt.imshow(data)
# R红色单通道
plt.subplot(4, 2, 3)
plt.imshow(data[:, :, 0])
# R红色的映射灰度
plt.subplot(4, 2, 4)
plt.imshow(data[:, :, 0], cmap='gray')
# G绿色单通道
plt.subplot(4, 2, 5)
plt.imshow(data[:, :, 1])
# G绿色的映射灰度
plt.subplot(4, 2, 6)
plt.imshow(data[:, :, 1], cmap='gray')
# B蓝色单通道
plt.subplot(4, 2, 7)
plt.imshow(data[:, :, 2])
# B蓝色的映射灰度
plt.subplot(4, 2, 8)
plt.imshow(data[:, :, 2], cmap='gray')
# 显示画布
plt.show()
绘制子图
四、图的类型
折线图plt.plot
import matplotlib.pyplot as plt
markers = {
'.': 'point',
',': 'pixel',
'^': 'triangle_up',
'<': 'triangle_left',
'>': 'triangle_right',
'+': 'plus',
'|': 'vline',
'_': 'hline',
'*': 'star',
'd': 'thin_diamond',
'D': 'diamond',
'h': 'hexagon1',
'H': 'hexagon2',
'p': 'pentagon',
'P': 'plus_filled',
'x': 'x',
'X': 'x_filled',
's': 'square',
'o': 'circle',
'v': 'triangle_down',
'1': 'tri_down',
'2': 'tri_up',
'3': 'tri_left',
'4': 'tri_right',
'8': 'octagon',
0: 'tickleft',
1: 'tickright',
2: 'tickup',
3: 'tickdown',
4: 'caretleft',
5: 'caretright',
6: 'caretup',
7: 'caretdown',
8: 'caretleftbase',
9: 'caretrightbase',
10: 'caretupbase',
11: 'caretdownbase',
'None': 'nothing',
None: 'nothing',
' ': 'nothing',
'': 'nothing'
}
plt.figure(figsize=(8, 15))
plt_index = 1
for maker in markers:
y = [1, 3, 10, 2, 5]
plt.subplot(9, 5, plt_index)
plt.plot(y, marker=maker) # here
plt.title('[{}]'.format(maker))
plt_index += 1
plt.tight_layout()
plt.savefig('plot123_2.png', dpi=300)
散点图
import matplotlib.pyplot as plt
markers = {
'.': 'point',
',': 'pixel',
'^': 'triangle_up',
'<': 'triangle_left',
'>': 'triangle_right',
'+': 'plus',
'|': 'vline',
'_': 'hline',
'*': 'star',
'd': 'thin_diamond',
'D': 'diamond',
'h': 'hexagon1',
'H': 'hexagon2',
'p': 'pentagon',
'P': 'plus_filled',
'x': 'x',
'X': 'x_filled',
's': 'square',
'o': 'circle',
'v': 'triangle_down',
'1': 'tri_down',
'2': 'tri_up',
'3': 'tri_left',
'4': 'tri_right',
'8': 'octagon',
0: 'tickleft',
1: 'tickright',
2: 'tickup',
3: 'tickdown',
4: 'caretleft',
5: 'caretright',
6: 'caretup',
7: 'caretdown',
8: 'caretleftbase',
9: 'caretrightbase',
10: 'caretupbase',
11: 'caretdownbase',
'None': 'nothing',
None: 'nothing',
' ': 'nothing',
'': 'nothing'
}
plt.figure(figsize=(8, 15))
plt_index = 1
for maker in markers:
y = [1, 3, 10, 2, 5]
plt.subplot(9, 5, plt_index)
plt.scatter(range(len(y)), y, marker=maker) # here
plt.title('[{}]'.format(maker))
plt_index += 1
plt.tight_layout()
plt.savefig('plot123_2.png', dpi=300)