Bowen Peng
Bowen Peng

Reputation: 1815

ValueError: x and y must be the same size when drawing ax[i].scatter in plt.subplots()

I try to plot the tradeMoney of every day in scatter().
So I just write a simple one as fllows:

plt.scatter(train_EDA[(train_EDA['tradeMonth'] == 1)]['tradeDay'],\
            train_EDA[(train_EDA['tradeMonth'] == 1)]['tradeMoney'])

enter image description here

It is definitely right.
So I try to plot every month as follows:

nrows, ncols = 12, 1
fig, ax = plt.subplots(nrows=12, ncols=1, figsize=(8, 96))

for i in range(12):
    ax[i].scatter(train_EDA[(train_EDA['tradeMonth'] == i)]['tradeDay'], 
train_EDA[(train_EDA['tradeMonth'] == 1)]['tradeMoney'])

The plots are all blank.
And error messages are as fllows:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last) in
3
4 for i in range(12):
----> 5 ax[i].scatter(train_EDA[(train_EDA['tradeMonth'] == i)]
['tradeDay'], train_EDA[(train_EDA['tradeMonth'] == 1)]['tradeMoney'])

~\Anaconda3\lib\site-packages\matplotlib__init__.py in inner(ax, data, *args, **kwargs)
1808 "the Matplotlib list!)" % (label_namer, func.name),
1809
RuntimeWarning, stacklevel=2)
-> 1810 return func(ax, *args, **kwargs)
1811
1812 inner.doc = _add_data_doc(inner.doc,


~\Anaconda3\lib\site-packages\matplotlib\axes_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
4180 y = np.ma.ravel(y)
4181 if x.size != y.size:
-> 4182 raise ValueError("x and y must be the same size")
4183
4184 if s is None:


ValueError: x and y must be the same size

Here is the dataframe:

    tradeMonth  tradeDay    tradeMoney
0   12          22          16000.0
1   11          14          14000.0
2   2           10          6000.0
3   4           16          3400.0
4   2           28          8000.0
5   3           24          3000.0
......
......
......

After searching for some relevant questions, its error is mostly caused by the dimensions of data.
BUT there is no such problem.
Could anyone help me how to solve it?

Upvotes: 0

Views: 277

Answers (1)

Kyle
Kyle

Reputation: 1170

Looks like you accidentally left a 1 where you wanted an i; change

ax[i].scatter(train_EDA[(train_EDA['tradeMonth'] == i)]['tradeDay'], 
train_EDA[(train_EDA['tradeMonth'] == 1)]['tradeMoney'])

to

ax[i].scatter(train_EDA[(train_EDA['tradeMonth'] == i)]['tradeDay'], 
train_EDA[(train_EDA['tradeMonth'] == i)]['tradeMoney'])

Upvotes: 1

Related Questions