【问题标题】:Numpy arrays same shape but getting ValueError: x and y must have same first dimensionNumpy 数组形状相同但出现 ValueError:x 和 y 必须具有相同的第一维
【发布时间】:2017-01-06 01:38:57
【问题描述】:

难住了。它们都是 Numpy 数组它们都是相同的形状所以为什么我会得到“ValueError:x 和 y 必须具有相同的第一维”

谢谢

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

代码运行产生

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

【问题讨论】:

  • 对于任何想知道的人来说,错误不是Python错误,而是matplotlib错误(我没有测试最后三行,但之前没有错误)。
  • 不要让我们陷入悬念。显示打印。一些错误回溯可能会有所帮助,即使错误深深嵌套在 plot 调用中。其中两个输入的形状不匹配。
  • 请查看运行输出的编辑问题。抱歉,我没有考虑包含它
  • 请将引用的文本更改为代码文本以提高可读性。
  • 如果您查看回溯,您会发现错误来自plt.plot_date(dates,2)。 ranks 的形状无关紧要,因为 ranks 根本不涉及。

标签: python-3.x numpy matplotlib


【解决方案1】:

来自文档:

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.

您的 dates 是 (31,),但 2 是什么?为什么不是31个值的数组?

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

在上升线上绘制一组点,日期在 x 轴上。

【讨论】:

  • Hey hpaulj dates 是一个形状为 31 的 numpy 数组,由 dates = np.arange('2016-01', num_of_days , dtype='datetime64[D]') 行创建
  • 你想画什么?日期反对什么? 2号?如果我注释掉 plot_date 线,其余的工作,在 50 处产生一条水平线。试试我的最后一条绘图线而不是你的 2。
  • 嗨 hpaulj 我只是想根据排名值 50 绘制日期。所以我应该得到一条平线,这就是我想要的(这是有原因的)我“认为”它会很容易,但显然不是。我只想用我在 num_of_days ( 31) 中指定的天数 50 填充 numpy 数组排名,并使用 numpy 范围生成我已经完成的 31 个日期。所以我仍然不明白为什么我会收到这个错误,因为形状被明确定义为等级和日期相同,并且它们都是 numpy 数组。我错过了什么?
  • 问题不在于rank 情节;之前是plot_date 行。你想去那里做什么?
  • 嗨 Hpauli 如果我有大脑,我会很危险,如果我听 user2357112 的话,我会马上看到这个,如果我也听你的话,我看起来不会像一个完全的白痴。我不知道我在那里想做什么,当我听你说的那一刻,两个程序都起作用了。非常感谢你和一个明显是白痴的人在一起。
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-10
  • 2020-04-01
  • 2021-06-30
相关资源
最近更新 更多