0

I am trying to get my function to plot a vectorized function over a python list interval and having a name/title for the graph. I am fairly new to python packages like numpy and matplotlib. I hope someone can shed some light on my code:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline    

def test_plot(x,interval,title):
    x = np.vectorize(x)
    interval = [0,interval+1]
    ttl = plt.title(title)
    plt.plot(x,interval)
    return plt.plot(x,interval)    

test_plot([-1,0,1,2,3,4],2,'test')

I get this error:

ValueError: x and y must have same first dimension

Note: I need the interval to be a list.

8
  • Have you read the documentation ? Commented Feb 3, 2016 at 3:10
  • Your x-coordinates (-1 through 4) have 6 elements in the set. Your y-coordinates only have two because of this line: interval = [0,interval] Now if you run that in the Python interpreter you get something like this: interval = 2 (defined in your input to the function), interval = [0,interval+1] (specified in your function), which leaves your y-coordinates with only 0 and 3 to work with (check the value of interval in your python interpreter to verify this). The poor code can then pair only the first two elements of the x and y coordinates. Commented Feb 3, 2016 at 3:13
  • Why are you vectorizeing a list? Commented Feb 3, 2016 at 3:13
  • @fjarri i'm trying to vectorize a function. Commented Feb 3, 2016 at 3:17
  • @fjarri good question. OP: I don't see the need for numpy in your code. You can save a bit of time by not importing it. Just think about why you want to put anything in your code before doing it. ;) Commented Feb 3, 2016 at 3:19

2 Answers 2

1

I don't quite understand what you are going to plot.

After checking the document , you can find :

numpy.vectorize requires a pyfunc to be initialized .

For example :

>>> def myfunc(a, b):
...     "Return a-b if a>b, otherwise return a+b"
...     if a > b:
...         return a - b
...     else:
...         return a + b

Then initiate your vectorize function

>>>vfunc = np.vectorize(myfunc)
>>>vfunc([1, 2, 3, 4], 2)
array([3, 4, 1, 2])

The parameter pyfunc must be callable . Passing a list seems meaningless .

As for matplotlib.pyplot.plot.

This function require x and y to have same dimension to be plotted .

But what your pass is a function and two elements list causing a ValueError

Sign up to request clarification or add additional context in comments.

Comments

0

Like KIDJournety, I don't quite understand what you're wanting either, but if I understand your comments correctly this might work:

def test_plot(x, interval, title): import matplotlib.pyplot as plt plt.title(title) plt.plot(x, interval) plt.show()

test_plot([1,2,3], [7,6,5], "hello, world!") works well. If this doesn't do what you need (what with the vectorisation) please comment below and I'll do my best to adjust the code accordingly.

Now for some practical reasons why your original code did not work: You were passing a list or vector with 6 elements (-1 through 4) as the x-values of the graph, but only 2 elements(0 and interval+1, which was 3 in your example) as your y-values. Your poor code could only pair (-1, 0) and (0,3). The rest of the x-coordinates had no corresponding y-coordinates to be paired to, so the code failed.

4 Comments

Well, I thank you for your effort. The question just states to define a function that takes a vectorized function and a python list interval of length 2 and have a title on it. I think what you gave is suitable. Sorry for the confusion !!
@misheekoh homework or self-studying? Sometimes we newbies get confused by stuff like this. :) Keep learning and it'll get easier! BTW: if this or KIDJourney's answer solved your trouble, would you be so kind as to accept the correct answer? Thanks!
Homework + studying. But I understand now. thank you. I can't accept @KIDJourney's answer. I will probably delete this bad question. thank you again
@misheekho: I can't tell you what to do with your own question, of course, but I wouldn't delete if I were you. It's valuable for anyone else who makes the same mistake. Trust me, we've all been there! :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.