Python Forum

Full Version: legend/color
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The following code is running as it should, however, when it comes to plot the graph,
the command cm.rainbow is printing the same color, and I'd like the legend to have all
values of the iteration according to the range, but I cannot manage to obtain this outcome,
instead it prints exactly whatever I write in the curly brackets.
I am not sure how should I correct my code.

def applog(n,x):
    a0=(1+x)/2
    b0=sqrt(x)
   
    for i in range(n):
        a0=(a0+b0)/2
        b0=sqrt((a0)*b0)
      
        
    return (x-1)/a0

print("the log approximation is: " , ((applog(4,4)))) 
print("log value-------------------:" , (np.log(4)) )
                   
error=(abs( applog(4,4)- (np.log(4))   ) )                    
print("the error is : ", error)
  
x = np.linspace(10, 500, 100)
nn=range(1,6)
colors = mpl.cm.rainbow(np.linspace(0, 1, len(nn)))


for c,n in zip(colors,nn):
    plt.plot(x, (applog(n,x)),color='c', label='${n}$') 
plt.plot(x, np.log(x), color='red', label='ln(x)') 
plt.legend(loc='upper left')
plt.show()          
plt.plot(x,abs(np.log(x)-applog(n,x)) )
plt.show()         
don't assume that we know what your imports are,
I am assuming np is numpy, mpl is matplotlib, sqrt from math,
but don't know what mpl.cm is.
here is everything I Imported.
Does it help?

from scipy import *
import numpy as np
from numpy import array
from scipy import integrate
import matplotlib.pyplot as plt
import scipy.integrate as si
from scipy.optimize import fsolve
from math import log
import matplotlib as mpl
hi,

I am still trying to figure out how to solve this issue.
Anyone?
looking at the value for c and n for line 24, range is:
c: [0.5 0. 1. 1. ], n:1
c: [0.00196078 0.70928131 0.92328911 1. ], n:2
c: [0.50392157 0.99998103 0.70492555 1. ], n:3
c: [1. 0.70054304 0.37841105 1. ], n:4
c: [1.0000000e+00 1.2246468e-16 6.1232340e-17 1.0000000e+00], n:5
Are these supposed to be R G B values? If so, aren't they supposed to be integers?
yes, they are integers.
I managed to solve the color issue.
Now I would like the legend to have n=1,n=2.... until n=5,
but with my new code, the legend shows the whole range for each value of n.
I am not sure how to fix this.

def applog(n,x):
    a0=(1+x)/2
    b0=sqrt(x)
   
    for i in range(n):
        a0=(a0+b0)/2
        b0=sqrt((a0)*b0)
      
        
    return (x-1)/a0

print("the log approximation is: " , ((applog(4,4)))) 
print("log value-------------------:" , (np.log(4)) )
                   
error=(abs( applog(4,4)- (np.log(4))   ) )                    
print("the error is : ", error)
  
x = np.linspace(10, 500, 100)
nn=range(1,6)
colors = mpl.cm.rainbow(np.linspace(0, 1, len(nn)))


for c,n in zip(colors,nn):
    plt.plot(x, (applog(n,x)), label='$n = {nn}$'.format(nn=list(nn)  )) 
plt.plot(x, np.log(x), color='red', label='ln(x)') 
plt.legend(loc='upper left')
plt.show()   
for c,n in zip(colors,nn):       
    plt.plot(x,abs(np.log(x)-applog(n,x)) )
plt.show()