0%

使用Matplotlib进行简单绘图

数据可视化库(Matplotlib)

1. 常规绘图方法

1
2
import matplotlib.pyplot as plt
%matplotlib inline
1
2
3
plt.plot([1,2,3,4,5],[1,4,9,16,25])
plt.xlabel('xlabel', fontsize = 16)
plt.ylabel('ylabel')
Text(0, 0.5, 'ylabel')

1.1 细节设置

image.png

image.png

1
2
3
plt.plot([1,2,3,4,5],[1,4,9,16,25],'-.',color = 'r')
plt.xlabel('xlabel', fontsize = 16)
plt.ylabel('ylabel')
Text(0, 0.5, 'ylabel')

png

1
2
3
4
5
import numpy as np
array = np.arange(0,10,0.5)
plt.plot(array,array,'r--')
plt.plot(array,array ** 2, 'bs')
plt.plot(array,array ** 3, 'go')
[<matplotlib.lines.Line2D at 0x20291197cc0>]

png

1
2
3
x = np.linspace(-10,10)
y = np.sin(x)
plt.plot(x,y,linewidth = 3.0)
[<matplotlib.lines.Line2D at 0x20291252a58>]

png

1
2
3
x = np.linspace(-10,10)
y = np.sin(x)
plt.plot(x,y, color = 'b',linestyle = ':', marker = 'o', markerfacecolor = 'r', markersize = 10)
[<matplotlib.lines.Line2D at 0x202912c46a0>]

png

1
2
3
line = plt.plot(x,y)
#alpha表示透明度
plt.setp(line,color = 'r', linewidth = 2.0, alpha = 0.4)
[None, None, None]

png

1.2 子图与标注

1
2
3
4
plt.subplot(211)
plt.plot(x,y,color = 'r')
plt.subplot(212)
plt.plot(x,y,color = 'b')
[<matplotlib.lines.Line2D at 0x202913e1e10>]

png

1
2
3
4
plt.subplot(121)
plt.plot(x,y,color = 'r')
plt.subplot(122)
plt.plot(x,y,color = 'b')
[<matplotlib.lines.Line2D at 0x2029152ee10>]

png

1
2
3
4
plt.subplot(321)
plt.plot(x,y,color = 'r')
plt.subplot(324)
plt.plot(x,y,color = 'b')
[<matplotlib.lines.Line2D at 0x20292784eb8>]

png

1
2
3
4
5
6
7
8
9
10
11
12
#标注解释说明
plt.plot(x,y,color = 'b', linestyle = ':', marker = 'o', markerfacecolor = 'r', markersize = 10)
plt.xlabel('x:---')
plt.ylabel('y:---')
#图题
plt.title('1231231')
#指定坐标叫注释
plt.text(0,0, 'yuandian')
#显示网格
plt.grid(True)
#添加箭头,指定箭头的起始终止点和属性
plt.annotate('jiadedian', xy=(-5,0),xytext=(-2,0.3), arrowprops = dict(facecolor = 'red', shrink=0.05,headlength = 20, headwidth=20) )
Text(-2, 0.3, 'jiadedian')

png

1.3 风格设置

1
plt.style.available
['bmh',
 'classic',
 'dark_background',
 'fast',
 'fivethirtyeight',
 'ggplot',
 'grayscale',
 'seaborn-bright',
 'seaborn-colorblind',
 'seaborn-dark-palette',
 'seaborn-dark',
 'seaborn-darkgrid',
 'seaborn-deep',
 'seaborn-muted',
 'seaborn-notebook',
 'seaborn-paper',
 'seaborn-pastel',
 'seaborn-poster',
 'seaborn-talk',
 'seaborn-ticks',
 'seaborn-white',
 'seaborn-whitegrid',
 'seaborn',
 'Solarize_Light2',
 'tableau-colorblind10',
 '_classic_test']
1
2
3
4
#plt.style.use('dark_background')
# plt.style.use('bmh')
plt.style.use('ggplot')
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x20292977978>]

png

2. 常用图表绘制

2.1 条形图

1
2
3
4
5
6
7
8
plt.style.use('seaborn')
patterns = ('-', '+', 'x', '\\','*', 'o', 'O', '.')
mean_value = range(1, len(patterns) + 1)
x_pos = list(range(len(mean_value)))
bars = plt.bar(x_pos, mean_value, color = 'white')
for bar, pattern in zip(bars, patterns):
bar.set_hatch(pattern)
plt.show()

png

2.2 盒图

1
2
3
4
5
6
data = [np.random.normal(0,std,100) for std in range(1,4)]
fig = plt.figure(figsize = (8,6))
plt.boxplot(data, sym = 's', vert = True)
plt.xticks([y+1 for y in range(len(data))], ['x1','x2','x3'])
plt.xlabel('x')
plt.ylabel('box plot')
Text(0, 0.5, 'box plot')

png

2.3 直方图和散点图

1
2
3
4
5
data = np.random.normal(0,20,1000)
bins = np.arange(-100,100,5)
plt.hist(data,bins=bins)
plt.xlim([min(data) - 5, max(data) + 5])
plt.show()

png

1
2
3
4
5
6
N = 1000
x = np.random.randn(N)
y = np.random.randn(N)
plt.scatter(x, y, alpha = 0.3)
plt.grid(True)
plt.show()

png

2.4 3D图

1
2
3
4
5
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111,projection = '3d')
plt.show()

png

2.5 布局设置

image.png

1
2
3
4
5
6
7
8
9
10
#嵌套子图
x = np.linspace(0,10,1000)
y2 = np.sin(x**2)
y1 = x**2

fig, ax1 = plt.subplots()
left,bottom,width,height = [0.22,0.45,0.3,0.35]
ax2 = fig.add_axes([left,bottom,width,height])
ax1.plot(x,y1)
ax2.plot(x,y2)
[<matplotlib.lines.Line2D at 0x20294514828>]

png

1
2