0

Similar with: Plot size = 1/{N∗⌈log2N⌉∗[(1/70)/60]} in R?

But with matplotlib in python (I guess it will be better to plot the function with matplotlib):

size = 1/{N∗⌈log_2(N)⌉∗[(a)/60]}

a = [1/70, 1/60, 1/50, 1/40]

How can I plot this function (for every value in a - it should be one graphic) with matplotlib in python?

(⌈⌉= ceil)

For example: enter image description here

With label "size" for y-axis and "N" for the x-axis.

N >= 2, N is natural Number (2,3,4,5,6,...) (but it is not necessary to implement this... see picture above)

I have tried this one as a first approach:

import matplotlib.pyplot as plt
import numpy as np

n = np.arange(3,50,0.1)
size = (1)/n*np.ceil(np.log2(n))*((1/70)/60))
plt.plot(n,size)
plt.axis([3,50,0,550])
plt.show()
Innat
  • 16,113
  • 6
  • 53
  • 101
GenXGer
  • 67
  • 8
  • You have a typo in your equation, it should be `size = 1/(n*np.ceil(np.log2(n))*((1/70)/60))`. In addition `plt.axis()` does not work the way you seem to think it does. You need to adjust `plt.xlim()` and `plt.ylim()`. Besides that, please explain what it wrong with the solution you have found – Diziet Asahi Jul 17 '18 at 14:18
  • The solution which I "have found" was plotted with an online-tool and some information added via paint... ;) – GenXGer Jul 19 '18 at 10:59

1 Answers1

2

If you are looking to plot all the distinct segments and not as continuous lines, one way would be to look for discontinuities in the derivative. In this case, the slopes should always be increasing as n increases (n > 0), so you can look for when it violates this condition and then split the lines there.

    import matplotlib.pyplot as plt
    import numpy as np
    from numpy import diff

    n = np.arange(3,50,0.1)
    a = [1/70,1/60,1/50,1/40]

    discont = np.ones(len(n)-1)   #array to show discontinuities
    discont[1] = 0

    for i in a:
    
        size = 1/(n*np.ceil(np.log2(n))*(i/60)) 
        derivs = diff(size)
    
        for k in range(len(derivs)-2): 
            if derivs[k+1] > derivs[k]:
                discont[k+2] = 0     
                   
        segments = np.squeeze(np.asarray(discont.nonzero())) 
        
        for j in range(len(segments)-1):
            start, stop = segments[j], segments[j+1]
            plt.plot(n[start:stop],size[start:stop], 'b')

    plt.axis([0,20,0,300])
    plt.xlabel('N')
    plt.ylabel('Size')
    plt.grid()
    plt.show()

This will produce the following plot: log_graphs.png