Jane Lee Jane Lee - 12 days ago 5
Python Question

Plotting two different dataframes with different sizes?

I am having issue plotting two dataframs. One has 20711 entries, the other is 20710 entries. I am using plot(x,y) to plot like this:

import pandas as pd
import csv
import matplotlib.pyplot as plt

fig1 = plt.figure(figsize= (10,10))
ax = fig1.add_subplot(111)
ax.plot(X, Y)


Both are dataframes that were pulled from a csv. that have this structure:

print(X)
0 -2.343060
1 -2.445431
2 -2.335754
3 -2.478535
4 -2.527026
print(Y)
0 0.026940
1 -0.075431
2 0.024246
3 -0.118535
4 -0.167026
5 -0.145475


I keep getting error:

ValueError: x and y must have same first dimension


How do I fix it so that it ignores the last entry?

Answer

Well if you can just ditch the last value of Y then the following should work, assuming you have the index in your dataframe too, that is, your csv looks like this:

0,-2.343060
1,-2.445431
2,-2.335754
3,-2.478535
4,-2.527026

and you loaded it like X=pandas.read_csv('x.csv'), then

ax.plot(X.as_matrix().T[1], Y.as_matrix().T[1][:-1])

should work.

As you mentioned in your comment the overlap varies:

ax.plot(X.as_matrix().T[1], Y.as_matrix().T[1][:len(x)])
Comments