definit_colors():return['blue','red','green','black','pink','purple','gray','yellow']defshow_graph(data, save_png_name=None, colors=init_colors()):"""
绘制折线图
:param data: 数据格式:{label:{X:Y}, label:{X:Y}...}
:param save_png_name:保存的图片的名字
:param colors: 颜色列表
:return:
None
"""# 解决中文显示乱码的问题,不用中文就不需要设置了
my_font = font_manager.FontProperties(fname="/自己补充路径/IOS8.ttf")
plt.figure(figsize=(14,6))
plts =[]
labels =[]for index, label inenumerate(data.keys()):if label is'rotate':continue
color = colors[index]
X = data.get(label).keys()
Y =[data.get(label).get(x)for x in X]
temp,= plt.plot(X, Y, color=color, label=label)
plts.append(temp)
labels.append(label)
plt.legend(handles=plts, labels=labels, prop=my_font)
plt.show()if save_png_name isnotNone:
plt.savefig(save_png_name)
散点图
import matplotlib.pyplot as plt
import numpy as np
n =1024# 从[0]
X = np.random.normal(0,1, n)
Y = np.random.normal(0,1, n)
T = np.arctan2(X, Y)
plt.scatter(np.arange(5), np.arange(5))
plt.xticks(())
plt.yticks(())
plt.show()
条形图
import matplotlib.pyplot as plt
import numpy as np
n =12
X = np.arange(n)
Y1 =(1- X/float(n))* np.random.uniform(0.5,1.0, n)
Y2 =(1- X/float(n))* np.random.uniform(0.5,1.0, n)
plt.figure(figsize=(12,8))
plt.bar(X,+Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X,-Y2, facecolor='#ff9999', edgecolor='white')for x, y inzip(X,Y1):# ha: horizontal alignment水平方向# va: vertical alignment垂直方向
plt.text(x, y+0.05,'%.2f'% y, ha='center', va='bottom')for x, y inzip(X,-Y2):# ha: horizontal alignment水平方向# va: vertical 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()
contour等高线图
import matplotlib.pyplot as plt
import numpy as np
defget_height(x, y):# the height functionreturn(1-x/2+x**5+y**3)*np.exp(-x**2-y**2)
n =256
x = np.linspace(-3,3, n)
y = np.linspace(-3,3, n)
X, Y = np.meshgrid(x, y)
plt.figure(figsize=(14,8))# use plt.contourf to filling contours# X, Y and value for (X, Y) point# 横坐标、纵坐标、高度、 、透明度、cmap是颜色对应表# 等高线的填充颜色
plt.contourf(X, Y, get_height(X, Y),16, alpah=0.7, cmap=plt.cm.hot)# use plt.contour to add contour lines# 这里是等高线的线
C = plt.contour(X, Y, get_height(X, Y),16, color='black', linewidth=.5)# adding label
plt.clabel(C, inline=True, fontsize=16)
plt.xticks(())
plt.yticks(())
plt.show()
3D数据图
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(12,8))
ax = Axes3D(fig)# 生成X,Y
X = np.arange(-4,4,0.25)
Y = np.arange(-4,4,0.25)
X,Y = np.meshgrid(X, Y)
R = np.sqrt(X**2+ Y**2)# height value
Z = np.sin(R)# 绘图# rstride(row)和cstride(column)表示的是行列的跨度
ax.plot_surface(X, Y, Z,
rstride=1,# 行的跨度
cstride=1,# 列的跨度
cmap=plt.get_cmap('rainbow')# 颜色映射样式设置)# offset 表示距离zdir的轴距离
ax.contourf(X, Y, Z, zdir='z', offest=-2, cmap='rainbow')
ax.set_zlim(-2,2)
plt.show()
Subplot多合一显示
import matplotlib.pyplot as plt
import numpy as np
plt.figure()# 将整个figure分成两行两列
plt.subplot(2,2,1)# 第一个参数表示X的范围,第二个是y的范围
plt.plot([0,1],[0,1])
plt.subplot(222)
plt.plot([0,1],[0,2])
plt.subplot(223)
plt.plot([0,1],[0,3])
plt.subplot(224)
plt.plot([0,1],[0,4])
plt.show()