6

How to make a basic scatter plot of column in a DataFrame vs the index of that DataFrame? Im using python 2.7.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

dataframe['Col'].plot()
plt.show()

This shows a line chart of 'Col' plotted against the values in my DataFrame index (dates in this case).

But how do I plot a scatterplot rather than a line chart?

I tried

plt.scatter(dataframe['Col'])
plt.show()

But scatter() requires 2 arguments. So how do I pass the series dataframe['Col'] and my dataframe index into scatter() ?

I for this I tried

plt.scatter(dataframe.index.values, dataframe['Col'])
plt.show()

But chart is blank.

1
  • That works for me... can you include dataframe.head() and dataframe.describe() in your question? Commented Dec 6, 2013 at 22:44

2 Answers 2

9

If you just want to change from lines to points (and not really want/need to use matplotlib.scatter) you can simply set the style:

In [6]: df=  pd.DataFrame({'Col': np.random.uniform(size=1000)})

In [7]: df['Col'].plot(style='.')
Out[7]: <matplotlib.axes.AxesSubplot at 0x4c3bb10>

scatter_example

See the docs of DataFrame.plot and the general plotting documentation.

Sign up to request clarification or add additional context in comments.

2 Comments

How are you getting that color theme? When I'm executing the same commands, it looks like the regular old and boring matplotlib type of chart.
Just use pd.options.display.mpl_style = 'default' before you plot or use your own matplotlibrc
7

Strange. That ought to work.

Running this

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

dataframe = pd.DataFrame({'Col': np.random.uniform(size=1000)})
plt.scatter(dataframe.index, dataframe['Col'])

spits out something like this

enter image description here

Maybe quit() and fire up a new session?

1 Comment

How are you getting that color theme? When I'm executing the same commands, it looks like the regular old and boring matplotlib type of chart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.