Reputation: 79
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.
Upvotes: 0
Views: 96
Reputation: 1992
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")
Upvotes: 1
Reputation: 137
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
Upvotes: 4