'plotting multiple functions in different intervals (python) in log-log axes

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!



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1