简体   繁体   中英

ValueError: x and y must have same first dimension when plotting dataframe column of integers

I have aa dataframe with year/month column and a int column. It looks like this:

         Date     count
0  2018-01-01  19730570
1  2018-02-01  19301103
2  2018-03-01  21962470
3  2018-04-01  21034792
4  2018-05-01  21556113
5  2018-06-01  21118266
6  2018-07-01  21584891
7  2018-08-01  22101502
8  2018-09-01  22123605
9  2018-10-01  23266816
10 2018-11-01  22861081

I am trying to graph this using matplot, but i keep getting an error that my x and y are different dimensions, when clearly they are not.

ValueError: x and y must have same first dimension, but have shapes (11,) and (1,)

I already converted my date column to datetime, but still am unsure why I am getting this error.

Here is what I am trying to execute:

plt.plot(monthly_trips_fhv.Date, monthly_trips_fhv.count)

  • pandas.DataFrame.count is a dataframe method.
  • Your column name is mirroring that method name, so you need to use monthly_trips_fhv['count']
import pandas as pd
import matplotlib.pyplot as plt

plt.plot(monthly_trips_fhv.Date, monthly_trips_fhv['count'])
plt.xticks(rotation=90)
plt.show()

在此处输入图像描述

You can also use Pandas's plot function:

df.plot(x='Date', y='count')

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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