2016-08-29 3 views
0

Stumped. sie sind beide Numpy Arrays sie beide die gleiche Form sind also warum bin ich immer „Valueerror: x und y müssen gleiche erste Dimension haben“Numpy-Arrays mit gleicher Form, aber mit ValueError: x und y müssen dieselbe erste Dimension haben

dank

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

Der Code Lauf Ausbeuten

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

Für alle, die sich wundern, ist der Fehler kein Python-Fehler, sondern ein Matplotlib-Fehler (Ich habe die letzten drei Zeilen nicht getestet, aber es gibt keinen Fehler vorher). – Benjamin

+0

Lassen Sie uns nicht in der Schwebe. Zeigen Sie die Drucke. Ein Teil des Fehler-Tracebacks kann hilfreich sein, auch wenn der Fehler im Aufruf "Plot" stark verschachtelt ist. Zwei dieser Eingänge stimmen nicht in ihrer Form überein. – hpaulj

+1

Bitte beachten Sie die bearbeitete Frage für die Run-Ausgabe. Tut mir leid, dass ich nicht daran gedacht habe, es einzuschließen – theakson

Antwort

1

Aus der Dokumentation:

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. 

Ihre dates ist (31,), aber was ist mit der 2? Warum ist es nicht ein Array mit 31 Werten?

zeichnet eine Reihe von Punkten in einer steigenden Linie mit Daten auf der X-Achse.

+0

Hey hpaulj dates ist ein numpliges Array mit der Form 31, erstellt von der Zeile dates = np.arange ('2016-01', num_of_days, dtype = 'datetime64 [D]') – theakson

+0

Was versuchst du zu zeichnen? Die Daten gegen was? Die Nummer 2? Wenn ich die 'plot_date'-Zeile auskommentiere, arbeitet der Rest und erzeugt eine horizontale Linie bei 50. Probieren Sie meine letzte Handlungslinie statt Ihrer 2. – hpaulj

+0

Hi hpaulj Ich versuche nur, Datum gegen den Rangwert 50 zu plotten. Also sollte ich enden mit einer flachen Linie, was ich will (es gibt einen Grund dafür) Ich "dachte" es würde leicht sein, aber anscheinend nicht. Ich wollte nur den numpy array rank mit der Zahl 50 so viele Tage füllen, wie ich in num_of_days (31) angegeben habe UND numpy range erzeugen 31 Daten, die ich gemacht habe. SO sehe ich immer noch nicht, warum ich diesen Fehler bekomme, wenn man bedenkt, dass die Formen sowohl für Rang als auch für Datum eindeutig definiert sind und beide Nummernfelder sind. Was vermisse ich? – theakson

Verwandte Themen