Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
legend/color
#1
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()         
Reply
#2
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.
Reply
#3
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
Reply
#4
hi,

I am still trying to figure out how to solve this issue.
Anyone?
Reply
#5
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?
Reply
#6
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()        
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 1,040 Oct-25-2023, 09:09 AM
Last Post: codelab
  Unique Legend Image and Line nettesheim2 1 17,077 Sep-30-2016, 11:36 PM
Last Post: Exponential_Sinusoid

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020