Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

python - Matplotlib 3D Indicate Points on Axis

I'm drawing a 3D plot as shown here.

I'd like to indicate some points on the axes like this (I drew the dashed lines using PowerPoint)

My code is attached below. I found some related posts (like this one Matplotlib Indicate Point on X and Y Axis) but they don't seem to work with my scenario. Any help would be much appreciated!

import sys
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from scipy import interpolate

import numpy
from numpy.random import randn
from scipy import array, newaxis

# ======
## data

DATA = array([
    [0.5, 5, 33.9114],
    [1.0, 5, 33.3244],
    [1.5, 5, 31.7503],
    [2.0, 5, 29.2423],
    [2.5, 5, 33.1644],
    [3.0, 5, 31.4835],
    [10.0, 5, 42.1025],
    [20.0, 5, 54.8292],
    [30.0, 5, 62.1131],
    [0.5, 10, 12.7001],
    [1.0, 10, 14.0875],
    [1.5, 10, 16.9157],
    [2.0, 10, 18.7033],
    [2.5, 10, 11.4194],
    [3.0, 10, 21.7449],
    [10.0, 10, 32.3106],
    [20.0, 10, 40.3949],
    [30.0, 10, 52.7481],
    [0.5, 15, 3.3885],
    [1.0, 15, 6.0832],
    [1.5, 15, 6.6169],
    [2.0, 15, 9.2583],
    [2.5, 15, 8.5112],
    [3.0, 15, 9.8453],
    [10.0, 15, 20.2241],
    [20.0, 15, 36.6862],
    [30.0, 15, 45.7577],
    [0.5, 20, 1.4941],
    [1.0, 20, 2.6681],
    [1.5, 20, 3.6019],
    [2.0, 20, 5.0427],
    [2.5, 20, 4.6958],
    [3.0, 20, 6.1099],
    [10.0, 20, 17.3159],
    [20.0, 20, 29.7492],
    [30.0, 20, 39.6745],
    [0.5, 25, 0.7471],
    [1.0, 25, 1.5208],
    [1.5, 25, 2.3916],
    [2.0, 25, 3.5219],
    [2.5, 25, 4.4557],
    [3.0, 25, 5.6297],
    [10.0, 25, 15.635],
    [20.0, 25, 26.0672],
    [30.0, 25, 36.5261],
    [0.5, 30, 0.667],
    [1.0, 30, 1.5742],
    [1.5, 30, 2.1612],
    [2.0, 30, 3.175],
    [2.5, 30, 4.0288],
    [3.0, 30, 4.1889],
    [10.0, 30, 13.2337],
    [20.0, 30, 25.2401],
    [30.0, 30, 35.4055],
    [0.5, 35, 0.9338],
    [1.0, 35, 1.5475],
    [1.5, 35, 2.0277],
    [2.0, 35, 2.9616],
    [2.5, 35, 3.7086],
    [3.0, 35, 4.429],
    [10.0, 35, 12.7535],
    [20.0, 35, 23.1857],
    [30.0, 35, 34.1782],
    [0.5, 40, 0.8805],
    [1.0, 40, 1.4941],
    [1.5, 40, 1.9477],
    [2.0, 40, 2.7215],
    [2.5, 40, 3.4152],
    [3.0, 40, 3.8687],
    [10.0, 40, 11.8196],
    [20.0, 40, 23.3725],
    [30.0, 40, 33.3244],
    [0.5, 45, 0.667],
    [1.0, 45, 1.334],
    [1.5, 45, 1.7876],
    [2.0, 45, 2.4813],
    [2.5, 45, 3.175],
    [3.0, 45, 3.6553],
    [10.0, 45, 12.1932],
    [20.0, 45, 22.1451],
    [30.0, 45, 32.1772],
    [0.5, 50, 0.667],
    [1.0, 50, 1.2273],
    [1.5, 50, 1.8143],
    [2.0, 50, 2.3746],
    [2.5, 50, 3.0149],
    [3.0, 50, 3.762],
    [10.0, 50, 11.873],
    [20.0, 50, 22.1718],
    [30.0, 50, 31.4835],
    [0.5, 55, 0.6137],
    [1.0, 55, 1.174],
    [1.5, 55, 1.7609],
    [2.0, 55, 2.4013],
    [2.5, 55, 2.8549],
    [3.0, 55, 3.4152],
    [10.0, 55, 11.4728],
    [20.0, 55, 21.6649],
    [30.0, 55, 31.0566],
    [0.5, 60, 0.5603],
    [1.0, 60, 1.1206],
    [1.5, 60, 1.7076],
    [2.0, 60, 2.2946],
    [2.5, 60, 2.7481],
    [3.0, 60, 3.3351],
    [10.0, 60, 11.0459],
    [20.0, 60, 21.0512],
    [30.0, 60, 30.5496]
])

Xs = DATA[:,0]
Ys = DATA[:,1]
Zs = DATA[:,2]

# ======
## plot:

x = numpy.reshape(Xs, (9, 12))
y = numpy.reshape(Ys, (9, 12))
z = numpy.reshape(Zs, (9, 12))

xnew, ynew = numpy.mgrid[5:30:80j, 5:60:80j]
tck = interpolate.bisplrep(x, y, z, s=30)
znew = interpolate.bisplev(xnew[:,0], ynew[0,:], tck)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

surf = ax.plot_surface(xnew,ynew,znew, linewidth=10, cmap=viridis, rstride=1, cstride=1)

ax.set_xlabel('Initial Infected (%)')
ax.set_ylabel('Quarantined (%)')
ax.set_zlabel('Total Infected (%)')

ax.grid(True)
fig.colorbar(surf, shrink=0.5, aspect=10)

fig.tight_layout()

ax.elev = 16
ax.azim = 145
plt.show()
fig.savefig('3dfig0204.pdf')
question from:https://stackoverflow.com/questions/66049658/matplotlib-3d-indicate-points-on-axis

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...