Login
📚 Python for Introductory Statistics: Lab Workbook
Chapters ▾

Graphs

import pandas as pd
import mathplotlib.pyplot as plt

x = [x1, x2, x3, ...]
x_df = pd.DataFrame({'column_name',x})

CHOOSE A CODE FROM BELOW (Bar, Pie, Histogram etc)

plt.suptitle("Plot Title")
plt.xlabel("x-axis label")
plt.ylabel("y-axis label")
  1. Bar graph (raw data) x_df.groupby(['column_name']).size.plot(kind='bar')
  2. Pie Chart (raw data) x_df.groupby(['column_name']).size.plot(kind='pie', autopct='%1.1f%%')
  3. Bar graph (frequency table) x_df.plot.bar(x='column_name',y='frequency')
  4. Pie chart (frequency table)
x_df.set_index('column_name',inplace=True)   
x_df.plot.pie(y='frequency')
  1. Side by Side Bar (frequency table)
x_df.set_index('column_name',inplace=True)
x_df.plot.bar()
  1. Histogram
freq,bins,patches = plt.hist(x,bins=5,ec='k',range=[min(x),max(x)+1])
plt.xticks(bins)   # df['column_name']=x
  1. Scater Plot (x, y list given) plt.scatter(x,y)

Adapted from Python for Introductory Statistics: Student's Lab Workbook, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.