1

I've been following this post in order to connect points in a scatter plot with lines, the written code is:

import pandas as pd
import matplotlib.pyplot as plt
#data exploration
data = pd.read_csv("file.csv",encoding = 'utf8')
scan=[range(1,55)]
row2=data.iloc[1,1:]
plt.scatter(scan,row2)
#plt.plot(scan,row2)

If I remove the last line comment then terminal throws out:

ValueError: x and y must have same first dimension, but have shapes (1, 54) and (54,)

And prints only the scatterplot. Any help with this? I don't know how to build a complete MWE in this case (sorry for that).

1 Answers1

0

Try converting your range object to a list as

scan = list(range(1,55))
plt.scatter(scan, row2)
plt.plot(scan, row2)
Sheldore
  • 35,129
  • 6
  • 43
  • 58
  • perfect. Can you please describe briefly why it doesn't work? You don't have to, anyways. I don't understand why [ ] is different from list( ) –  May 24 '19 at 12:21
  • 1
    @santimirandarp: This question seems to have no convincing accepted answer so far I think. Read [this](https://stackoverflow.com/questions/32684338/matplotlib-plotting-generators) and [this](https://stackoverflow.com/questions/28098342/plotting-in-matplotlib-from-a-generator) for example. It seem `plot` doesn't accept `range` which is a sequence iterator. I upvote your question as it is quite interesting observation. I am sorry for not having a clear answer as to why this is so. Experienced users like @ImportanceOfBeingEarnest might know this. – Sheldore May 24 '19 at 12:27