Python Forum
ValueError: x and y must have same first dimension, but have shapes (11,) and (15406,
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: x and y must have same first dimension, but have shapes (11,) and (15406,
#1
Hello, any idea how can I fix this error?

Error:
ValueError: x and y must have same first dimension, but have shapes (11,) and (15406, 1)
I am trying to plot... I tried also to convert the x values and y values by using np.array without any success.
Reply
#2
You are so scarce on information, so
https://numpy.org/doc/stable/reference/g...shape.html

reshaping may or may not solve your problem, depending on what you actually want to plot.
hobbyist likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Ok, so I have an excel with 15000 measurements, (1st column:timestamps, 2nd column values). I want to represent them to a plot, where on the y-axis will be the measurements (the graph) and on the x-axis totally 20-25 values. I cannot obviously put 15000 xticks. I follow this code:

import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

time = mdates.drange(dt.datetime(2014, 12, 20), dt.datetime(2015, 1, 2),
                     dt.timedelta(hours=2))
y = np.random.normal(0, 1, time.size).cumsum()
y -= y.min()

fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(time, y, 'bo-')
ax.set(title='Active Calls', ylabel='Calls', xlabel='Time')
ax.grid()

ax.xaxis_date() # Default date formatter
fig.autofmt_xdate()

plt.show()
and I try to do modifications to the code in order to make it work...but I got errors like the one above. Can you help me?
Reply
#4
First of all - do you really want 15000 points on that plot? Do you really have 15000 distinct dates?
You can explicitly set the xticks - look at https://stackoverflow.com/q/12608788/4046632
hobbyist likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Here is an example as far as I can understand what your goal is

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import random

x = mdates.drange(dt.datetime(2021, 3, 1, 0, 0, 0), dt.datetime(2021, 3, 3, 0, 0, 0),
                     dt.timedelta(hours=1))
y = [random.randint(0,100) for _ in range(len(x))]
time = mdates.drange(dt.datetime(2021, 3, 1, 0, 0, 0), dt.datetime(2021, 3, 3, 1, 0, 0),
                     dt.timedelta(hours=6))
 
fig, ax = plt.subplots(figsize=(8, 6))
plt.xticks(time)
ax.xaxis.set_major_formatter(mdates.DateFormatter("%m-%d %H:%M"))
# ax.xaxis.set_minor_formatter(mdates.DateFormatter("%m-%d %H:%M"))
ax.plot(x, y, 'bo-')
ax.set(title='Active Calls', ylabel='Calls', xlabel='Time')
ax.grid()
 
ax.xaxis_date() # Default date formatter
fig.autofmt_xdate()
 
plt.show()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
So, I tried a mix of the code you posted above + stackoverflow code from your link. This is what I am trying:

time = mdates.drange(dt.datetime(2021, 3, 1, 0, 0, 0), dt.datetime(2021, 3, 3, 1, 0, 0), dt.timedelta(hours=6))
y = trainPredictPlot
fig, ax = plt.subplots()
ax.plot(time,y)
start, end = ax.get_xlim()
ax.xaxis.set_ticks(np.arange(start, end, 10))
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
plt.show()
The error I got a similar error:
Error:
ValueError: x and y must have same first dimension, but have shapes (9,) and (15406, 1)
The problem starts at line 4. It does not run from line 4 and down...
Reply
#7
Again you are doing the same mistake. In ax.plot first argument which is X values must be the same size as the second one - y values. It must be timestamps from your file. time should be used only for xticks.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
So, I should somehow transform the excel column of the timestamps to an array? np.array?
Reply
#9
is it np.array is up to you. it can be pandas dataframe or simple list
e.g.


import datetime as dt
import pandas as pd 
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import random

days = mdates.HourLocator(byhour=(0, 12))   # every 12 hours
hours = mdates.HourLocator()

df = pd.read_excel('data.xlsx')
 
fig, ax = plt.subplots(figsize=(8, 6))
ax.xaxis.set_major_locator(days)
ax.xaxis.set_major_formatter(mdates.DateFormatter("%m-%d %H:%M"))
ax.xaxis.set_minor_locator(hours)
ax.plot(df.x, df.y, 'bo-')
ax.set(title='Active Calls', ylabel='Calls', xlabel='Time')
ax.grid()
 
ax.xaxis_date() # Default date formatter
fig.autofmt_xdate()
 
plt.show()
   


import datetime as dt
import pandas as pd 
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import random

df = pd.read_excel('data.xlsx')
time = mdates.drange(dt.datetime(2021, 3, 1, 0, 0, 0), dt.datetime(2021, 3, 3, 1, 0, 0),
                     dt.timedelta(hours=6))
 
fig, ax = plt.subplots(figsize=(8, 6))
plt.xticks(time)
ax.xaxis.set_major_formatter(mdates.DateFormatter("%m-%d %H:%M"))
ax.plot(df.x, df.y, 'bo-')
ax.set(title='Active Calls', ylabel='Calls', xlabel='Time')
ax.grid()
 
ax.xaxis_date() # Default date formatter
fig.autofmt_xdate()
 
plt.show()
   
hobbyist likes this post

Attached Files

.xlsx   data.xlsx (Size: 5.57 KB / Downloads: 227)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
I ran the first (of the two) piece of code, and I got this error. Instead of df I use dataframe command.

Error:
return object.__getattribute__(self, name) AttributeError: 'DataFrame' object has no attribute 'x'
So to be more specific it does not run line 16 and down. Is it something that has to do with the excel? The excel file has 4 columns. The first is the timestamps, and the other 3 the measurements.

Any idea?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I design shapes on a curve in python? mervea 2 765 Sep-14-2023, 01:04 PM
Last Post: Pedroski55
  Array dimension don't match asja2010 0 1,019 Feb-23-2023, 04:22 PM
Last Post: asja2010
  x and y must have same first dimension, but have shapes (1,) and (50,) asja2010 5 2,483 Jan-12-2023, 07:24 PM
Last Post: deanhystad
  Shapes in Video finndude 0 632 Oct-07-2022, 03:30 PM
Last Post: finndude
  Shapes over video in tkinter finndude 1 927 Oct-04-2022, 06:14 PM
Last Post: deanhystad
  operands could not be broadcast together with shapes (337,451) (225,301) kevinabbot 0 1,537 Dec-14-2021, 04:02 PM
Last Post: kevinabbot
  Strange error ValueError: dimension mismatch Anldra12 0 1,937 Aug-17-2021, 07:54 AM
Last Post: Anldra12
  ValueError: dimension mismatch Anldra12 0 3,347 Jul-17-2021, 04:46 PM
Last Post: Anldra12
  Error When Plotting ValueError: x and y must have same first dimension JoeDainton123 1 9,017 Oct-04-2020, 12:58 PM
Last Post: scidam
  Wrong dimension for my plot Jemeronimo 1 1,988 Apr-25-2019, 06:19 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020