简体   繁体   中英

Numpy arrays same shape but getting ValueError: x and y must have same first dimension

Stumped. they are both Numpy arrays they are both the same shape so why am I getting "ValueError: x and y must have same first dimension"

thanks

import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime

num_of_days = 31
dates = np.empty(num_of_days)
ranks = np.empty(dates.shape[0])
ranks.fill(50)  # test data all 50's 
dates = np.arange('2016-01', num_of_days , dtype='datetime64[D]')
print("ranks shape", ranks.shape)
print("dates shape", dates.shape)
print("ranks type", type(ranks))
print("dates type", type(dates))
print("dates shape first dimension",dates.shape[0])
print( dates)
print(ranks)

plt.plot_date(dates,2)
plt.plot(dates,ranks)
plt.show()

The code run yields

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
===== RESTART: E:\scanned\als course\12 15 14 the al project\dev code\8 29 16 test plots.py =====
ranks shape (31,)
dates shape (31,)
ranks type <class 'numpy.ndarray'>
dates type <class 'numpy.ndarray'>
dates shape first dimension 31
['2016-01-01' '2016-01-02' '2016-01-03' '2016-01-04' '2016-01-05'
 '2016-01-06' '2016-01-07' '2016-01-08' '2016-01-09' '2016-01-10'
 '2016-01-11' '2016-01-12' '2016-01-13' '2016-01-14' '2016-01-15'
 '2016-01-16' '2016-01-17' '2016-01-18' '2016-01-19' '2016-01-20'
 '2016-01-21' '2016-01-22' '2016-01-23' '2016-01-24' '2016-01-25'
 '2016-01-26' '2016-01-27' '2016-01-28' '2016-01-29' '2016-01-30'
 '2016-01-31']
[ 50.  50.  50.  50.  50.  50.  50.  50.  50.  50.  50.  50.  50.  50.  50.
  50.  50.  50.  50.  50.  50.  50.  50.  50.  50.  50.  50.  50.  50.  50.
  50.]
Traceback (most recent call last):
  File "E:\scanned\als course\12 15 14 the al project\dev code\8 29 16 test plots.py", line 23, in <module>
    plt.plot_date(dates,2)
  File "C:\Users\dave\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\pyplot.py", line 3173, in plot_date
    data=data, **kwargs)
  File "C:\Users\dave\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\__init__.py", line 1812, in inner
    return func(ax, *args, **kwargs)
  File "C:\Users\dave\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\axes\_axes.py", line 1494, in plot_date
    ret = self.plot(x, y, fmt, **kwargs)
  File "C:\Users\dave\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\__init__.py", line 1812, in inner
    return func(ax, *args, **kwargs)
  File "C:\Users\dave\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\axes\_axes.py", line 1424, in plot
    for line in self._get_lines(*args, **kwargs):
  File "C:\Users\dave\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\axes\_base.py", line 386, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "C:\Users\dave\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\axes\_base.py", line 364, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "C:\Users\dave\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\axes\_base.py", line 223, in _xy_from_xy
    raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension
>>> 

From the docs:

plot_date(x, y, fmt='bo', tz=None, xdate=True,
          ydate=False, **kwargs)
Similar to the plot() command, except the x or y (or both) data is considered to be dates, and the axis is labeled accordingly.

Your dates is (31,), but what's with the 2 ? Why isn't it some array with 31 values?

plt.plot_date(dates, np.arange(dates.shape[0]))

plots a set of points in a rising line with dates on the x axis.

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