Resolved: can not plot the scatter plot
Dear Team ,
I am getting this error when I try to plot the scatter plot as you did in the lecture , what do I am missing here ?
plt.scatter(x1,y,color='C0')
plt.xlabel('SAT',fontsize=20)
plt.ylabel('Admitted',fontsize=20)
plt.show()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/var/folders/2r/8pcly7hx4t7fzdk8_jmmp3800000gn/T/ipykernel_2715/1683522301.py in <module>
----> 1 plt.scatter(x1,y,color='C0')
2 plt.xlabel('SAT',fontsize=20)
3 plt.ylabel('Admitted',fontsize=20)
4 plt.show()
~/opt/anaconda3/lib/python3.9/site-packages/matplotlib/pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, edgecolors, plotnonfinite, data, **kwargs)
2817 vmin=None, vmax=None, alpha=None, linewidths=None, *,
2818 edgecolors=None, plotnonfinite=False, data=None, **kwargs):
-> 2819 __ret = gca().scatter(
2820 x, y, s=s, c=c, marker=marker, cmap=cmap, norm=norm,
2821 vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths,
~/opt/anaconda3/lib/python3.9/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
1410 def inner(ax, *args, data=None, **kwargs):
1411 if data is None:
-> 1412 return func(ax, *map(sanitize_sequence, args), **kwargs)
1413
1414 bound = new_sig.bind(ax, *args, **kwargs)
~/opt/anaconda3/lib/python3.9/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, edgecolors, plotnonfinite, **kwargs)
4360 y = np.ma.ravel(y)
4361 if x.size != y.size:
-> 4362 raise ValueError("x and y must be the same size")
4363
4364 if s is None:
ValueError: x and y must be the same size
3 answers ( 1 marked as helpful)
Hey Mohamed,
Thank you for reaching out!
It seems that the variables x1
and y
in your code are of different sizes. It's important that the length of both arrays is the same, so that each value in x1
is mapped onto a corresponding value in y
. Go through your code and try to find why this discrepancy occurs.
Kind regards,
365 Hristina
Hey Hristina,
Thanks a lot for your feedback which was helpful.
Best regards,
Mohamed
Here is what I have done to display the bar plot:
numbers = range(1,len(x)+1)
plt.bar(numbers, y, tick_label=x)
bar expects the x position as the first argument