1

I want to plot an equation where the x axis represents time t and the y axis represents a variable n. Now the formula for n is n =((np.log(t)*10**6)/np.log(2)) + 1 and the maximum time on my graph is the time since big bang theory in seconds which is 4.35 x 10**17 seconds.

I tried this:

import matplotlib.pyplot as plt
import numpy as np

def graph(formula, x_range):  
    y = np.array(x_range)  
    x = eval(formula)
    plt.plot(x,y)  
    plt.show() 

graph(((np.log(x)*10**6)/np.log(2)) + 1, range(0, 4.35*10**17 ))

which doesn't seem to work. Any ideas on how I can achieve this?

2
  • eval works on a string. Nevertheless it is not seen as very elegant anyway. Commented Feb 1, 2017 at 21:39
  • Time since big bang theory is maximum 90000 seconds nowadays. Time since big bang may be considerably larger. Commented Feb 1, 2017 at 21:43

1 Answer 1

3

There are several problems here:

  • The range is supposed to be defined over integers;
  • The number of items the range would generate is too large, you have to take huge steps;
  • The formula takes x as a variable, but you seem to define a np.array(x_range) in y; and more importantly
  • you use eval(..), but eval(..) usually a string or another object that can be parsed;
  • you do not give graph(..) a formula as first element: before Python calls graph(..) it first evaluates the operands.

In my opinion, the best way to achieve this is using a lambda-expression:

import matplotlib.pyplot as plt
import numpy as np

def graph(formula, x_range):  
    x = np.array(x_range)
    #^ use x as range variable
    y = formula(x)
    #^          ^call the lambda expression with x
    #| use y as function result
    plt.plot(x,y)  
    plt.show() 

graph(lambda x : ((np.log(x)*10**6)/np.log(2)) + 1, range(0, 435*10**15,10**12))
#     ^use a lambda expression                               ^range over integers
#                                                             take huge steps

This generates the following image:

enter image description here

EDIT:

based on your comment you want time on the y-axis and the function on the x-axis, this can simply be achieved by assigning to the other variables like:

import matplotlib.pyplot as plt
import numpy as np

def graph(formula, x_range):  
    y = np.array(x_range)
    x = formula(y)
    plt.plot(x,y)  
    plt.show() 

graph(lambda x : ((np.log(x)*10**6)/np.log(2)) + 1, range(0,435*10**15,10**12))

Note that you do not need to change the name of the variable in the lambda-expression: indeed when you call the lambda expression, the local x will simply be the y of the caller.

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

2 Comments

I realise that maybe time t being on the y axis makes more sense. How would you flip it such that time is on y axis and n is on the x axis.
@WasswaSamuel: simply rename x, y and y x in the assignment in the function. Will edit the answer.

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.