博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

数据可视化之绘图基础

Posted on 2019-05-19 09:54  心默默言  阅读(928)  评论(0编辑  收藏  举报

1.一个简单的实例

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [5, 4, 3, 2]

plt.figure()  # 创建一个figure()

plt.subplot(231) # divide subplots into 2*3 grid and select 1
plt.plot(x, y)

plt.subplot(232) # divide subplots into 2*3 grid and select 2
plt.bar(x, y)

plt.subplot(233)
plt.barh(x, y)

plt.subplot(234)
plt.bar(x, y)
y1 = [7, 8, 5, 3]
plt.bar(x, y1, bottom=y, color='r')

plt.subplot(235)
plt.boxplot(x)

plt.subplot(236)
plt.scatter(x, y)

plt.show()

 

 

工作原理

2.简单的正弦和余弦图

 

import matplotlib.pyplot as plt
import numpy as np

# generate uniformly distributed
# 256 points from -pi to pi, inclusive
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)

# these are vectorised versions
# of math.cos, and math.sin in built-in Python maths
# compute cos for every x
y = np.cos(x)

# compute sin for every x
y1 = np.sin(x)

# plot both cos and sin
plt.plot(x, y)
plt.plot(x, y1)

plt.show()

 以这个简单图表为基础,可以进一步定制化增加更多的信息,并且让坐标轴及其边界更精确些。

import matplotlib.pyplot as plt
import numpy as np

# generate uniformly distributed
# 256 points from -pi to pi, inclusive
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)

# these are vectorised versions
# of math.cos, and math.sin in built-in Python maths
# compute cos for every x
y = np.cos(x)

# compute sin for every x
y1 = np.sin(x)

# plot cos
plt.plot(x, y)

# plot sin
plt.plot(x, y1)

# define plot title
plt.title("Functions $\sin$ and $\cos$")

# set x limit
plt.xlim(-3.0, 3.0)
# set y limit
plt.ylim(-1.0, 1.0)

# format ticks at specific values
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],
           [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$']) # 为了用希腊字母表示
plt.yticks([-1, 0, +1],
           [r'$-1$', r'$0$', r'$+1$']) # 为了用希腊字母表示
plt.show()

3.设置坐标轴长度和范围

import matplotlib.pyplot as plt
import numpy as np

# generate uniformly distributed
# 256 points from -pi to pi, inclusive
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)

# these are vectorised versions
# of math.cos, and math.sin in built-in Python maths
# compute cos for every x
y = np.cos(x)

# compute sin for every x
y1 = np.sin(x)

# plot both cos and sin
plt.plot(x, y)
plt.plot(x, y1)
plt.axis([-3, 3, -1.5, 1.5])  # 设置坐标轴长度和范围
plt.show()

4.设置图表的线型、属性和格式化字符串

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-np.pi, np.pi, 256, endpoint=True)

y = np.cos(x)

y1 = np.sin(x)

plt.plot(x, y, lw=5,c='c')
plt.plot(x, y1,marker='+')

plt.show()

工作原理

5.折线图

5.1 坐标化成XY的对数坐标

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

# 以下两行来显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

plt.figure(num=1)
# np.arange(20)生成数组[1..20], np.exp():返回e(2.71828182846)的幂次方
x = pd.Series(np.exp(np.arange(20)))
x.plot(label=u'原始数据图', legend=True)  # legend=True让label标签在子图上可见
plt.show()
plt.figure(num=2)
x.plot(logy=True, label=u'对数数据图', legend=True)  # logy=True:y轴坐标为10的n次方
plt.show()

5.2 figure语法及操作

figure语法说明

figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)

num:图像编号或名称,数字为编号 ,字符串为名称
figsize:指定figure的宽和高,单位为英寸;
dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80      1英寸等于2.5cm,A4纸是 21*30cm的纸张 
facecolor:背景颜色
edgecolor:边框颜色
frameon:是否显示边框

5.3 更换坐标轴

import matplotlib.pyplot as plt

import numpy as np

# 生成一系列数据

x = np.linspace(-1, 1, 50)
y1 = x ** 2
y2 = 2 * x + 1

plt.figure(num=3, figsize=(8, 5))
plt.plot(x, y2)
plt.plot(x, y1, color='red', linewidth=10, linestyle='--')
# 图像的属性:颜色线宽线条样式

plt.xlim((-1, 2))
plt.ylim((-2, 3))
# 坐标轴的取值范围

plt.xlabel('I am x.')
plt.ylabel('I am y.')
# 坐标轴的标识

new_ticks = np.linspace(-1, 2, 5)
print(new_ticks)
# 打印坐标轴新的单位长度
plt.xticks(new_ticks)
# 更换坐标轴的单位长度
plt.yticks(np.linspace(-2, 5, 5))
# # 更换坐标轴不同单位长度上的标识,换成文字形式

#
# gca='get current axis'
# ax = plt.gca()
# ax.spines['right'].set_color('c')
# ax.spines['left'].set_color('r')

plt.show()

5.4刻度标记大小

import matplotlib.pyplot as plt

# 导入模块pyplot,并给它赋别名plt,以免后续重复输入pyplot,pyplot中包含了许多用于生成图表的函数

# 简单绘图
squares = [1, 4, 9, 16, 25]  # 创建一个列表,平方数
plt.plot(squares)  # 再将这个列表传递给函数plot(),这个函数会根据根据这些数字绘出有意义的图
# plt.show()#打开matplotlib查看器,并显示绘制图形

# 修改标签文字和线条粗细
plt.plot(squares, linewidth=5)  # linewidth决定了plot()绘制图像线条的粗细
plt.title("Square Numbers", fontsize=24)  # 设置图标标题,fontsize字型大小
plt.xlabel("Value", fontsize=14)  # 给x轴加标签
plt.ylabel("Square of Value", fontsize=14)  # 给y轴加标签
plt.tick_params(axis='both', labelsize=10)  # 设置刻度标记的大小
# tick_params设置刻度的样式,其中指定的实参将影响x轴y轴的刻度
# plt.show()

# 校正图形
# 当你向plot提供一系列数据时,他假设第一个数据点对应的x坐标值为0,
# 但我们第一个点对应的坐标值为1,为改变这种默认行为,我们可以给plot同时提供输入值和输出值
input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]
plt.plot(input_values, squares, linewidth=5)
plt.show()

6.散点图

6.1

import numpy as np

import matplotlib.pyplot as plt

x = np.arange(0., 5., 0.2)

plt.plot(x, x, 'r--', x, x**2, 'bs', x, x**3, 'g^')

plt.show()

6.2

import matplotlib.pyplot as plt
import numpy as np

n = 1024    # data size
# 正太分布
X = np.random.normal(0, 1, n)
Y = np.random.normal(0, 1, n)
T = np.arctan2(Y, X)    # 根据xy值设置颜色

plt.scatter(X, Y, s=75, c=T, alpha=.5)

plt.xlim(-1.5, 1.5)
plt.ylim(-1.5, 1.5)

plt.show()

7.柱状图

7.1

import matplotlib.pyplot as plt
import numpy as np

# 12个柱状图
n = 12
X = np.arange(n)  # x会生成0到11
Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)  # 随机随机生成0.5到1的数
Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)

plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')

# zip是把X,Y1中的值分别给x和y
# plt.text(x位置,y位置,值)
for x, y in zip(X, Y1):
    # ha:horizontal alignment对齐方式
    plt.text(x, y + 0.05, '%.2f' % y, ha='center', va='bottom')

for x, y in zip(X, Y2):
    # ha:horizontal alignment对齐方式
    plt.text(x, -y - 0.05, '-%.2f' % y, ha='center', va='top')

plt.xlim(-.5, n)
plt.xticks(())
plt.ylim(-1.25, 1.25)
plt.yticks(())
plt.show()

8直方图

8.1

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(1)
mu1, sigma1 = 100, 15
mu2, sigma2 = 80, 15
x1 = mu1 + sigma1 * np.random.randn(10000)
x2 = mu2 + sigma2 * np.random.randn(10000)
n1, bins1, patches1 = plt.hist(x1, 50, density=True, facecolor='g', alpha=1)
n2, bins2, patches2 = plt.hist(x2, 50, density=True, facecolor='r', alpha=0.2)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.xlabel('智商')
plt.ylabel('置信度')
plt.title('IQ直方图')
plt.text(110, .025, r'$mu=100, sigma=15$')
plt.text(50, .025, r'$mu=80, sigma=15$')
# 设置坐标范围
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()

9条形图

9.1平行条形图

import numpy as np
import matplotlib.pyplot as plt

size = 4
a = np.random.random(size)
b = np.random.random(size)
c = np.random.random(size)

x = np.arange(size)
total_width, n = 0.8, 3
width = total_width / n
# redraw the coordinates of x
x = x - (total_width - width) / 2
# here is the offset
plt.bar(x, a, width=width, label='a')
plt.bar(x + width, b, width=width, label='b')
plt.bar(x + 2 * width, c, width=width, label='c')

plt.legend()
plt.show()

9.2堆积条形图

import numpy as np
import matplotlib.pyplot as plt

size = 5
a = np.random.random(size)
b = np.random.random(size)
c = np.random.random(size)
x = np.arange(size)
plt.bar(x, a, width=0.5, label='a',fc='r')
plt.bar(x, b, bottom=a, width=0.5, label='b', fc='g')
plt.bar(x, c, bottom=a+b, width=0.5, label='c', fc='b')
plt.ylim(0, 2.5)
plt.legend()
# plt.grid(True)
plt.show()

10.饼状图

10.1一般饼状图

import matplotlib.pyplot as plt

labels = 'A', 'B', 'C', 'D'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0)
plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=False, startangle=90)
plt.axis('equal')
plt.show()

10.2嵌套饼状图

import numpy as np
import matplotlib.pyplot as plt

size = 0.3
vals = np.array([[60., 32.], [37., 40.], [29., 10.]])
cmap = plt.get_cmap("tab20c")
outer_colors = cmap(np.arange(3)*4)
inner_colors = cmap(np.array([1, 2, 5, 6, 9, 10]))
print(vals.sum(axis=1))
# [92. 77. 39.]
plt.pie(vals.sum(axis=1), radius=1, colors=outer_colors,
wedgeprops=dict(width=size, edgecolor='w'))
print(vals.flatten())
# [60. 32. 37. 40. 29. 10.]
plt.pie(vals.flatten(), radius=1-size, colors=inner_colors,
wedgeprops=dict(width=size, edgecolor='w'))
# equal makes it a perfect circle
plt.axis('equal')
plt.show()

10.3极轴饼状图

import numpy as np

import matplotlib.pyplot as plt

np.random.seed(19680801)
N = 10
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
radii = 10 * np.random.rand(N)

width = np.pi / 4 * np.random.rand(N)

ax = plt.subplot(111, projection='polar')

bars = ax.bar(theta, radii, width=width, bottom=0.0)

for r, bar in zip(radii, bars):
    bar.set_facecolor(plt.cm.viridis(r / 10.))
    bar.set_alpha(0.5)
plt.show()

11三维图

11.1三维散点图

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

data = np.random.randint(0, 255, size=[40, 40, 40])

x, y, z = data[0], data[1], data[2]

ax = plt.subplot(111, projection='3d')

ax.scatter(x[:10], y[:10], z[:10], c='y')

ax.scatter(x[10:20], y[10:20], z[10:20], c='r')

ax.scatter(x[30:40], y[30:40], z[30:40], c='g')

ax.set_zlabel('Z')

ax.set_ylabel('Y')

ax.set_xlabel('X')

plt.show()