matplotlib 之散点图scatter

matplotlib 之散点图scatter

1.基础画图

import matplotlib.pyplot as plt
import numpy as np
import random
import numpy
# X = [random.randint(0, 50) for _ in range(10)]  # 随机生成10个50以内的点的横坐标
# Y = [random.randint(0, 50) for _ in range(10)]  # 随机生成10个50以内的点的纵坐标
#
# plt.scatter(X, Y)  # 绘图
# plt.show()

2.点的大小

# X = [random.randint(0, 50) for _ in range(10)]  # 随机生成10个50以内的点的横坐标
# Y = [random.randint(0, 50) for _ in range(10)]  # 随机生成10个50以内的点的纵坐标
# s = [random.randint(50, 100) for _ in range(10)]
# plt.scatter(X, Y,s)  # 绘图,s为点的大小
# plt.show()

3.颜色设置

# X = [random.randint(0, 50) for _ in range(10)]  # 随机生成10个50以内的点的横坐标
# Y = [random.randint(0, 50) for _ in range(10)]  # 随机生成10个50以内的点的纵坐标
# s = [random.randint(50, 100) for _ in range(10)]
# c="r"#圆圈颜色
# plt.scatter(X, Y,s,c)  # 绘图,s为点的大小
# plt.show()

在这里插入图片描述

4.透明度

# X = [random.randint(0, 50) for _ in range(10)]  # 随机生成10个50以内的点的横坐标
# Y = [random.randint(0, 50) for _ in range(10)]  # 随机生成10个50以内的点的纵坐标
# s = [random.randint(50, 100) for _ in range(10)]
# c="r"#圆圈颜色
#
# plt.scatter(X, Y,s,c,alpha=0.4)  # 绘图,s为点的大小 alpha=0.4 透明度
# plt.show()

5.颜色条

# x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
# y = np.array([1, 4, 9, 16, 7, 11, 23, 18])
#
# c=numpy.array([0, 10, 20, 30, 40, 45, 50, 55])
# plt.scatter(x,y,s=100,c=c,alpha=0.5,cmap="viridis")#cmap 颜色条
# plt.colorbar()#显示颜色条
# plt.show()

在这里插入图片描述

6.多组散点

# 多组散点
# x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
# y = np.array([1, 4, 9, 16, 7, 11, 23, 18])
# z = np.array([4, 6, 9, 16, 22, 11, 23, 19])
# c=numpy.array([0, 10, 20, 30, 40, 45, 50, 55])
# plt.scatter(x,y,s=100,alpha=0.5,cmap="viridis")#cmap 颜色条
# plt.scatter(x,z,s=100,c=c,alpha=0.7,cmap="afmhot_r")#cmap 颜色条
# plt.colorbar()#显示颜色条
# plt.show()

7.点的形状

marker=“+”

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([1, 4, 9, 16, 7, 11, 23, 18])

c=numpy.array([0, 10, 20, 30, 40, 45, 50, 55])
plt.scatter(x,y,s=100,c=c,alpha=0.5,cmap="viridis",marker="+")#cmap 颜色条
plt.colorbar()#显示颜色条
plt.show()
marker:常见的形状有如下
".":点 ",":像素点  "o":圆形
"v":朝下三角形   "^":朝上三角形   "<":朝左三角形 
">":朝右三角形 "s":正方形  "p":五边星     
"*":星型  "h":1号六角形   "H":2号六角形 
"+":+号标记     "x":x号标记
"D":菱形        "d":小型菱形 
"|":垂直线形       "_":水平线形

8.添加x,y轴标签

plt.xlabel(‘X’)
plt.ylabel(‘y周’)

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([1, 4, 9, 16, 7, 11, 23, 18])

c=numpy.array([0, 10, 20, 30, 40, 45, 50, 55])
plt.scatter(x,y,s=100,c=c,alpha=0.5,cmap="viridis",marker="+")#cmap 颜色条
plt.colorbar()#显示颜色条
plt.xlabel('X')
plt.ylabel('y周')
plt.show()

9.添加标题

fig = plt.figure()
ax1 = fig.add_subplot(111)
# 设置标题
ax1.set_title('title')
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([1, 4, 9, 16, 7, 11, 23, 18])
fig = plt.figure()
ax1 = fig.add_subplot(111)
# 设置标题
ax1.set_title('title')
c=numpy.array([0, 10, 20, 30, 40, 45, 50, 55])
plt.scatter(x,y,s=100,c=c,alpha=0.5,cmap="viridis",marker="+")#cmap 颜色条
plt.colorbar()#显示颜色条
plt.xlabel('X')
plt.ylabel('y周')
plt.show()

10 三维散点图

import matplotlib.pyplot as plt
import numpy as np
import mpl_toolkits.mplot3d as p3d


fig = plt.figure()
ax = p3d.Axes3D(fig)
x = np.random.randn(1000)
y = np.random.randn(1000)
ax.scatter(x, y, c='b', s=10, alpha=0.5)
ax.scatter(x + 4, y, c='b', s=10, alpha=0.5)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
import mpl_toolkits.mplot3d as p3d

fig = plt.figure()
ax = p3d.Axes3D(fig)
z = 6 * np.random.randn(5000)
x = np.sin(z)
y = np.cos(z)
ax.scatter(x, y, z, c='b', s=10, alpha=0.5)
plt.show()
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值