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()