matplotlib绘图系列----3D曲面图与散点图

使用mpl_toolkits.mplot3d下Axes3D绘制3D图形

Axes3D:可以在2D matplotlib图形上绘制3D对象

1.曲面图

plot_surface()–创建一个曲面图。参数:
这里写图片描述

我们先看参数:画三维图形需要三个坐标 xyz
rstride: Array row stride (step size) row步长
cstride: Array column stride (step size) column 步长
cmap:A colormap for the surface patches. 简单的说就是曲面图的配色

代码:

#-*- coding: utf-8 -*-

#加入中文显示
import  matplotlib.font_manager as fm
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D  #绘制三D图形

fig=plt.figure() #创建一张图片
ax3d=Axes3D(fig)
#三个轴  x y z
x=np.arange(-2,2.0,0.1)
y=np.arange(-2,2.0,0.1)
#z  随机生成
def f(x,y):
    return (1-y**5+x**5)*np.exp(-x**2-y**2)
x,y=np.meshgrid(x,y)
ax3d.plot_surface(x,y,f(x,y),rstride=1,cstride=1,cmap=plt.cm.spring)#cmap还可以是summer autumn winter也可以自己配置 
#rstride    Array row stride (step size)    步长
#cstride    Array column stride (step size)
plt.show()

这里写图片描述

2.散点图(scatter)

#-*- coding: utf-8 -*-

#加入中文显示
import  matplotlib.font_manager as fm
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

fig=plt.figure()

x1=np.random.randint(30,100,100)
y1=np.random.randint(30,100,100)
z1=np.random.randint(30,100,100)
x2=np.random.randint(30,100,100)
y2=np.random.randint(30,100,100)
z2=np.random.randint(30,100,100)
x3=np.random.randint(30,100,100)
y3=np.random.randint(30,100,100)
z3=np.random.randint(30,100,100)

ax3d=Axes3D(fig)    #绘制3D图形
ax3d.scatter(x1,y1,z1,c='r',marker=".")
ax3d.scatter(x2,y2,z2,c='b',marker="*")
ax3d.scatter(x3,y3,z3,c='r',marker='v')

#markes 标记的形状   http://matplotlib.org/api/markers_api.html

#设置轴坐标label 
ax3d.set_xlabel('武力')
ax3d.set_ylabel('智慧')
ax3d.set_zlabel('防御') 
#未解决matplotlib中文乱码的同学记得先解决乱码问题。 
plt.show()

中文乱码请参考我的另一篇文章”http://blog.csdn.net/ningyingqi/article/details/78802135

结果:

这里写图片描述

部分markes对应字符:

这里写图片描述

详细请查看api:http://matplotlib.org/api/markers_api.html

  • 7
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值