Reputation: 473
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.
Upvotes: 10
Views: 15572
Reputation: 322
At least as of today, df.plot()
accepts matplotlib.plot
arguments as keyword arguments. Hence you can simply do `df.plot(linestyle='none',marker='.') to get the desired behaviour.
Happy Coding!
Upvotes: 0
Reputation: 1530
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')
Upvotes: 20
Reputation: 153560
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:
Upvotes: 3
Reputation: 3817
or replace last three lines
df.plot(x='x', y='y')
plt.grid(True)
plt.show()
by:
plt.scatter(x=df.x.tolist(), y=df.y.tolist())
plt.grid(True)
plt.show()
Upvotes: 0