Python Forum
Issues with matplotlib.pyplot
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issues with matplotlib.pyplot
#1
I'm plotting different sets of data onto the same plot, but would like to adjust the y-axis to display powers of ten, from 10-19 to 10-5 along all 10n for all integers n in between -19 and -5. I've also tried plt.yticks(), but to no avail. Here's an image of the plot from the below code.

[Image: nCJzYot]

error_2=[]
error_3=[]
error_4=[]
error_5=[]
xvalue=[x for x in np.linspace(6,21,16)]
for x in xvalue:
    error_2.append(abs(ln(x,2)-np.log(x)))
    error_3.append(abs(ln(x,3)-np.log(x)))
    error_4.append(abs(ln(x,4)-np.log(x)))
    error_5.append(abs(ln(x,5)-np.log(x)))
plt.figure(5)
plt.plot(xvalue,error_2,'b')
plt.plot(xvalue,error_3,'g')
plt.plot(xvalue,error_4,'r')
plt.plot(xvalue,error_5,'y')
plt.ylim(10**(-19), 10**(-5))
plt.show()
My lowest values in error_5:
[4.440892098500626e-16, 2.6645352591003757e-15, 5.329070518200751e-15,
 1.199040866595169e-14, 2.1760371282653068e-14, 3.6415315207705135e-14,
 5.639932965095795e-14, 8.304468224196171e-14, 1.1857181902996672e-13,
 1.6431300764452317e-13, 2.2160051571518125e-13, 2.8954616482224083e-13,
 3.717026686445024e-13, 4.698463840213662e-13, 5.830891325331322e-13,
 7.123190925995004e-13]
Reply
#2
You need to use semilogy plotting function to get desired result.
Moreover, I don't understand why you are converting numpy array returned by np.linspace into list.
There is helper method for that:

xvalue = [x for x in np.linspace(6,21,16)]
# is totally equivalent to
np.linspace(6, 21, 16).tolist() 
But...,I think, we don't need to convert numpy array to list in this case.

I didn't see definition of ln(x, y) function and just created a sample one to run
the code I wrote:

import numpy as np
from matplotlib import pyplot as plt
xvalues = np.linspace(6, 21, 16) 

@np.vectorize
def ln(x, y): # now x and y could be vectors. 
    return x + y

def error(j, xvalues):
    return abs(ln(xvalues, j) - np.log(xvalues))

for j, c in zip(range(2, 6), 'bgry'):
    plt.semilogy(xvalues, error(j, xvalues), color=c)
plt.show()
Reply
#3
Yeah, you're right about the np.linspace thing.

When I copy your code, I get almost the desired result (see linked image).

[Image: pstNJrm]

How would one change it so that the ticks on the y-axis are in intervals of 10-n for all integers n in between -19 and -5, which means that the y-axis should also have 10-19 and 10-5 as its bottom and top limits?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to customize x axis in matplotlib.pyplot for a scatter plot? wlsa 9 8,191 Nov-10-2018, 01:32 AM
Last Post: wlsa
  How to create custom error bars in matplotlib.pyplot? wlsa 1 4,253 Nov-04-2018, 09:49 PM
Last Post: wlsa
  Pyplot line color and spacing/padding metalray 0 2,690 May-26-2017, 08:39 AM
Last Post: metalray

Forum Jump:

User Panel Messages

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