【问题标题】:Connecting two points in a 3D scatter plot in Python and matplotlib在 Python 和 matplotlib 中连接 3D 散点图中的两个点
【发布时间】:2012-05-13 15:29:00
【问题描述】:

在下面的代码中,如何在 .show() 指令之前创建由两行代码创建的连接每对散点图的线(即将绿色圆圈连接到黄色箭头)?

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')

ax.scatter(dates, zaxisvalues0, highs, color = 'g', marker = "o")
ax.scatter(dates, zaxisvalues0, lows, color = 'y', marker = "^")

matplotlib.pyplot.show()

【问题讨论】:

    标签: python matplotlib mplot3d


    【解决方案1】:

    在这些点之间画一条线段:

    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()
    

    生产:

    【讨论】:

    • 太棒了!谢谢,马克。我从来没有意识到我可以随意绘制单个点(而不是数组或列表中的一系列点)。现在如果我想画一个矩形而不是一条线怎么办?我尝试在“for”循环中将“ax.plot”更改为“ax.bar”,但我得到了倾斜的线条。有没有办法画一个矩形?提前致谢。
    • @Zambi 欢迎来到 Stackoverflow!与其附加额外的问题,不如在这里提出一个新的问题。
    • 赞比,正如@Hooked 所说,最好打开一个新问题,以便更大的社区可以看到它。不过快速浏览一下,您有两个选择,用“绘图”线绘制矩形的 4 条边,或者使用 PolyCollection (matplotlib.sourceforge.net/examples/mplot3d/polys3d_demo.html)
    • 上钩和马克谢谢。发布了一个新问题。 (stackoverflow.com/questions/10599942/…)
    猜你喜欢
    • 2019-08-18
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    相关资源
    最近更新 更多