0
$\begingroup$

I am getting started with visualization, but right at the start I am having a serious conceptual problem. Repeatedly, I get the error ‘x and y must be the same size’ / ‘array must be the same length’ / ‘have the same shape’. I just fundamentally don’t understand why that must be so. Let me give you an example:

I go bowling once a day, every day, for a month. I keep track of my scores. I want to know what day of the week I get the best scores. I want to make a chart with days of the week on the y axis, and dates of the month on the x axis. My scores go in each intersection of these two axes. There are 7 days in a week, and 28 (just to ignore a few days getting more turns) days in a month. Clearly, x and y are not the same length, and yes, I get an error. But so what? Why is that an issue? Every day and date will get a score. I would think it is much more common for x and y to not be the same length in the real world.

For example, how would anyone ever be able to do a long running time series? So clearly there is something fundamental about charts that I just am not grasping. Can you tell me what it is? Thanks.

@seanowen

Yes, and bars, too...

import altair as alt
import pandas as pd

data = pd.DataFrame({'y': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
                     'x':[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,
27,28]})

ValueError                                Traceback (most recent call last)
/tmp/ipykernel_233189/3256713105.py in <module>
.....snip...
~/.virtualenvs/PapasBook/lib/python3.9/site-packages/pandas/core/internals/construction.py in _extract_index(data)
    633             lengths = list(set(raw_lengths))
    634             if len(lengths) > 1:
--> 635                 raise ValueError("All arrays must be of the same length")
    636 
    637             if have_dicts:

ValueError: All arrays must be of the same length
$\endgroup$
4
  • $\begingroup$ What error, from where? x and y axes most certainly do not have to be the same size. Indeed they are rarely even in the same units. Are you talking about a scatterplot? $\endgroup$
    – Sean Owen
    Jul 26 '21 at 21:47
  • $\begingroup$ Yes, and bars, too. $\endgroup$ Jul 26 '21 at 23:04
  • $\begingroup$ "...you're asking for 7 rows, but also 31 rows" I thought I was asking for 7 rows and 31 columns. It is the same issue. altair-viz.github.io/getting_started/starting.html "When using Altair, datasets are most commonly provided as a Dataframe. As we will see, the labeled columns of the dataframe are an essential piece of plotting with Altair." I had this same issue with matplotlib before I gave up and went to Altair. Bokeh, however, does not insist on the use of pandas... Thanks $\endgroup$ Jul 30 '21 at 22:21
  • $\begingroup$ You're misunderstanding the DataFrame constructor. You need to pass N values for each column you specify in the dict. You are not supplying the same number of values. I'm confused what you think this would do - what do you think the x and y values you supply here specify as data in the table? $\endgroup$
    – Sean Owen
    Aug 12 '21 at 13:34
1
$\begingroup$

You aren't making a plot here. You're creating a DataFrame. A DataFrame is like a table, with rows and columns. You are specifying two columns x and y, but don't give the same number of values. It's like you're asking for 7 rows, but also 31 rows.

I believe you really mean to create a DataFrame with three columns: dayOfWeek, dayOfMonth, and score. A row represents one score, along with the day of week and day of month it happened.

This kind of data could be correctly used for all kinds of plots then.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.