[英]Connecting two points in a 3D scatter plot in Python and matplotlib
[英]Drawing a rectangle or bar between two points in a 3D scatter plot in Python and matplotlib
我有一个三维散点图,在其中一个平面上,每个日期绘制2个点。 我问过如何在每对点之间绘制一条LINE ,并得到了一个我很感激的答案。 我现在想要的是画一个BAR或RECTANGLE连接点而不是一条线。
这是目前情节的样子,但我希望它看起来有点像来自matplolib文档的3D条形演示中的情节,除了条形“浮动”而不是固定在轴上。
我已经尝试过使用Axes3D.bar
(如matplotlib页面所述),但它希望我为每个条形提供“高度”而不是两个实际坐标,并且该高度将锚定到轴。
这是代码,任何帮助表示赞赏。
import matplotlib.pyplot
from mpl_toolkits.mplot3d import Axes3D
dates = [20020514, 20020515, 20020516, 20020517, 20020520]
highs = [1135, 1158, 1152, 1158, 1163]
lows = [1257, 1253, 1259, 1264, 1252]
upperLimits = [1125.0, 1125.0, 1093.75, 1125.0, 1125.0]
lowerLimits = [1250.0, 1250.0, 1156.25, 1250.0, 1250.0]
zaxisvalues0= [0, 0, 0, 0, 0]
zaxisvalues1= [1, 1, 1, 1, 1]
zaxisvalues2= [2, 2, 2, 2, 2]
fig = matplotlib.pyplot.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.plot(dates, zaxisvalues1, lowerLimits, color = 'b')
ax.plot(dates, zaxisvalues2, upperLimits, color = 'r')
for i,j,k,h in zip(dates,zaxisvalues0,lows,highs):
ax.plot([i,i],[j,j],[k,h],color = 'g')
ax.scatter(dates, zaxisvalues0, highs, color = 'g', marker = "o")
ax.scatter(dates, zaxisvalues0, lows, color = 'y', marker = "^")
matplotlib.pyplot.show()
我认为使用PolyCollection会更容易。 这跟你所追求的很接近吗?
import matplotlib.pyplot
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
import random
dates = [20020514, 20020515, 20020516, 20020517, 20020520]
highs = [1135, 1158, 1152, 1158, 1163]
lows = [1257, 1253, 1259, 1264, 1252]
upperLimits = [1125.0, 1125.0, 1093.75, 1125.0, 1125.0]
lowerLimits = [1250.0, 1250.0, 1156.25, 1250.0, 1250.0]
zaxisvalues0= [0, 0, 0, 0, 0]
zaxisvalues1= [1, 1, 1, 1, 1]
zaxisvalues2= [2, 2, 2, 2, 2]
fig = matplotlib.pyplot.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.plot(dates, zaxisvalues1, lowerLimits, color = 'b')
ax.plot(dates, zaxisvalues2, upperLimits, color = 'r')
verts = []; fcs = []
for i in range(len(dates)-1):
xs = [dates[i],dates[i+1],dates[i+1],dates[i],dates[i]] # each box has 4 vertices, give it 5 to close it, these are the x coordinates
ys = [highs[i],highs[i+1],lows[i+1],lows[i], highs[i]] # each box has 4 vertices, give it 5 to close it, these are the y coordinates
verts.append(zip(xs,ys))
fcs.append((random.random(),random.random(),random.random(),0.6))
poly = PolyCollection(verts, facecolors = fcs, closed = False)
ax.add_collection3d(poly, zs=[zaxisvalues0[0]] * len(verts), zdir='y') # in the "z" just use the same coordinate
ax.scatter(dates, zaxisvalues0, highs, color = 'g', marker = "o")
ax.scatter(dates, zaxisvalues0, lows, color = 'y', marker = "^")
matplotlib.pyplot.show()
感谢Alexis和Mark的帮助。 我认为现在已经整理好了。
我使用了Alexis的提示来使用'zdir'属性。
至于错误的平面问题,你可以用参数zdir修复它,例如ax.bar(日期,高点,zdir ='y',底部=高点,zs = 0,颜色='b') - Alexis
起初它产生的柱子应该是它们应该的两倍,因为它是从底部测量的(即“低”值),然后向它添加一个高度(从“高”值开始)。
所以我最后引入了一个新的列表,'displacements',它测量每个高点和每个低点之间的距离(并且在此过程中发现我有低点和高点交换。对,对不起)。 所以现在我正在策划'位移'而不是高位。
我向Alexis的线条添加了宽度,对齐,边缘颜色和alpha(透明度); 然后加厚ax.scatter图的标记。 现在代码工作了(好吧,差不多,除了第4个栏上的那个箭头高于它应该是......嗯)
import matplotlib.pyplot
from mpl_toolkits.mplot3d import Axes3D
dates = [20020514, 20020515, 20020516, 20020517, 20020520]
lows = [1135, 1158, 1152, 1158, 1163]
highs = [1257, 1253, 1259, 1264, 1252]
upperLimits = [1125.0, 1125.0, 1093.75, 1125.0, 1125.0]
lowerLimits = [1250.0, 1250.0, 1156.25, 1250.0, 1250.0]
zaxisvalues0= [0, 0, 0, 0, 0]
zaxisvalues1= [1, 1, 1, 1, 1]
zaxisvalues2= [2, 2, 2, 2, 2]
fig = matplotlib.pyplot.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.plot(dates, zaxisvalues1, lowerLimits, color = 'b')
ax.plot(dates, zaxisvalues2, upperLimits, color = 'r')
ax.scatter(dates, zaxisvalues0, highs, color = 'g', marker = "^", linewidth=4)
ax.scatter(dates, zaxisvalues0, lows, color = 'y', marker = "o", linewidth=4)
displacements = []
for i in lows:
position = lows.index(i)
disp = highs[position] - i
displacements.append(disp)
ax.bar(dates, displacements, zdir='y', bottom=lows, zs=0, width=0.2, align='center', alpha=0.6, edgecolor='k')
matplotlib.pyplot.show()
这是结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.