Fork me on GitHub

用matplotlib包画散点图

Scatter函数是一个强大的画散点图函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, *, data=None, **kwargs)

#x,y:表示数据的位置,也就是x,y轴
#s:表示图形的大小
#c:表示颜色或颜色序列,可能的情况如下:

'''
1. 单一颜色
2. 颜色序列
3. 使用cmap映射到颜色的序列数
4. 一个行为RGB的2-D数组

marker:绘出的图形的形状,具有多种风格
'''

一、画散点图(一)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#散点图
#导入必要的模块
import numpy as np
import matplotlib.pyplot as plt

#参数随机数
N=50
x=np.random.randn(N)
y=np.random.randn(N)

#画散点图
plt.scatter(x,y,s=50,c='r',marker='o',alpha=0.5)
#x--->x轴,y--->y轴
#s--->散点面积
#c--->散点颜色
#颜色参数c:b-->blue c-->cyan g-->green k-->black m-->magenta r-->red
#w-->white y-->yellow
#marker--->散点形状
#marker='o'为圆点,marker='x'为×号,marker='s'显示为小正方形
#alpha--->散点透明度

#显示所画的图
plt.show()

scatter

二、画散点图(二)

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
#导入必要的模块 
import numpy as np
import matplotlib.pyplot as plt

#产生测试数据
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)

#设置标题
ax1.set_title('Scatter Plot')

#设置X轴标签
plt.xlabel('X')

#设置Y轴标签
plt.ylabel('Y')

#画散点图
ax1.scatter(x,y,c = 'r',marker = 'o')

#设置图标
plt.legend('x1')

#显示所画的图
plt.show()

scatter