Matplotlib Basic Scatter Plot From Pandas DataFrame

How to make a basic graph of the scatter of a column in a DataFrame with respect to the index of this DataFrame? Im using python 2.7.

import numpy as np import pandas as pd import matplotlib.pyplot as plt dataframe['Col'].plot() plt.show() 

Here's a Col line chart plotted against the values ​​in my DataFrame index (dates in this case).

But how do I build a scatter plot, not a line chart?

I tried

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

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

I tried for this

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

But the schedule is empty.

+6
source share
2 answers

It’s strange. That should work.

Launch 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 start a new session?

+6
source

If you just want to go from lines to dots (and don't want / need to use matplotlib.scatter), you can just 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 DataFrame.plot documents and general plotting documentation .

+8
source

Source: https://habr.com/ru/post/959502/


All Articles