0

I am trying to plot two arrays that are input via command line. For instance I have the program testarray.py

Now when I enter the command line

python testarray.py "1 2 3 4 5 6" "1 2 4 8 16 32"

I am trying to get a plot that looks like the following:

Desired Plot

Unfortunately I am not getting this plot. I have a linear plot where the y axis is not linear.

Below is a snippet of my code:

xarr=np.array([])
yarr=np.array([])
zarr=np.array([])

x=sys.argv[1].split(' ')
y=sys.argv[2].split(' ')
length=len(x)

for t in range(0,length):
  xarr = np.append(xarr,x[t])
  yarr = np.append(yarr,y[t])
plt.plot(xarr,yarr,'b')

How do I tweak this code to get the desired result?

1
  • Unfortunately I am not getting this plot. What are you getting instead? Please provide a minimal reproducible example. Commented Aug 5, 2020 at 0:07

1 Answer 1

1

This code works. Before you plot, you must convert the numbers that are of type string to integer.

import sys
import matplotlib.pyplot as plt

if __name__ == '__main__':
    x=sys.argv[1].split(' ')
    y=sys.argv[2].split(' ')
    for i in range(len(x)):
        x[i] = int(x[i])
        y[i] = int(y[i])
    plt.plot(x, y)
    plt.show()

Update With the use of map you can make the whole thing even more elegant.

import sys
import matplotlib.pyplot as plt
if __name__ == '__main__':
    x=list(map(int, sys.argv[1].split(' ')))
    y=list(map(int, sys.argv[2].split(' ')))

    plt.plot(x,y,'b')
    plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.