How to scale x axis which is increasing constantly using pandas

Viewed 67

I have 3 columns v1, v2, and v3 with 10,000 entries and I want to plot v1, v2, and v3 on the y-axis.

In the x-axis, I want to plot v1, v2, v3 points every 500 seconds until the length of column entries is reached (i.e 10,000 entries).

y-axis=['v1','v2', 'v3']
x-axis=[0,500,1000,1500... ,len(v1)]

I tried setting xticks:

length = len(df.axes[0]) #number of rows
x = np.arange(0,length,500)
y = ["v1", "v2", "v3"]
df.plot = (x,y)
plt.show()

but I am getting an error.

1 Answers

How's something like this, it grabs every 500th row (and includes the last row) and plots it using Pandas plot:

df[::500].append(df[-1:]).plot(y=['v1', 'v2', 'v3'], kind='line')

enter image description here

Full code:

############### create mock dataframe ################
import random
v1 = [random.randint(0,100) for i in range(10000)]
v2 = [random.randint(0,100) for i in range(10000)]
v3 = [random.randint(0,100) for i in range(10000)]
df = pd.DataFrame({'v1':v1, 'v2':v2, 'v3':v3})
######################################################

# Select every 500th row, include the last row with '.append(df[-1:])', then plot it
df[::500].append(df[-1:]).plot(y=['v1', 'v2', 'v3'], kind='line')
plt.xticks(np.arange(0,len(v1)+1, 500), rotation = 45)
plt.show()
Related