0

I have written following code,

import numpy as np
import matplotlib.pyplot as plt
x=np.random.randint(0,10,[1,5])
y=np.random.randint(0,10,[1,5])
x.sort(),y.sort()
fig, ax=plt.subplots(figsize=(10,10))
ax.plot(x,y)
ax.set( title="random data plot", xlabel="x",ylabel="y")

I am getting a blank figure. Same code prints chart if I manually assign below value to x and y and not use random function.

x=[1,2,3,4]
y=[11,22,33,44]

Am I missing something or doing something wrong.

0

2 Answers 2

4

x=np.random.randint(0,10,[1,5]) returns an array if you specify the shape as [1,5]. Either you would want x=np.random.randint(0,10,[1,5])[0] or x=np.random.randint(0,10,size = 5). See: https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.randint.html

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, Thanks. I was generating 2D array and thrn trying to plot. This should have been done with 1D array. x=np.random.randint(0,10, 5) would have sufficed.
1

Matplotlib doesn't plot markers by default, only a line. As per @Can comment, matplotlib then interprets your (1, 5) array as 5 different datasets each with 1 point, so there is no line as there is no second point.

If you add a marker to your plot function then you can see the data is actually being plotted, just probably not as you wish:

import matplotlib.pyplot as plt
import numpy as np
x=np.random.randint(0,10,[1,5])
y=np.random.randint(0,10,[1,5])
x.sort(),y.sort()
fig, ax=plt.subplots(figsize=(10,10))
ax.plot(x,y, marker='.') # <<< marker for each point added here
ax.set( title="random data plot", xlabel="x",ylabel="y")

1 Comment

Thanks, @'Paddy Harrison'. Your answer opened new learning for me. I thought 2D couldn't be plotted.

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.