# PyPlot error, plot 3D line

**URL:** <https://discourse.julialang.org/t/pyplot-error-plot-3d-line/5668>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 1, 2017, 3:19am UTC](https://discourse.julialang.org/t/pyplot-error-plot-3d-line/5668 "2017-09-01T03:19:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [September 1, 2017, 3:19am UTC](https://discourse.julialang.org/t/pyplot-error-plot-3d-line/5668/1 "2017-09-01T03:19:05Z")

</div>

The error in the following code.  
ax.plot(xs, ys, zs) # no method found  
Could anyone give me some help? Thanks!

```julia
using PyPlot; const plt = PyPlot
xs = Float64[]
ys = Float64[]
zs = Float64[] 
# initialize xs, ys, zs
...

# show 3D line
fig = plt.figure()
ax = fig[:add_subplot](111, projection="3d")
ax.plot(xs, ys, zs) # error, "type PyObject has no field plot "

```

---

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [September 1, 2017, 4:09am UTC](https://discourse.julialang.org/t/pyplot-error-plot-3d-line/5668/2 "2017-09-01T04:09:24Z")

</div>

The aforementioned code is from the following python example. Can anyone convert the following code from Python to Julia? Thanks

```julia
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

mpl.rcParams['legend.fontsize'] = 10

fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()

plt.show()

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 1, 2017, 4:34am UTC](https://discourse.julialang.org/t/pyplot-error-plot-3d-line/5668/3 "2017-09-01T04:34:58Z")

</div>

Shouldn’t that be plot3d instead of plot?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 1, 2017, 4:36am UTC](https://discourse.julialang.org/t/pyplot-error-plot-3d-line/5668/4 "2017-09-01T04:36:01Z")

</div>

You need ax[:plot]

---

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [September 1, 2017, 4:50am UTC](https://discourse.julialang.org/t/pyplot-error-plot-3d-line/5668/5 "2017-09-01T04:50:19Z")

</div>

It is solved. The code is as following.  
can use keys( obj ) to show the attributes and member functions of the PyObject.

```julia
using PyPlot; const plt = PyPlot

theta = collect( linspace( -4π, 4π, 100 ) )
z = collect( linspace(-2, 2, 100) )
r = z.^2 + 1;
x = r .* sin(theta)
y = r .* cos(theta)

fig = plt.figure()
ax = fig[:add_subplot](111, projection="3d")    
ax[:plot](x, y, z) # PyObject, use obj[:function_name] to call the member function 

```

![image](https://global.discourse-cdn.com/julialang/original/3X/8/3/831779448e57225650185419fbfe4f4453e0a7c0.jpeg)

---

<div class="post-metadata">

**Author:** ![Marc.Cox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marc.cox/32/7514_2.png) [@Marc.Cox](https://discourse.julialang.org/u/Marc.Cox)\
**Post date:** [November 3, 2018, 9:05pm UTC](https://discourse.julialang.org/t/pyplot-error-plot-3d-line/5668/6 "2018-11-03T21:05:17Z")

</div>

```julia
**# Code updated to Julia version:=0.7.0 for Matplotlib/PyPlot.version:=2.1.2 running on Python version:=3.6.4**

**# Updated Julia code by Marc Cox / MSC**

**# file: 3d-Matplotlib-PyPlot-example.jl**

**using PyPlot**

# Marc Cox / MSC add logging some basic debugging info

println(&quot;## START Running Marc Cox 3d-Matplotlib-PyPlot-example.jl&quot;)

println(&quot;## Julia VERSION:=&quot;, VERSION)

println(&quot;## matplotlib. __version__ ():=&quot;, matplotlib[: __version__] )

println(&quot;## Matplotlib/PyPlot.version:=&quot;, PyPlot.version)

# Original code shows deprecation warnings

# which become hard stop errors in Julia version 1.0+

# Warning: `linspace(start, stop, length::Integer)` is deprecated, use `range(start, stop=stop, length=length)` instead.

# theta = collect(Float16, linspace( -4π, 4π, 100 ) )

# println(&quot;## typeof(theta) :=&quot;, typeof(theta))

# z = collect(Float16, linspace(-2, 2, 100) )

# println(&quot;## typeof(z) :=&quot;, typeof(z))

#

# range(start; length, stop, step=1)

theta = collect(Float16, range( -4π, length=100, stop=4π) )

println(&quot;## typeof(theta) :=&quot;, typeof(theta))

z = collect(Float16, range( -2, length=100, stop=2) )

println(&quot;## typeof(z) :=&quot;, typeof(z))

#

# MSC reconsider #man-dot-operators-1 ,

# Source: https://docs.julialang.org/en/v1/manual/mathematical-operations/index.html#man-dot-operators-1

# More specifically MUST UNDERSTAND

# Dot Syntax for Vectorizing Functions #man-vectorized-1

# Source: https://docs.julialang.org/en/v0.6.1/manual/functions/#man-vectorized-1

# Therefore, any Julia function f can be applied elementwise to any array (or other collection)

# with the syntax f.(A).

# For example sin can be applied to all elements in the vector A, like so:

# julia&gt; sin.(A)

# Warning: `a::AbstractArray + b::Number` is deprecated, use `a .+ b` instead.

r = z.^2 .+1;

println(&quot;## typeof(r), typeof(theta) :=&quot;, typeof(r), &quot;, &quot; ,typeof(theta))

# ORIGINAL WAS -- x = r .* sin(theta)

x = r.*sin.(theta)

# ORIGINAL WAS -- y = r .* cos(theta)

y = r.*cos.(theta)

# NOGO in Julia but Original OLD Python was -- fig = plt.figure()

# I feel like this should be more prominently explained in the documentation,

# but if you scroll down to the bottom of the Readme for PyCall (which PyPlot uses) it says:

# Important: The biggest difference from Python is that object attributes/members

# are accessed with o[:attribute] rather than o.attribute,

# so that o.method(...) in Python is replaced by o[:method](...)

fig = plt[:figure]()

#

# Source: https://github.com/JuliaPy/PyPlot.jl

# you must first call the using3D() function to ensure that mplot3d is loaded

using3D() # &lt;&lt; WORKS AOK per shows 3D axes below ...

ax = fig[:add_subplot](111, projection=&quot;3d&quot;)

ax[:plot](x, y, z) # PyObject, use obj[:function_name] to call the member function

**println(&quot;## FINISHED Running Marc Cox 3d-Matplotlib-PyPlot-example.jl&quot;)**

println(&quot; &quot;)

```

 ![Marc_Cox-3d-Matplotlib-PyPlot-example-jl](https://global.discourse-cdn.com/julialang/original/3X/3/7/37befa76a5fb62bb1bedae7eb4bdcd0c3a7cac87.png)
