matplotliba画有意思的3D图,主3D加xyz轴方向投影

44 篇文章 0 订阅
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np


def draw3Dcontour(func, s= "x^2 + y^3"):
    x = np.arange(-2*np.pi, 2*np.pi, 0.5)
    y = np.arange(-2*np.pi, 2*np.pi, 0.5)
    X, Y = np.meshgrid(x,y)


    Z = func(X,Y)#X ** 2 + Y ** 3

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot_surface(X,Y,Z, rstride=8, cstride=8, alpha = 0.3)

    cset = ax.contour(X,Y,Z, zdir='z', offset= np.min(np.array(Z)), cmap = cm.coolwarm)
    cset = ax.contour(X,Y,Z, zdir = 'x', offset = -6, cmap = cm.coolwarm)
    cset = ax.contour(X,Y,Z, zdir = 'y', offset = 6, cmap = cm.coolwarm)

    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    ax.set_title(s)
    # plt.show()

draw3Dcontour(lambda X,Y : X**2 + Y**2, "x^2+y^2")
## 1 : "x^2 + y^3"
draw3Dcontour(lambda X,Y : X**2 + Y**3, "x^2+y^3")

draw3Dcontour(lambda X,Y : np.sin(X) + np.cos(Y), "sinx+cosy") # sin(x), cos(y)

draw3Dcontour(lambda X,Y : np.abs(X) @ np.sin(X) + np.cos(Y), "sinx+cosy") # sin(x), cos(y)

plt.show()

原版样例

'''
======================================
Projecting filled contour onto a graph
======================================

Demonstrates displaying a 3D surface while also projecting filled contour
'profiles' onto the 'walls' of the graph.

See contour3d_demo2 for the unfilled version.
'''

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm

fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)

# Plot the 3D surface
ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)

# Plot projections of the contours for each dimension.  By choosing offsets
# that match the appropriate axes limits, the projected contours will sit on
# the 'walls' of the graph
cset = ax.contourf(X, Y, Z, zdir='z', cmap=cm.coolwarm) # offset=-100,
cset = ax.contourf(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
cset = ax.contourf(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)

ax.set_xlim(-40, 40)
ax.set_ylim(-40, 40)
ax.set_zlim(-100, 100)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

样例是一个峰-谷图,有点高端的,我改了一些,方便常见的图形暂时;具体图形因为程序问题,不上传了,感兴趣读者自己run一下。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值