ValueError: setting an array element with a sequence. when plotting R data

136 views
Skip to first unread message

Rolf

unread,
Jun 7, 2011, 4:57:09 AM6/7/11
to sage-support
There is a well known problem (http://ask.sagemath.org/question/192/
compiling-r-with-png-support) with plotting in R that apparently some
people successfully eliminated by installing the libraries mentioned
and recompiling. I didn’t succeed, so I'm trying a work around and
stumbled over an other problem.

If I use this little script:

def ndistribpts(x): return (x,r.dnorm(x,mean=100,sd=25))
v=map(ndistribpts,range(98,103))
v

[(98, [1] 0.01590671), (99, [1] 0.01594493), (100, [1] 0.01595769),
(101, [1] 0.01594493), (102, [1] 0.01590671)]
However this cannot be plotted.

A similar script:

def cube(x): return (x,x*x*x)
v=map(cube,range(98,103))
v

[(98, 941192), (99, 970299), (100, 1000000), (101, 1030301), (102,
1061208)]
That easily can be used by scatter_plot

I suppose it has something to do with the “[1] ” in front of each R
result. I have no idea how to eliminate that. The result cannot be
treated as a

string [3:]

or as matrix .GetItem(1)

Thank you for considering this.
R.

Rolf

unread,
Jun 7, 2011, 7:08:31 AM6/7/11
to sage-support
Problem solved!

The simple '._sage_()' does the trick. It converts data from R into
something that can be handled by Python or Sage.

def ndistribpts(x): return (x,r.dnorm(x,mean=100,sd=25)._sage_())
def cube(x): return (x,x*x*x)
v=map(ndistribpts,range(98,103))
scatter_plot(v)

Works though I'm still far from a pretty curve, but we are improving
rapidly.

Thank you.
R.

kcrisman

unread,
Jun 7, 2011, 8:19:46 AM6/7/11
to sage-support


On Jun 7, 7:08 am, Rolf <kamha...@googlemail.com> wrote:
> Problem solved!
>
> The simple '._sage_()' does the trick. It converts data from R into
> something that can be handled by Python or Sage.

Glad you found a solution!

> def ndistribpts(x): return (x,r.dnorm(x,mean=100,sd=25)._sage_())
> def cube(x): return (x,x*x*x)
> v=map(ndistribpts,range(98,103))
> scatter_plot(v)
>
> Works though I'm still far from a pretty curve, but we are improving
> rapidly.
>

Does this give you what you are looking for? I don't know that you
will get a pretty curve with only a few points, of course, so I
broadened the range.


sage: v=map(ndistribpts,range(50,151))
sage: line(v,ymin=0,ymax=.02)

The command 'points' might also give what you want, without connecting
the points.

- kcrisman

Rolf

unread,
Jun 8, 2011, 2:16:41 AM6/8/11
to sage-support
> sage: v=map(ndistribpts,range(50,151))
> sage: line(v,ymin=0,ymax=.02)
>
> The command 'points' might also give what you want, without connecting
> the points.

Thank you for this enhancement. The curve looks nice. However only in
this example the bell-shape can be appreciated.

It doesn't work here.

def ndistribpts(x): return (x,r.dnorm(x,mean=5,sd=0.5)._sage_())
v=map(ndistribpts,range(1,10))
line(v,ymin=0,ymax=1)

It needs more ajustments than just ymax as the integer steps provided
by the range function are too clumsy.
Thus I wonder why this doesn't work

def f(x): return r.dnorm(x,mean=100,sd=25)._sage_()
plot(f(x)(x,90,110))

while

def f(x): return x^3
plot(f(x),(x,0,2))

works perfectly. Is this a problem that can be solved?

D. S. McNeil

unread,
Jun 8, 2011, 5:22:03 AM6/8/11
to sage-s...@googlegroups.com
> Thus I wonder why this doesn't work
>
> def f(x): return r.dnorm(x,mean=100,sd=25)._sage_()
> plot(f(x)(x,90,110))
>
> while
>
> def f(x): return x^3
> plot(f(x),(x,0,2))
>
> works perfectly.

The problem is that the "f(x)" calls f with the symbolic argument "x"
_at the time you call the plot function_.

In the first case, r.dnorm doesn't know what to do with a symbolic x,
and barfs before it returns. OTOH, in the second function, the f(x)
call simply returns x^3, so it's equivalent to typing plot(x^3, (x, 0,
2).

Both cases should work if you use

plot(f, (x,90,110))
plot(f, (x,0,2))


Doug

Rolf

unread,
Jun 8, 2011, 6:04:11 AM6/8/11
to sage-support


> Both cases should work if you use
>
> plot(f, (x,90,110))
> plot(f, (x,0,2))
>
... and in fact they do.
I feel, this should be documented somewhere.
I used http://tutorial.sagenb.org/pub/4/
for reference.

Best regards
R.

Jason Grout

unread,
Jun 8, 2011, 8:07:19 AM6/8/11
to sage-s...@googlegroups.com
On 6/8/11 5:04 AM, Rolf wrote:
>
>
>> Both cases should work if you use
>>
>> plot(f, (x,90,110))
>> plot(f, (x,0,2))
>>
> ... and in fact they do.
> I feel, this should be documented somewhere.

Is item 4 of http://www.sagemath.org/doc/tutorial/tour_functions.html
sufficient? If not, that might be a place someone could clarify better.

Thanks,

Jason

kcrisman

unread,
Jun 8, 2011, 8:13:48 AM6/8/11
to sage-support


On Jun 8, 6:04 am, Rolf <kamha...@googlemail.com> wrote:
> > Both cases should work if you use
>
> > plot(f, (x,90,110))
> > plot(f, (x,0,2))
>
> ... and in fact they do.
> I feel, this should be documented somewhere.

The difference between symbolic callable functions and all other
function types (not to mention symbolic expressions that are not
functions) is a subtle one. Basically, the rule is that anything you
don't define using the syntax

f(x)=expression_with_x

should be called in plot with

plot(f,(x,90,110))

In particular, anything defined using 'def' will have to be done this
way. Or

plot(f,90,110)

or even

var('y')
plot(f,(y,90,110))

Reply all
Reply to author
Forward
0 new messages