matplotlib可视化之散点图plt.scatter()

  • 函数功能:散点图,寻找两个变量之间的关系
  • 调用方法:plt.scatter(x, y, s, c, marker, cmap, norm, alpha, linewidths, edgecolorsl)
  • 参数说明:
    • x: x轴数据
    • y: y轴数据
    • s: 散点大小
    • c: 散点颜色
    • marker: 散点形状
    • cmap: 指定特定颜色图,该参数一般不用,有默认值
    • alpha: 散点的透明度
    • linewidths: 散点边框的宽度
    • edgecolors: 设置散点边框的颜色

一、绘制两组100个随机数的散点图:

x = np.random.randn(100) # 横坐标
y = np.random.randn(100) # 纵坐标
plt.scatter(x, y,s = 50) # s表示点的大小
plt.show()

matplotlib可视化之散点图plt.scatter()_第1张图片

 

二、使用参数绘图:增加颜色,大小,透明度设置

#设置画布大小
plt.figure(figsize = (10, 6))

#设置颜色:根据颜色图和数字大小来控制点的颜色
colors = np.random.randn(100)
plt.scatter(x, y, 
            #大小
            s = np.power(10*x + 20*y, 2),  # 表示点的大小:(10x+20y)**2
            c = colors,          # 颜色
            marker = '*',        # 点的形状
            cmap = 'rainbow',    # 指定某个colormap值(颜色图)
            edgecolors = 'r',    # 散点边框颜色
            alpha = 0.6)         # 透明度

plt.title('彩虹五角星图',fontsize= 20,c = 'r')
plt.show()

matplotlib可视化之散点图plt.scatter()_第2张图片

 三、marker参数可指定值:

‘o’ 圆圈
‘+’ 加号
‘*’ 星号
‘.’ 点
‘x’ 叉号
‘square’ 或 ‘s’ 方形
‘diamond’ 或 ‘d’ 菱形
‘^’ 上三角
‘v’ 下三角
‘>’ 右三角
‘<’ 左三角
‘pentagram’ 或 ‘p’ 五角星(五角形)
‘hexagram’ 或 ‘h’ 六角星(六角形)

四、绘制多组散点图:

#设置画布大小
plt.figure(figsize=(10, 6), dpi=80)

colors = ['r', 'y', 'b']  # 设置颜色
markers = ['*', 'o', 'x'] # 设置点的形状

for i in range(0,3):
    y = np.random.rand(100)
    plt.scatter(x, y, s=60, c=colors[i], marker=markers[i])
    
plt.legend(['class1', 'class2', 'class3'], loc='upper right')
plt.show()

matplotlib可视化之散点图plt.scatter()_第3张图片

你可能感兴趣的:(python数据可视化,python,数据可视化,matplotlib)