Pandas scatter plot
Pandas scatter plot
Im new to Python and Pandas but have a CSV file with multiple columns that I have read in to a dataframe. I would like to plot a scatter plot of x=Index and y='data'. Where the index is Index of the dataframe and is a date.
Thanks heaps
Jason
2 Answers
2
You can use plot_date
:
plot_date
plot_date(df.index, df.data)
I would use `df.plot()', would be more "pandas-like".
– bmu
Dec 18 '12 at 20:03
@bmu trying to scatter plot with datetimes is not as straightforward as you might expect. You will end up with KeyErrors if you attempt to provide datetimes as the x in
df.plot()
and you'll notice pd.Series.plot()
has no 'scatter' argument for kind
.– conner.xyz
Sep 21 '15 at 20:52
df.plot()
pd.Series.plot()
kind
Whilst, I guess technically not a scatter plot, you can use the pandas.plot
function with point markers drawn on and no lines.
pandas.plot
df.plot(marker='o', linewidth=0)
This then allows us to use all of the convenient pandas functionality you desire. e.g. plot two series and different scales, using a single function,
df.plot(marker='o', linewidth=0, secondary_y='y2')
The downside to this is that you lose some of the scatter functionality such as shading and sizing the markers differently.
Still, if your aim is a quick scatter plot, this might be the easiest route.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Great. I'd didnt realise it was as simple as passing df.index. I was having trouble because I was trying to use the column names but the index doesnt have a column name.
– user1911866
Dec 18 '12 at 11:39