做数据可视化必不可少的要了解一些绘图包和相关函数的使用,Python中scatter函数就是一个很好的工具。scatter函数参数众多,为了方便使用,小编这里整理了其参数说明,并且以实例来增加可读性让读者朋友们有一目了然的感觉。
1、scatter函数原型
2、其中散点的形状参数marker如下:
3、其中颜色参数c如下:
4、基本的使用方法如下:
[python]
view plain copy
-
-
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'
)
-
-
plt.xlabel(
'X'
)
-
-
plt.ylabel(
'Y'
)
-
-
ax1.scatter(x,y,c =
'r'
,marker =
'o'
)
-
-
plt.legend(
'x1'
)
-
-
plt.show()
结果如下:
5、当scatter后面参数中数组的使用方法,如s,当s是同x大小的数组,表示x中的每个点对应s中一个大小,其他如c,等用法一样,如下:
(1)、不同大小
[python]
view plain copy
-
-
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'
)
-
-
plt.xlabel(
'X'
)
-
-
plt.ylabel(
'Y'
)
-
-
sValue = x*
10
-
ax1.scatter(x,y,s=sValue,c=
'r'
,marker=
'x'
)
-
-
plt.legend(
'x1'
)
-
-
plt.show()
(2)、不同颜色
[python]
view plain copy
-
-
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'
)
-
-
plt.xlabel(
'X'
)
-
-
plt.ylabel(
'Y'
)
-
-
cValue = [
'r'
,
'y'
,
'g'
,
'b'
,
'r'
,
'y'
,
'g'
,
'b'
,
'r'
]
-
ax1.scatter(x,y,c=cValue,marker=
's'
)
-
-
plt.legend(
'x1'
)
-
-
plt.show()
结果:
(3)、线宽linewidths
[python]
view plain copy
-
-
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'
)
-
-
plt.xlabel(
'X'
)
-
-
plt.ylabel(
'Y'
)
-
-
lValue = x
-
ax1.scatter(x,y,c=
'r'
,s=
100
,linewidths=lValue,marker=
'o'
)
-