一、散点图
1、画几个散点一点都不难
将x,y放入plt.scatter()中就好了。
plt.scatter(np.arange(5),np.arange(5)) #绘制散点图
2、代码
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 23 19:20:54 2017
@author: ryoyun
"""
# Scatter 散点图
import matplotlib.pyplot as plt
import numpy as np
n = 1024
X = np.random.normal(0,1,n)
Y = np.random.normal(0,1,n)
#T = np.arctan2(Y,X) # for color value
#plt.scatter(X,Y,s=75,c=T,alpha= 0.5)
plt.scatter(np.arange(5),np.arange(5)) #绘制散点图
#plt.xlim((-1.5,1.5))
#plt.ylim((-1.5,1.5))
plt.xticks(())
plt.yticks(())
plt.show()
3、效果
二、柱状图
1、柱状图生成
plt.bar(X, +Y1,facecolor='#9999ff',edgecolor='white') # 生成柱状图
2、代码
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 23 19:28:19 2017
@author: ryoyun
"""
# bar 柱状图
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.bar(X, +Y1,facecolor='#9999ff',edgecolor='white') # 生成柱状图
plt.bar(X, -Y2,facecolor='#ff9999',edgecolor='white')
for x,y in zip(X,Y1):
# ha:horizontal alignment
plt.text(x+0.4,y+0.05,'%.2f'%y,ha ='center',va='bottom') # 数字标注
for x,y in zip(X,Y2):
# ha:horizontal alignment
plt.text(x+0.4,-y-0.05,'-%.2f'%y,ha ='center',va='top')
plt.xlim(-0.5,n)
plt.xticks(())
plt.ylim(-1.25,1.25)
plt.yticks(())
plt.show()
3、效果
三、等高线
1、三部曲
1.1轮廓填充
plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hot) # 填充轮廓
1.2画等高线
C = plt.contour(X,Y,f(X,Y),8,colors= 'black',linewidth = 0.5) #画等高线
1.3进行标注
plt.clabel(C,inline= True ,fontsize= 10) #增加标注
2、代码
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 23 19:44:25 2017
@author: ryoyun
"""
# Contours 等高线图
import matplotlib.pyplot as plt
import numpy as np
def f(x,y):
# the height function
return (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.contourf to filling contours
# X,Y and value for (X,Y) point
plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hot) # 填充轮廓
# use plt.counter to add counter lines
C = plt.contour(X,Y,f(X,Y),8,colors= 'black',linewidth = 0.5) #画等高线
# add label
plt.clabel(C,inline= True ,fontsize= 10) #增加标注
plt.xticks(())
plt.yticks(())
plt.show()
3、效果
四、图片
1、数字用图片来表示
plt.imshow(a,interpolation='nearest',cmap='bone',origin='lower')
a = 填充的数组
cmap='bone' #灰度色图
2、代码
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 23 19:57:52 2017
@author: ryoyun
"""
# image 图片
import matplotlib.pyplot as plt
import numpy as np
n = 9
#a = [[0 1 2]
# [3 4 5]
# [6 7 8]]
a = np.array(np.arange(n)).reshape(3,3)# 生成3个数组,每个数组个元素
plt.imshow(a,interpolation='nearest',cmap='bone',origin='lower')
plt.colorbar()# 颜色标注
plt.xticks(())
plt.yticks(())
plt.show()
3、效果
五、3D视图
1、更酷炫的3D图
1.1导入3d包
from mpl_toolkits.mplot3d import Axes3D
1.2增加3D的figure
ax = Axes3D(fig)
1.3 开始画3D图
ax.plot_surface(X,Y,Z,rstride= 1,cstride= 1,cmap=plt.get_cmap('rainbow'))
1.4 增加投影
ax.contourf(X,Y,Z,zdir='z',offset = -2,cmap='rainbow')
2、代码
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 23 20:09:50 2017
@author: ryoyun
"""
# 3D
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
# X,Y value
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) # z的值
# rstride,cstride跨度
# cmap=plt.get_cmap('rainbow') 颜色设置
ax.plot_surface(X,Y,Z,rstride= 1,cstride= 1,cmap=plt.get_cmap('rainbow'))
ax.contourf(X,Y,Z,zdir='z',offset = -2,cmap='rainbow')
ax.set_zlim(-2,2)
plt.show()
3、效果
参考:莫烦python
https://morvanzhou.github.io/tutorials/
来源:CSDN
作者:若云流风
链接:https://blog.csdn.net/u013584315/article/details/78076167