tangerine7199
tangerine7199

Reputation: 489

Matplotlib "ValueError: x and y must be the same size"

I'm trying to create a scatter plot very similar to this.

My code is below. I'm comparing two groups of schools, one in a system and the other group is that system's peers. This was modeled after the directions found here.

plt.figure(figsize=(10,8))
plt.scatter(sys_peers_sat_earning['MD_EARN_WNE_P6'][sys_peers_sat_earning['SystemorPeer'] == 'USM'],
            sys_peers_sat_earning['SAT_AVG'][sys_peers_sat_earning['SystemorPeer'] == 'USM'],
            marker='x',
            color='b',
            alpha=0.7,
            s = 124,
            label='USM Schools')
plt.scatter(sys_peers_sat_earning['MD_EARN_WNE_P6'][sys_peers_sat_earning['SystemorPeer'] == 'Peer'],
            sys_peers_sat_earning['SAT_AVG'][sys_peers_sat_earning['SystemorPeer'] == 'Peer'],
            marker='x',
            color='b',
            alpha=0.7,
            d = 124,
            label='USM Peers')
plt.title('SATs and Earnings of Students Not Enrolled 6 Yrs. After Entry')
plt.ylabel('Median earnings of students working and not enrolled 6 years after entry')
plt.xlabel('Average SAT equivalent score of students admitted')
X_train[:,0]
plt.legend(loc='upper right')

errors both with and without the line of X_train[:,0] mentioned here came as follows: ValueError: x and y must be the same size

Can someone talk to me like I'm two with steps on how to edit this?

Upvotes: 2

Views: 13175

Answers (1)

Gustav Rasmussen
Gustav Rasmussen

Reputation: 3961

The error indicates that

sys_peers_sat_earning['MD_EARN_WNE_P6'][sys_peers_sat_earning['SystemorPeer'] == 'USM']

(your x value)

has a different size than

sys_peers_sat_earning['SAT_AVG'][sys_peers_sat_earning['SystemorPeer'] == 'USM']

(your y value)

Confirm this by renaming them into x and y respectively and check their length:

len(x) == len(y)

this will tell you if x and y are indeed the same size.

Upvotes: 1

Related Questions