PyPlot: LineCollection help needed

490 views
Skip to first unread message

NotSoRecentConvert

unread,
Jun 10, 2016, 10:29:25 AM6/10/16
to julia-users
I'm trying to do a plot using LineCollection in PyPlot however am having a hard time converting an example (1 or 2).

lines = Any[Any[[1.0 2.0]];Any[[3.0 4.0]];Any[[5.0 .06]];Any[[0.0 0.0]]] # Points
c
= Any[Any[1 0 0];Any[0 1 0];Any[0 0 1]] # Color

line_segments
= matplotlib[:collections][:LineCollection](lines,colors=c)

fig
= figure("Line Collection Example")
ax
= axes()
ax
[:add_collection](line_segments)
axis
("tight")

It doesn't return an errors but I don't see any lines. Any idea what could be wrong?

David P. Sanders

unread,
Jun 10, 2016, 9:38:54 PM6/10/16
to julia-users
Does it work with ax=gca() instead?

Christoph Ortner

unread,
Jun 11, 2016, 8:34:35 AM6/11/16
to julia-users
seems not.

David P. Sanders

unread,
Jun 11, 2016, 10:20:52 AM6/11/16
to julia-users
After looking at some matplotlib examples, it seems that the data structure needed by LineCollection in Python
is a list of lists, with the inner lists being lists of (x,y) pairs. 

After playing around for a while, the following works for me
for drawing a pair of lines in Julia:
xs = [1., 3., 5., 0.]
ys = [2., 4., .06, 0.]
lines = Any[collect(zip(xs, ys))]

xs = [3., 4]
ys = [5., 6]
push!(lines, collect(zip(xs, ys)))
#lines = Vector{Float64}[[1.0, 2.0], [3.0 4.0], 5.0 .06]];Any[[0.0 0.0]]] # Points
#c = Any[Any[1 0 0];Any[0 1 0];Any[0 0 1]] # Color
c = Vector{Int}[[1,0,0], [0,1,0], [0,0,1]]

line_segments = matplotlib[:collections][:LineCollection](lines, colors=c)

fig = figure("Line Collection Example")
ax = gca()
ax[:add_collection](line_segments)
axis("image")

Christoph Ortner

unread,
Jun 11, 2016, 4:13:06 PM6/11/16
to julia-users
thanks, David, this will be useful for me as well! C

Tom Breloff

unread,
Jun 14, 2016, 3:41:29 PM6/14/16
to julia-users
David: Thanks so much for saving me a ton of headache figuring this out for Plots.  Just pushed to dev:

```
using Plots
y = cumsum(randn(500))
plot(y, line_z = y, w = 3)
```




David P. Sanders

unread,
Jun 14, 2016, 11:06:38 PM6/14/16
to julia-users
Great stuff, Tom!
Reply all
Reply to author
Forward
0 new messages