简体   繁体   English

绘制两个不同大小的不同数据框?

[英]Plotting two different dataframes with different sizes?

I am having issue plotting two dataframs. 我在绘制两个数据帧时遇到问题。 One has 20711 entries, the other is 20710 entries. 一个有20711个条目,另一个是20710个条目。 I am using plot(x,y) to plot like this: 我正在使用plot(x,y)进行如下绘制:

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. 两者都是从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? 如何解决它,使其忽略最后一个条目?

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: 好吧,如果您可以放弃Y的最后一个值,那么以下操作应该起作用,假设您的数据帧中也有索引,即csv看起来像这样:

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 然后像X=pandas.read_csv('x.csv')一样加载它,然后

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)])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM