Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a simple 3D line with Matplotlib?

I want to generate the lines, which I get from an array in 3D.

Here is the code:

VecStart_x = [0,1,3,5]
VecStart_y = [2,2,5,5]
VecStart_z = [0,1,1,5]
VecEnd_x = [1,2,-1,6]
VecEnd_y = [3,1,-2,7]
VecEnd_z  =[1,0,4,9]

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot([VecStart_x ,VecEnd_x],[VecStart_y,VecEnd_y],[VecStart_z,VecEnd_z])
plt.show()
Axes3D.plot()

I get that error:

ValueError: third arg must be a format string

like image 478
Daniel Zemljic Avatar asked Jul 18 '12 12:07

Daniel Zemljic


People also ask

How do I draw a 3d shape in Matplotlib?

Matplotlib was introduced for two-dimensional plotting. The 3d plot is enabled by importing the mplot3d toolkit., which comes with your standard Matplotlib. After importing, 3D plots can be created by passing the keyword projection=”3d” to any of the regular axes creation functions in Matplotlib.

Can Matplotlib do 3d?

In order to plot 3D figures use matplotlib, we need to import the mplot3d toolkit, which adds the simple 3D plotting capabilities to matplotlib. Once we imported the mplot3d toolkit, we could create 3D axes and add data to the axes. Let's first create a 3D axes.


2 Answers

I guess, you want to plot 4 lines. Then you can try

for i in range(4):
    ax.plot([VecStart_x[i], VecEnd_x[i]], [VecStart_y[i],VecEnd_y[i]],zs=[VecStart_z[i],VecEnd_z[i]])

As Nicolas has suggested, do have a look at the matplotlib gallery.

like image 80
imsc Avatar answered Sep 30 '22 11:09

imsc


The gallery is a great starting point to find out examples:

http://matplotlib.org/gallery.html

There is an example of 3d line plot here:

http://matplotlib.org/examples/mplot3d/lines3d_demo.html

You see that you need to pass to the ax.plot function 3 vectors. You are actually passing list of lists. I don't know what you mean by the Start and End sublist, but the following line should work :

ax.plot(VecStart_x + VecEnd_x, VecStart_y + VecEnd_y, VecStart_z +VecEnd_z)

Here I sum the sublist (concatenation) in order to have only one list by axis.

like image 21
Nicolas Barbey Avatar answered Sep 30 '22 11:09

Nicolas Barbey