How to create a scatter plot using two columns of a dataframe with pandas in python ?


Example of how to create a scatter plot using two columns of a dataframe with pandas in python:

Create a scatter plot with pandas: example 1

Lets create a dataframe using pandas

import pandas as pd
import matplotlib.pyplot as plt

data = {'c':['a','b','c','d','e','f','g','h','i','f'], 
        'x':[0,1,2,3,4,5,6,7,8,9],
        'y':[0,0,0,0,0,0,0,0,0,0]}

data['y'] = [i* 2.0 + 1.0 for i in  data['x'] ]

df = pd.DataFrame(data)

print(df)

returns

   c  x     y
0  a  0   1.0
1  b  1   3.0
2  c  2   5.0
3  d  3   7.0
4  e  4   9.0
5  f  5  11.0
6  g  6  13.0
7  h  7  15.0
8  i  8  17.0
9  f  9  19.0

and create a simple scatter plot using pandas (see pandas.DataFrame.plot)

df.plot(x='x', y='y', style='o')

plt.savefig("pandas_scatter_plot_01.png", bbox_inches='tight', dpi=100)

How to create a scatter plot using two columns of a dataframe with pandas in python ?
How to create a scatter plot using two columns of a dataframe with pandas in python ?

Create a scatter plot with pandas: example 2

Now lets improve the plot a little bit

ax = df.plot(x='x', y='y', style='o', legend=False)

ax.set_xlabel("x label")
ax.set_ylabel("y label")

ax.set_title("Create a scatter plot with pandas")

ax.set_xlim(0,10)
ax.set_ylim(0,20)

ax.grid()

plt.savefig("pandas_scatter_plot_02.png", bbox_inches='tight', dpi=100)

How to create a scatter plot using two columns of a dataframe with pandas in python ?
How to create a scatter plot using two columns of a dataframe with pandas in python ?

Image

of