legend/color - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: legend/color (/thread-17547.html) |
legend/color - mcgrim - Apr-15-2019 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() RE: legend/color - Larz60+ - Apr-15-2019 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. RE: legend/color - mcgrim - Apr-16-2019 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 RE: legend/color - mcgrim - Apr-17-2019 hi, I am still trying to figure out how to solve this issue. Anyone? RE: legend/color - Larz60+ - Apr-17-2019 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? RE: legend/color - mcgrim - Apr-17-2019 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() |