机器学习-数据可视化

#画3D散点图
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#生成一个空白图像
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
n = 250
#生成lambda函数来生成给定范围值
f = lambda minval, maxval, n:minval + (maxval - minval) * np.random.rand(n)
#生成值
x_vals = f(15, 41, n)
y_vals = f(-10, 70, n)
z_vals = f(-52, -37, n)
#画出这些值
ax.scatter(x_vals, y_vals, z_vals, c = 'k', marker = 'o')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
plt.show()

#画气泡图
num_vals = 40
x = np.random.rand(num_vals)
y = np.random.rand(num_vals)
#制定最大半径
max_radius = 25
area = np.pi * (max_radius * np.random.rand(num_vals))**2
#定义颜色
colors = np.random.rand(num_vals)
plt.scatter(x , y, s = area, c = colors, alpha=1.0)
plt.show()

#画饼图
labels = ['China', 'Swiss', 'USA', 'UK', 'Laos', 'Spain']
X = [222, 42, 455, 664, 454, 334]
#定义可视化的颜色
colors = ['orange', 'lightgreen', 'lightblue', 'gold', 'cyan', 'red']
#定义是否需要突出一部分
explode = {0, 0, 0, 0, 0, 0}
plt.pie(X, labels=labels, colors= colors, autopct='%1.2f%%')  # 画饼图(数据,数据对应的标签,百分数保留两位小数点)
plt.title("Pie chart")
#设置饼图的宽高比
plt.axis('equal')
plt.show()

#画直方图
#输入数据
apples = [30, 25, 22, 36, 21, 29]
oranges = [24, 33, 19, 27, 35, 20]
#设置组数
num_groups = len(apples)
fig, ax = plt.subplots()
#定义x轴
indices = np.arange(num_groups)
bar_width = 0.4
opacity = 0.6
#画直方图
hist_apples = plt.bar(indices, apples, bar_width, alpha = opacity, color = 'g', label = 'Apple')
hist_oranges = plt.bar(indices + bar_width, oranges, bar_width, alpha = opacity, color = 'b', label = 'Orange')
#设置直方图的参数
plt.xlabel('Month')
plt.ylabel('Production quantity')
plt.title('Comparing apples and oranges')
plt.xticks(indices + bar_width, ('Jan','Feb','Mar','Apr','May','Jun'))
plt.ylim([0, 45])
plt.legend()
plt.tight_layout()
plt.show()


猜你喜欢

转载自blog.csdn.net/u012967763/article/details/79218726