matplotlib scatter array lengths are not same

2024/10/11 16:30:46

i have 2 arrays like this

x_test = [[ 14.   1.] [ 14.   2.] [ 14.   3.] [ 14.   4.] [ 14.   5.] [ 14.   6.] [ 14.   7.] [ 14.   8.] [ 14.   9.] [ 14.  10.] [ 14.  11.] [ 14.  12.]]y_test = [ 254.7  255.4  476.5   19.5   85.6  352.   238.7  144.8   83.5  278.8   449.6  312.7]

i want these array to show scatter using matplotlib.

this is my code

plt.scatter(x_test, y_test,  color='black')
plt.plot(x_test, y_predict, color='blue', linewidth=3)plt.xticks(())
plt.yticks(())plt.show() 

and it gives me this error

Traceback (most recent call last):File "C:\wamp\www\python\test1.py", line 84, in <module>plt.scatter(x_test, y_test,  color='black')File "C:\Python34\lib\site-packages\matplotlib\pyplot.py", line 3251, in scatteredgecolors=edgecolors, data=data, **kwargs)File "C:\Python34\lib\site-packages\matplotlib\__init__.py", line 1812, in innerreturn func(ax, *args, **kwargs)File "C:\Python34\lib\site-packages\matplotlib\axes\_axes.py", line 3840, in scatterraise ValueError("x and y must be the same size")
ValueError: x and y must be the same size

both array length are same and i don't understand what is the problem here. please help me thanks

this is my y_predict array

y_predict = [  91.76428571  103.1          86.85714286  401.44642857  339.69642857196.83571429  126.38928571   31.41071429  211.67857143  167.35357143288.53214286  107.36785714]
Answer

The x_test, like you provided it, doesn't make sense for a scatter plot. And the arrays are actually not the same size:

print(x_test.shape)
print(y_test.shape)

(12, 2)

(12,)

What you might want to have instead is something like this (using only the second column of x_test):

plt.scatter(x_test[:,1], y_test,  color='black')
plt.plot(x_test[:,1], y_predict, color='blue', linewidth=3)
https://en.xdnf.cn/q/118298.html

Related Q&A

APLpy/matplotlib: Coordinate grid alpha levels for EPS quality figure

In the normal matplotlib axes class, it is possible to set gridlines to have a certain transparency (alpha level). Im attempting to utilise this with the APLpy package using the following:fig = pyplot.…

How to extract word frequency from document-term matrix?

I am doing LDA analysis with Python. And I used the following code to create a document-term matrixcorpus = [dictionary.doc2bow(text) for text in texts].Is there any easy ways to count the word frequen…

Remove only overlapping ticks in subplots grid

I have created a subplots grid without any spaces between the subplots, with shared x,y-axes. I only show the ticks and labels for the outer subplots. The problem is that the tick numbers overlap at th…

how to start a thread when django runserver?

I want to start a thread when django project runserver successfully. where can I put the create-thread-and-start code? Is there any hook for the django runserver?

pandas groupby plot values

I have a pandas dataframe that looks like this:**real I SI weights**0 1 3 0.3 0 2 4 0.20 1 3 0.50 1 5 0.51 2 5 0.3…

Any python module for customized BNF parser?

friends.I have a make-like style file needed to be parsed. The grammar is something like:samtools=/path/to/samtools picard=/path/to/picardtask1: des: descriptionpath: /path/to/task1para: [$global.samto…

How to draw an histogram with multiple categories in python

I am a freshman in python, and I have a problem of how to draw a histogram in python.First of all, I have ten intervals that are divided evenly according to the length of flowers petal, from min to max…

Turtle in Tkinter creating multiple windows

I am attempting to create a quick turtle display using Tkinter, but some odd things are happening.First two turtle windows are being created, (one blank, one with the turtles), secondly, any attempt of…

Array tkinter Entry to Label

Hey Guys I am beginner and working on Project Linear and Binary search GUI application using Tkinter, I want to add multiple Entry boxes values to label and in an array here, I tried but its not workin…

Grid search and cross validation SVM

i am implementing svm using best parameter of grid search on 10fold cross validation and i need to understand prediction results why are different i got two accuracy results testing on training set not…