21

In the code below, how do I create lines connecting each pair of scatter plots (i.e. linking the green circle to the yellow arrowhead) created by the two lines of code towards the end just before the .show() instruction?

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()
Zambi
  • 363
  • 1
  • 3
  • 9

1 Answers1

34

Draw a line segment between those points:

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

Produces:

enter image description here

Mark
  • 106,305
  • 20
  • 172
  • 230
  • Superb! Thanks, Mark. I never realized I can plot individual points at will (instead of a series of in an array or list). Now what if I wanted to draw a rectangle instead of a line? I tried changing "ax.plot" to "ax.bar" in your "for" loop but I got skewed lines instead. Is there a way to draw a rectangle? Thanks in advance. – Zambi May 14 '12 at 09:12
  • 3
    @Zambi Welcome to Stackoverflow! Rather than tack on extra questions, it makes more sense here to ask a _new_ question. – Hooked May 14 '12 at 13:38
  • Zambi, as @Hooked said, it's best to open a new question so the larger community can see it. A quick looks though, you have two options, draw the 4 sides of the rectangle with "plot" lines or look into using PolyCollection (http://matplotlib.sourceforge.net/examples/mplot3d/polys3d_demo.html) – Mark May 15 '12 at 00:42
  • 1
    Hooked and Mark thanks. A new question has been posted. (http://stackoverflow.com/questions/10599942/drawing-a-rectangle-or-bar-between-two-points-in-a-3d-scatter-plot-in-python-and) – Zambi May 15 '12 at 11:46