Python for Introductory StatisticsXYZ Homework Edition

⇩ Download ▾

Graphs

import pandas as pd
import matplotlib.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. Scatter Plot (x, y list given) plt.scatter(x,y)

Adapted from Python for Introductory Statistics, 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.

These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or any other accessibility standard. Every page is built against an automated accessibility gate, and our target for the published editions is WCAG 2.2 Level AA. If something is unusable, please tell us.