Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ploting data points by omitting the lines Python Pandas

how do I plot only the data points by omitting the lines with Python Python/matplotlib.

Here is my example code so far:

  import matplotlib.pyplot as plt
  import pandas as pd

  df = pd.DataFrame({'x': [1,2,-3,3,5,7,-1],
                   'y' : [2.6,3.4,3.25,2.8,1.75,1.34,-3.345]})
  df.plot(x='x', y='y')
  plt.grid(True)
  plt.show()

All helpfull comments are appericated.

so far I tried although

df.plot.scatter(x='x', y='y')

but for my real applications, there seems to be a problem, that's why I was wondering if there exists another way to plot the data points.

As someone pointed out, I should say something about the issue. So I use df = pd.read_csv(...) to get my data. And for some reason df.plot.scatter (...) did not work. I always got " not in index" error. So When I tried any of the solutions mentioned here, I did not get the same Problem. To be frank I have no Idea why there was a problem in the first place. Still, I wanted to least mention the problem.

like image 582
sulphur Avatar asked Mar 24 '26 07:03

sulphur


2 Answers

You can use parameters marker and linestyle in plt.plot. You can experiment with marker, but if you want to omit lines, linestyle='none' is crucial here.

plt.plot(df.x, df.y, marker='.', linestyle='none')
like image 183
Piotrek Avatar answered Mar 25 '26 21:03

Piotrek


Use kind='scatter' as a paramater in pandas plot as shown in your code below:

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'x': [1,2,-3,3,5,7,-1],
                   'y' : [2.6,3.4,3.25,2.8,1.75,1.34,-3.345]})
df.plot(x='x', y='y', kind='scatter')
plt.grid(True)
plt.show()

Output:

enter image description here

like image 23
Scott Boston Avatar answered Mar 25 '26 20:03

Scott Boston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!