Вы можете сделать это более просто, используя plot()
вместо plot_date()
.
Сначала преобразуем ваши строки в экземпляры Python datetime.date
:
import datetime as dt
dates = ['01/02/1991','01/03/1991','01/04/1991']
x = [dt.datetime.strptime(d,'%m/%d/%Y').date() for d in dates]
y = range(len(x)) # many thanks to Kyss Tao for setting me straight here
Затем сюжет:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.plot(x,y)
plt.gcf().autofmt_xdate()
Результат:
[/g0]
Итак, я уверен, что ваша проблема связана с форматом массива train_x. Я пробовал вашу программу с массивом из 10 000 строк и 6 столбцов, и он работал нормально, поэтому проблема не в размерах. По какой-то причине один из len(x_train)
или len(x_train[0])
равен 0. Что заставляет меня думать, что это так:
ValueError, который вы получаете, - это модуль matplotlib.axes._subplot, который касается рисования многих небольшие подсети внутри большого участка (так каждая маленькая гистограмма). Код модуля таков:
"""
*rows*, *cols*, *num* are arguments where
the array of subplots in the figure has dimensions *rows*,
*cols*, and where *num* is the number of the subplot
being created. *num* starts at 1 in the upper left
corner and increases to the right.
"""
rows, cols, num = args
rows = int(rows)
cols = int(cols)
if isinstance(num, tuple) and len(num) == 2:
num = [int(n) for n in num]
self._subplotspec = GridSpec(rows, cols)[num[0] - 1:num[1]]
else:
if num < 1 or num > rows*cols:
raise ValueError(
"num must be 1 <= num <= {maxn}, not {num}".format(
maxn=rows*cols, num=num))
Ваша проблема в этой части (см. Пояснение в комментариях в коде):
if num < 1 or num > rows*cols:
# maxN is the number of rows*cols and since this is showing 0 for you (in your error stacktrace),
# it means the number of cols being passed into your histogram is 0. Don't know why though :P
raise ValueError(
"num must be 1 <= num <= {maxn}, not {num}".format(
maxn=rows*cols, num=num))
Я не знаю, как вы читают ваш формат ввода, но я уверен, что проблема связана с ним. Если вы установите x_train на это, он отлично работает:
x_train = [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[1.0, 1.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.3333333333333333, 0.3333333333333333, 2.0, 2.0, 2.0, 2.0],
[0.0, 0.0, 3.0, 3.0, 3.0, 3.0]]
Попробуйте сделать это, прежде чем вызывать код в своем вопросе и посмотреть, работает ли это:
x_train = list([list(x) for x in x_train])
У меня была та же проблема, и я обнаружил, что это связано с тем, что массив NumPy был массивом объектов, а не массивом с плавающей запятой.
Попробуйте следующее:
x_train = x_train.astype(np.float)