1

I'm very new to python just started using it from a day or two.. I'm using Anaconda python notebook. so I'm trying to plot, but in the output there is only grid and nothing no lines or anything, my program is as follows

from __future__ import print_function
from decimal import *
import numpy as np
from sympy import *
import pylab

k = Symbol('k')
A = Symbol('A')
E = Symbol('E')
d = Symbol('d')
C = Symbol('C')
Y = Symbol('Y')

Y = []

for A in np.arange(-1.11, 1.11, 0.002):        
    s = sin(A)
    c = cos(A)
    C = (s/A) + c
    Y.append(C)      

pylab.plot(C, A)   
grid()

xlabel('$x$')
ylabel('$y$')

title('graph')

The code doesn't show any errors, but will you please help me as to what am I doing wrong here ...

4
  • You replace the symbol Y with the array. Commented Nov 29, 2013 at 7:41
  • isn't putting Y = [] same as defining Y as array? Commented Nov 29, 2013 at 7:46
  • The line before you have defined Y as Symbol('Y') Commented Nov 29, 2013 at 7:50
  • oh!! ok, so I removed that line and directly kept Y = [], but still didn't work.. Commented Nov 29, 2013 at 7:53

3 Answers 3

2

You are mixing different plotting functions from pylab, sympy and you are not giving an X axis:

import numpy as np
from matplotlib import pyplot
Y=[]
X = np.arange(-1.11, 1.11, 0.002)
for A in X:
     s = np.sin(A)
     c = np.cos(A)
     C = (s/A)+c
     Y.append(C)
line, = pyplot.plot(X,Y, "-b")
pyplot.grid(True)
pyplot.show()

Gives me: enter image description here

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

6 Comments

Well, OP tried to give an x axis, but using A... which, after the loop, is not a list but the last element of its arange call.
The OP code and problems also nicely illiterates why ever doing from pppp import * is a bad idea, doing it more than once is a really bad idea!
Respected Steve, thank you so much, for your help.. I will try to keep this mistakes in check from next time.. really appreciate your help..
Steve, what do you mean by -- The OP code and problems also nicely illiterates why ever doing from pppp import * is a bad idea, doing it more than once is a really bad idea!
what does '-b' in 'line = pyplot.plot(X,Y, "-b") represent?
|
1

What about showing the graph with

 pylab.show()

2 Comments

well, after replacing pylab.plot(C, A) with pylab.show(C, A), I get the following error C:\Anaconda\lib\site-packages\matplotlib\pyplot.pyc in show(*args, **kw) 143 """ 144 global _show --> 145 _show(*args, **kw) 146 147 TypeError: show() takes at most 1 argument (2 given)
Oz123's answers only suggested that pylab.show() command is to be _added at the bottom of your code, as last line, not as replacement to pylab.plot(...).
0

If I do this, I have a figure with both grid and graph:

import pylab
pylab.plot([1, 3, 4], [1, 2, 3])
pylab.grid()
pylab.show()

But if I do this, I have first a figure with only the graph, and then with only the grid:

import pylab
pylab.plot([1, 3, 4], [1, 2, 3])
pylab.show()  # here I get only the graph
pylab.grid()
pylab.show()  # here I get only the grid

Note: calling grid(), title(), xlabel and ylabel as you do shall not work; each time it shall be prepended by pylab.. Is that really your code?

2 Comments

yeah, that is my own code that I wrote after going through some webpages and stuff.. thanks for your help, And I'm confused about writing the variable = Symbol('Variable') each time as I did in my program so how to get around that problem?
What were you trying to achieve with that? The reason that grid, etc, don't give an error is the from sympy import * so those bits use sympy and the pylab uses pylab....

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.