0

I am trying to plot 3 different functions on a log-log scale in python for three intervals of the x-range. Attaching the image of the kind of plot that I want to create and which functions, y, for what intervals of x. enter image description here

My code attempt is as follows. Maybe I am overcomplicating it.

import math as m
import numpy as np
import pylab as pl 
import matplotlib.pyplot as plt
x = np.arange(100)  # this gives `array([0, 1, 2, ..., 9])`
y = np.arange(100)

for i in range (-50,20):
   if x[i] < -43:
      y[i] = m.log10((10**x[i])/(10**-43))**(1/2)
   if x[i] >  -43 and x[i] < -40:
      y[i] = m.log10(np.exp((10**36)((10**x[i])-(10**-43))))
   if x[i] >-40:
      y[i] = m.log10((np.exp((10**36)((10**-40) - (10**-43)))(((10**x[i])/(10**-43))**(1/2)))
   #i+=1


pl.plot(x,y)
#pl.xlim([-100.,100.])
#pl.ylim([-100.,100.]) 
pl.xlabel('log x')
pl.ylabel('log y')
pl.show()


PLEASE NOTE: updated code with help from @Sembei which works but there's further question on colours below:

import matplotlib.pyplot as plt
x = np.linspace(-50,23,500)
y = []

for xval in x:
    if xval < -36:
        y.append(m.log10(((10**xval)/(10**-36))**(1/2)))
    elif -36 <= xval <= -34:
        y.append(m.log10(np.exp((10**36)*((10**xval)-(10**-36)))))
    else:
        y.append(m.log10((np.exp((10**36)*((10**-34) - (10**-36)))*(((10**xval)/(10**-36))**(1/2)))))
       
plt.plot(x,y)
pl.xlim([-44.,-30.])
#pl.ylim([-10.,20.]) 
pl.xlabel('log x')
pl.ylabel('log y')
plt.show()

FURTHER QUESTION: how to set 3 different colours for the different y functions for the 3 x-intervals? Any help is appreciated. Thanks!

Jerome
  • 49
  • 8
  • Is your code working? If not, what is undesired? Your code does not seem overcomplicated, except that I suggest the use of `elif` and `else` – Giovanni Tardini May 03 '22 at 14:22
  • x has indexes from 0 to 99. you cannot do `x[-50]`. instead you should iterate over x and check if the logarithm of x is smaller than -36 `for xval in x: if log(xval) < -36: ..` Of course x cannot start at 0, for which log is undefined –  May 03 '22 at 14:22
  • @SembeiNorimaki that isn't correct, you can do ```x[-50]``` – Nin17 May 03 '22 at 14:26
  • sorry, i have edited the values. They are -43 and -40 etc for the intervals... – Jerome May 03 '22 at 14:26
  • @Nin17 Ok, yes, you can, but it will not do what the OP thinks it does. –  May 03 '22 at 14:32
  • @SembeiNorimaki, yes I will have to have a break at log x=0... there is actually a fourth curve I will need to draw after log(x)=10 which will be log of y where y=x^(2/3). – Jerome May 03 '22 at 14:34
  • just check my answer. don;t use logs at all, treat your values as linear and just add the label log to the axis. it will make you life way easier. –  May 03 '22 at 14:39

1 Answers1

2

you can do something like this:

x = range(-50,23)
y = []

for xval in x:
   if xval < -43:
       y.append(-43) #your function in this interval
   elif -43 <= xval <= -40:
       y.append(xval) #your function in this interval)
   else:
       y.append(-40) #your function in this interval)

plt.plot(x,y, '.-')
plt.xlabel('log x')
plt.ylabel('log y')
plt.show()

You just need to fill the #your function in this interval with a correct syntax (note that in your syntax you are missing product operators *)

Here I have used y as a list and I'm appending values. You can also initialize y to all zeros and assign values based on indexes. For that you will need to include an enumerate in the loop that will give you the index of y where you have to put the value.

Note: here, range steps one by one. if you want more resolution you might want to use np.linspace so you can control the resolution of your function.

Edit: I put some toy definitions of the function so you can see how it works. Now just change my function definitions for your own

enter image description here

  • Wanted to add that if the sole purpose of calculating the log of the functions is to display them in a log-log scale (which seems to be the case) you can use [`pl.loglog` which does that for you](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.loglog.html). – user2246849 May 03 '22 at 14:35
  • @sembei norimaki, thanks and I used your way but it gives error SyntaxError: invalid syntax for plt.plot(x,y). is it because of having log of 0? – Jerome May 03 '22 at 14:43
  • could you update your code and add the new code you are using? you should not use logs at all, since x is already in logarithmic values. –  May 03 '22 at 14:44
  • I updated. you mentioned missing * in syntax but where? – Jerome May 03 '22 at 14:46
  • 1
    thats because you are missing a closing parenthesis before the plt.plot. You will need to debug the small syntax errors you might have but if you use this code as a template it will work. Also in your definition of the functions you are still using `x[i]` and you shoul use `xval` instead –  May 03 '22 at 14:48
  • after fixing the parentheses, it shows TypeError: 'int' object is not callable pointing to the second y.append() line – Jerome May 03 '22 at 14:52
  • you need to fix the syntax problem yourself. I'll update my function with some simple function definitions and you can change them with your own definitions –  May 03 '22 at 14:56
  • @SembeiNorimaki sorry I did change to xval in the console, forgot to paste in the edit on the original post. Even after that the TypeError: 'int' object is not callable pointing to the second y.append() line stays – Jerome May 03 '22 at 14:56
  • because you are still missing product operators. you need to check that your function definitions are syntactically correct. –  May 03 '22 at 15:06
  • yes, found the missing * finally. Thanks! it still doesn't exactly look the way I expect it to..i'll have to play around with it.. – Jerome May 03 '22 at 15:09
  • @SembeiNorimaki, do you know how to plot the 3 different functions with different colours? – Jerome May 03 '22 at 15:44