https://www.jianshu.com/p/8bb06d3fd21b
matplotlib画散点图
- 简单版
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
# 10个点
N = 10
x = np.random.rand(N)
y = np.random.rand(N)
plt.scatter(x, y)
plt.show()
image.png
- 放大或者缩小点的大小
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
# 10个点
N = 10
x = np.random.rand(N)
y = np.random.rand(N)
# 每个点随机大小
s = (30*np.random.rand(N))**2
plt.scatter(x, y, s=s)
plt.show()
image.png
- 更改颜色和透明度
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
# 10个点
N = 10
x = np.random.rand(N)
y = np.random.rand(N)
# 每个点随机大小
s = (30*np.random.rand(N))**2
# 随机颜色
c = np.random.rand(N)
plt.scatter(x, y, s=s, c=c, alpha=0.5)
plt.show()
image.png
- 在一张图上绘制两组数据的散点
import matplotlib.pyplot as plt
import numpy as np
# 10个点
N = 10
x1 = np.random.rand(N)
y1 = np.random.rand(N)
x2 = np.random.rand(N)
y2 = np.random.rand(N)
plt.scatter(x1, y1, marker='o')
plt.scatter(x2, y2, marker='^')
plt.show()
image.png
- 为散点设置图例
import matplotlib.pyplot as plt
import numpy as np
# 10个点
N = 10
x1 = np.random.rand(N)
y1 = np.random.rand(N)
x2 = np.random.rand(N)
y2 = np.random.rand(N)
plt.scatter(x1, y1, marker='o', label="circle")
plt.scatter(x2, y2, marker='^', label="triangle")
plt.legend(loc='best')
plt.show()
image.png
- 绘制3D散点图
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
# numpy.random.randint(low, high=None, size=None, dtype='l')
# size 是随机数个数
xs = np.random.randint(30, 40, 100)
ys = np.random.randint(20, 30, 100)
zs = np.random.randint(10, 20, 100)
xs2 = np.random.randint(50, 60, 100)
ys2 = np.random.randint(30, 40, 100)
zs2 = np.random.randint(50, 70, 100)
xs3 = np.random.randint(10, 30, 100)
ys3 = np.random.randint(40, 50, 100)
zs3 = np.random.randint(40, 50, 100)
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(xs, ys, zs)
ax.scatter(xs2, ys2, zs2, c='r', marker='^')
ax.scatter(xs3, ys3, zs3, c='g', marker='*')
# 设置坐标轴名称
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
image.png
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
# 绘制散点图
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(X, Y, Z)
# 添加坐标轴(顺序是Z, Y, X)
ax.set_zlabel('Z', fontdict={'size': 15, 'color': 'red'})
ax.set_ylabel('Y', fontdict={'size': 15, 'color': 'red'})
ax.set_xlabel('X', fontdict={'size': 15, 'color': 'red'})
plt.show()
image.png
seaborn画散点图(以iris数据集为例)
import numpy as np
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
iris=pd.read_csv('iris.csv')
sns.lmplot('PetalLengthCm','SepalLengthCm',iris)
plt.show()
sns.lmplot('PetalLengthCm','SepalLengthCm',iris)
sns.lmplot('PetalLengthCm','SepalLengthCm',iris,hue='Species')
sns.lmplot('PetalLengthCm','SepalLengthCm',iris,hue='Species', fit_reg=False)
网友评论