数据可视化

# matplotlib 可以创建出版书籍中的绘图工具包

import matplotlib.pyplot as plt

import numpy as np

# 正态直方图

import scipy

from scipy import stats

# 创建一维数组

arr = np.random.rand(100)

arr = np.arange(100)

# 折线图

plt.plot(arr)

plot.show()

x = np.linspace(-5, 15, 50)

# 正态分布线图

plt.plot(x, stats.norm.pdf(x=x, loc=5, scale=2))

# hist直方图

# 1.数据 2.bins 柱子的数量(宽度 值越大宽度越窄)

# loc 均值 标准差 alpha 透明度

plt.hist(stats.norm.rvs(loc=5, scale=2, size=1000), bins=50, normed=True, color='r', alpha=0.7)

# 这是一个简单的直方图

# 1.准备数据

arr = np.random.randn(100)

plt.hist(arr, bins=50, normed=True, color='b', alpha=0.7)

# 散点图

x = np.arange(50)

y = x+5 * np.random.randn(50)

plt.scatter(x, y)

# 正弦余弦图

x = np.linspace(-2*np.pi, 2*np.pi, 100)

c = np.cos(x)

s = np.sin(x)

t = np.tan(x)

plt.plot(x, c)

plt.plot(x, s)

plt.plot(x, t)

# 柱状图

x = np.arange(5)

y1 = np.random.randint(1, 25, size=5)

y2 = np.random.randint(1, 20, size=5)

plt.bar(x, y1, 0.25, color='r')

# 在指定的子图上进行绘图

ax = plt.subplot(1, 1, 1)

width = 0.25

ax.bar(x, y1, width, color='r')

ax.bar(x+width, y2, width, color='g')

# 指定x轴标签及位置

ax.set_xticks(x+width-(width/2))

ax.set_xticklabels(['a', 'b', 'c', 'd', 'e'])

ax2 = plt.subplot(2, 2, 2)

ax2.bar(x, y1+5, 0.25, color='g')

# 矩阵图

m = np.random.rand(10,15)

# plt.cm.gray 灰色 plt.cm.ocean 海洋色

plt.imshow(m, camp=plt.cm.ocean)

# 颜色值

plt.colorbar()

plt.subplots() 获取一个指定行和列的图表数组

# 返回的是一个元组, 元组中存放figure对象和子图数组

fig, sp_arr = plt.subplots(2, 2)

sp_arr[0, 0].hist(np.random.randn(100), bins=10, color='b', alpha=0.5)

sp_arr[0, 1].plot(np.random.randn(100))

sp_arr[1, 0].scatter(np.arange(50), x+5*np.random.randn(50))

fig, axes = plt.subplots(2)

axes[0].plot(np.random.randint(0, 100, 50), 'rp')

axes[1].plot(np.random.randint(0, 100, 50), color='r', linstyle='dashed', marker="*")


fig, ax = plt.subplots(1)

# 设置x轴刻度范围

ax.set_xlim([0,800])

# 设置显示的刻度

ax.set_xticks(range(0, 500, 100))

# 设置y轴刻度标签

ax.set_yticklabels(['', 'python', 'java', 'php'])

# 设置座标轴标签

ax.set_xlabel('numbers')

ax.set_ylabel('categray')


# 设置图示标题

ax.set_title('jobs_number')

# label 设置图例

ax.plot(np.random.randn(1000).cumsum(), label='line1')

ax.plot(np.random.randn(1000).cumsum(), label='line2')

ax.plot(np.random.randn(1000).cumsum(), label='line3')

# 加载图例

# best 选取最优摆放位置

ax.legend(loc='best')

plt.show()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章