matplotlib库
pyplot:
matplotlib.pyplot是绘制各类可视化图形的命令子库
引用方式:import matplotlib.pyplot as plt
plot(x, y, [fmt], x2, y2, [fmt2], …, **kwargs) # x,y可以存放元组和数组类型
plt.ylabel() # y轴标签
plt.xlabel() # x轴标签
plt.subplot(x, y, c) # 绘制一个人横x,纵y的区域,在第c个区域绘制
plt.savefig(name, dpi) #保存为PNG格式
plt.show() # 展示
plt.plot()是一个灵活的函数,当仅给其传一个元组类型参数时,他会自动补充y轴坐标
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.show()
plt.plot()参数:当参数存放多组,则代表多条曲线,也可以修改他的颜色、标记点、线型等
例:
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0., 5., 0.2)
# 红色的破折号,蓝色的方块,绿色的三角形
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()
[ftm] = ‘[color][marker][line]’
color:
============= ===============================
character color
============= ===============================
``'b'`` blue 蓝
``'g'`` green 绿
``'r'`` red 红
``'c'`` cyan 蓝绿
``'m'`` magenta 洋红
``'y'`` yellow 黄
``'k'`` black 黑
``'w'`` white 白
marker:
============= ===============================
character description
============= ===============================
``'.'`` point marker
``','`` pixel marker
``'o'`` circle marker
``'v'`` triangle_down marker
``'^'`` triangle_up marker
``'<'`` triangle_left marker
``'>'`` triangle_right marker
``'1'`` tri_down marker
``'2'`` tri_up marker
``'3'`` tri_left marker
``'4'`` tri_right marker
``'s'`` square marker
``'p'`` pentagon marker
``'*'`` star marker
``'h'`` hexagon1 marker
``'H'`` hexagon2 marker
``'+'`` plus marker
``'x'`` x marker
``'D'`` diamond marker
``'d'`` thin_diamond marker
``'|'`` vline marker
``'_'`` hline marker
============= ===============================
line:
============= ===============================
character description
============= ===============================
``'-'`` solid line style 实线
``'--'`` dashed line style 虚线
``'-.'`` dash-dot line style 点画线
``':'`` dotted line style 点线
============= ===============================
import numpy as np
import matplotlib.pyplot as plt
def f(t):# 能量衰减曲线
return np.exp(-t)*np.cos(2*np.pi*t) # exp(-t):以e为底-t的对数
a = np.arange(0.0,5.0,0.02) # numpy.arange(start, stop, step, dtype = None) 0~5,步长为0.2的数组
plt.subplot(211)# 绘制区域 横2 纵1 选择1,==plt.subplot(2, 1, 1)
plt.plot(a,f(a))
plt.subplot(2,1,2)
plt.plot(a,np.cos(2*np.pi*a),'r--') # 'r--':以虚线形式展示
plt.show()
pyplot虽然是matplotlib库的其中一个,但是内置了大量的图形:
饼状图
使用 plt.pie() 绘制
import matplotlib.pyplot as plt
lables = 'Frogs','Hogs','Dogs','Logs'# 标签
sizes = [15, 30, 45, 10]# 占比(列表类型)
explode = (0, 0.1, 0, 0)# 突出(元组类型)
plt.pie(sizes, explode = explode, labels = lables, autopct = '%1.1f%%',
shadow = False, startangle = 90)
plt.axis('equal')
plt.show()
直方图
使用 plt.hist() 绘制
matplotlib.pyplot.hist(x, bins=None, range=None, normed=False,
weights=None, cumulative=False, bottom=None, histtype='bar', align='mid',
orientation='vertical', rwidth=None, log=False, color=None, label=None,
stacked=False, hold=None, data=None, **kwargs)
这么多参数,我们只需要关注其中几个
示例:
import numpy as np
import matplotlib.pyplot as plt
mu,sigma = 100,20
a = np.random.normal(mu, sigma,size = 100)# 正态分布
plt.hist(a, 10, histtype = 'stepfilled',facecolor = 'b', alpha = 0.5)
# data bins 类型 颜色 透明度
plt.title('Histogram')# 标题
plt.show()
极坐标图
使用 plt.polar 绘制
import numpy as np
import matplotlib.pyplot as plt
N = 20
theta = np.linspace(0.0,2*np.pi, N, endpoint=False) #0 ~ 2派(不包含2派) 内成N个随机数
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()
散点图
散点图只需要使用plot函数即可
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(10*np.random.randn(100), 10*np.random.randn(100), 'o')
ax.set_title('Simple Scatter')
plt.show()