CIT228 - Advanced Database Systems

 Data Visualization


I. Overview

Chapter 15 covers how to plot data and create charts in Python using Matplotlib and Plotly. 

Examples from the chapter can be downloaded using the following link: Chapter_15.zip

For the assignment, you ONLY need to complete the hands-on exercises for your assignment (you don't need to complete all the small exercises within each chapter, although it is very good practice).

To see a lecture/demo on Matplotlib and Pyplot, view:  https://youtu.be/x2TJF1thjc4

To see a lecture/demo on Plotly, view: https://youtu.be/tue9gpUBC6k

To see a lecture/demo on instructions for hands on 1 through 10, view: https://youtu.be/LbVmfjhV8_w


II  Plotting Data using Matplotlib and Pyplot

A.  Matplotlib Overview

Matplotlib is a cross-platform library for making 2D plots from data in arrays.  It has a procedural interface named Pylab, which is designed to resemble MATLAB ( a programming language used for mathematical and technical computing favored by engineers). 

Matplotlib uses a numerical mathematic extension called NumPy which is designed to work with arrays. 

Matplotlib is the open source equivalent of MATLAB.

We will be using Matplotlib.pyplot which contains a collection of functions that will let us plot data and create a wide variety of charts.

B  Installing Matplotlib and using Pyplot

Matplotlib needs to be installed using the command prompt

python -m pip install --user matplotlib

Example:

C. Importing Pyplot

To plot data, you will need to import Matplotlib.pyplot.  It is standard practice to give pyplot an alias of plt when you import the module

Syntax: 

import matplotlib.pyplot as plt

Once the module is imported, you can use it's functions

D. Pyplot functions

A.  plot() function

The plot() function is used to draw markers in a diagram.  By default, the plot() function draws a line from point to point.

Syntax:  plt.plot(xpoints,ypoints) or plt.plot(xpoints) or plt.plot(ypoints)

Where xpoints represent an array with x-axis points and ypoints represents an array with y-axis points

B.  show() function

The show() function is used to display all figures

Syntax:  plt.show()

With the plot and show functions, you can create a simple chart

Example plotting both x and y points:

import matplotlib.pyplot as plt

xpoints=[1,10,20,30,40]
ypoints=[2,20,40,60,80]

plt.plot(xpoints,ypoints)
plt.show()

When you run the program, you will probably see a message in the terminal window indicating Matplotlib is building the font cache and that it may take a moment.  Eventually you should see a second window display with the chart

Sample Output

Code Example only plotting xpoints:

import matplotlib.pyplot as plt

xpoints=[1,10,20,30,40]

plt.plot(xpoints)
plt.show()

C. xlabel(), ylabel() and title() functions

You can add a chart title, x-axis title and y-axis title

Syntax: 

plt.title("chart title", optional parameters)

plt.xlabel("axis title", optional parameters)

plt.ylabel("axis title", optional parameters)

Optional parameters include:

Example:

plt.title("Cubed Numbers",color="green",fontfamily="Comic Sans MS", fontsize="20")

 

Example:

D.  grid() function

The grid() function can be used to add gridlines to the chart.

Syntax:  plt.grid()

Example:


Hands On #1 - Creating a simple plot

1. Open the command prompt on your system and install Matplotlib  (see full example above)

python -m pip install --user matplotlib

2.  Create a Chapter15 folder inside your CIT228 folder

3.  Create a new file named cubes.py

4. Create a program that cubes the numbers 1 through 5 and charts the values.  Your code should be similar to the example below, but you are cubing the numbers instead of squaring the numbers

import matplotlib.pyplot as plt

squared=[]
inputVal=[1,2,3,4,5]
for num in inputVal:
    squared.append(num*num) 
plt.plot(inputVal,squared)
plt.title("Squared Numbers")
plt.ylabel("Values Squared")
plt.xlabel("Input Values")
plt.grid()
plt.show()

 

5.  Run the program to produce the following chart:

 

 


E.  Subplots() function

The subplots() function let you draw multiple plots in a single figure and they give you control over the layout. 

Syntax:  plt.subplot(#rows,#cols,currentPlot#)

The values for rows, columns and currentPlot depend on how many graphs you are going to create and how you are going to organize them.

Example showing 2 graphs laid out in a single row:

import matplotlib.pyplot as plt

# plot 1
caloriesBurned=[500,450,550,650,250,750]
# subplot is 1 row, 2 columns, first column
plt.subplot(1,2,1)
plt.plot(caloriesBurned)
plt.grid()
plt.title("Calories Burned")
plt.ylabel("Calories")
plt.xlabel("Days")

#plot 2
caloriesConsumed=[1200,1500,1600,1100,1450,1200]
# subplot is 1 row, 2 columns, second column
plt.subplot(1,2,2)
plt.plot(caloriesConsumed)
plt.grid()
plt.title("Calories Consumed")
plt.ylabel("Calories")
plt.xlabel("Days")

plt.show()

F.  suptitle() function

If you are using subplots() and you have multiple charts, you can add a title for the figure using the suptitle() function

The function uses the same optional parameters as the title and axis titles (fontfamily, fontsize, color)

Example:

G.  Axis functions and methods

There are several functions and methods that allow customization of the x and y axis and the tick marks on the axes.

1.  tick_params() function

Arguments are used to change the appearance of tics and labels on the x and/or y axis

Syntax:  plt.tick_params(arguments)

Where some of the arguments could be:

2.  axis() method

Sets limits for the x and y axis

Syntax:  plt.axis([minX,maxX,minY,maxY])

Example using tick_params and axis()

import matplotlib.pyplot as plt  
    
inputValues = [*range(1,100,3)]
print(inputValues)
powOutput = []
for num in inputValues:
    powOutput.append(num*num)

plt.bar(inputValues,powOutput, color="hotpink",width=1.50)
plt.axis([10,100,100,8000])  
plt.tick_params(axis='both',which='major',labelsize=10, color="lightgreen", width=5, length=10, pad=10)
plt.ylabel("Output") 
plt.xlabel("Input") 
plt.title("Values Raised to the 2nd Power") 
plt.show() 

Sample Output

H.  Saving plots automatically with savefig()

When you run your program and display the plot, you can manually save it by selecting the save icon in the plot window. 

Pyplot includes an automatic way to save your plots that is easy to use.

Syntax:  plt.savefig('filename.png', arguments)

Popular arguments include:

By default, the graphic will be saved to the same directory as your program using whatever filename you specify. 


Hands On #2 - Creating subplots

1. Modify cubes.py to create 2 charts with a title for the figure as shown below

2.  You will need to add code for a second chart that raises numbers to the second power.

3. You need to modify your current code to match the example below:

for num in inputVal:
    cubed.append(num*num*num) 
ax1 = plt.subplot(1,2,1)    
ax1.plot(inputVal,cubed)

NOTE:  We need to use plt.subplot because we are creating more than 1 plot within a single figure.

3. Above the plt.show() command, and below the revised code for the cubed chart, add code to create the Numbers Raised chart.   

a)  Duplicate the code for the cubed chart and change the cubed variable to pow
b) In the for loop, use list2.append(num**2) for the equation  (NOTE: list2 is whatever you are calling the list that will store the values raised to the second power)
c) For the subplot code, use ax2 = plt.subplot(1,2,2) - this represents, 1 row, 2 columns, second column chart
d) To plot the chart, use ax2.plot(inputVal,list2)
e) Add the titles as shown in the example

4. The last line of the code should be plt.show()   This command is only called 1 time.   To add a title to the figure, right above that line, code  plt.suptitle("Fun with Numbers")

5.  Run the program.


E. Pyplot Arguments

There are several plot() function arguments you can use that let you customize how the markers and lines look. 

1.  Marker Argument

You can customize the markers by adding the marker argument to the plot() function

Syntax:  plt.plot(xpoints,ypoints,marker='type')

Where marker type can be:

Marker Description
'o' Circle
'*' Star
'.' Point
',' Pixel
'x' X
'X' X (filled)  
'+' Plus
'P' Plus (filled)
's' Square
'D' Diamond
'd' Diamond (thin)
'p' Pentagon
'H' Hexagon
'h' Hexagon
'v' Triangle Down
'^' Triangle Up
'<' Triangle Left
'>' Triangle Right
'1' Tri Down
'2' Tri Up
'3' Tri Left
'4' Tri Right
'|' Vline
'_' Hline

Example:

2.  Line Argument

You can customize the line style by adding the ls argument to the plot() function

Syntax:  plt.plot(xpoints,ypoints,ls='type')

Where line style type can be:

Style Or
'solid' (default) '-'
'dotted' ':'
'dashed' '--'
'dashdot' '-.'
'None' '' or ' '

Example:

3.  Color Argument

You can customize the line color by adding the color argument to the plot() function.

Syntax:  plt.plot(xpoints,ypoints,c='type')

Where color type can be hexidecimal color values or color names

Color Name Example:

Hexidecimal Color Example:

4.  Linewidth Argument

You can customize the thickness of the line using the linewidth argument.

Syntax:  plt.plot(xpoints,ypoints,lw='size')

Where size is a floating point value enclosed in quotes

Example:

 

Hands On #3 - Customizing Markers and Lines

1. Modify cubes.py by customizing the markers and lines in one of the charts.  Add a marker, color, linewidth and linestyle to the chart.

2.  Modify the figure title by adding a fontsize, fontfamily and color

Example:

NOTE:  To make more space between the title and plots, you can use subplots_adjust(top="top of chart", bottom="bottom of chart", hspace="vertical space between charts", wspace="horizontal space between plots")

In the example above, to put more space between the title and charts AND to put space between each chart, I could add the following code

plt.suptitle("Fun with Numbers",c="green",fontfamily="Comic Sans MS", fontsize="20")
plt.subplots_adjust(top=.8,wspace=1)
plt.show()

Sample Output after the change:


F.  PyPlot Builtin Styles

Matplotlib has several builtin styles you can apply to your charts.  To see what you have available, open the command prompt and enter the following:

C:\Users\Lisa.LAPTOP>python
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> plt.style.available
['Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10']
>>>

To apply one of the styles listed, use the following syntax:

plt.style.use("styleName")
plt.plot(input,output)

All the arguments for styling the plots can be used with the builtin styles

Example:

ax6=plt.subplot(2,3,6)     
plt.style.use("tableau-colorblind10")    
ax6.plot(inputVal,squared, c="hotpink",lw="2")
plt.grid()
plt.title("tableau-colorblind10")

Example shows 6 plots with different styles applied:

 


Hands On #4 - Applying builtin styles

1. Modify cubes.py by applying a builtin style to the chart you haven't modified  (if you modified both with arguments, that is OK, you can still apply one of the builtin styles)

2.  After applying the builtin style, feel free to modify the color, linewidth, line style and marker to whatever you want.

Example:

Code modified for the example is shown below:

plt.style.use("seaborn-poster")
ax2.plot(inputVal,pow,color="goldenrod",lw="2",marker="^")
plt.title("Numbers Raised", color="goldenrod")  

 


G.  Different types of charts

You can create 2D and 3D charts using Matplotlib.

2D charts include:

3D charts include:

We will cover several of the different chart types.  If you are interested in seeing information on all the charts and the 3D charts, see: https://www.tutorialspoint.com/Matplotlib/index.htm

 

1.  Scatter() function

Scatter plots are used to plot data points on horizontal and vertical axis in the attempt to show how much one variable is affected by another. There is typically some type of range you are plotting against to observe trends ore relatonships in the data. The dots in a scatter plot report individual data points AND patterns when the data are taken as a whole.

Syntax: 

plt.scatter(list#1, numericRange,arguments)
plt.scatter(list#2,numericRange,arguments)
plt.scatter(list#n, numericRange,arguments)

Where arguments can be color, markers, linewidth, label size, etc.

Example:  In the example below, pre-test scores are plotted against a set range for the scores.  Post-test scores are also plotted against the same set range for scores so a comparison between the two exams can be made and conclusions about the data can be drawn.

import matplotlib.pyplot as plt
pretest_scores = [55, 49, 30, 66, 25, 30, 45, 50, 60, 34]
posttest_scores = [99, 89, 88, 78, 100, 88, 68, 65, 99, 88]
score_range = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
plt .scatter(pretest_scores, score_range, color='green',label="pretest scores")
plt .scatter(posttest_scores,score_range, color='darkblue', label="posttest scores")
plt.ylabel('Points')
plt.xlabel('Test Results')
plt.title('Pre and Post Test Scores')
plt.legend(loc='lower center', ncol=2, fontsize=8)
plt.grid()
plt.show()

Sample Chart:

A few things to note about the code:

1.  Instead of using plt.plot, we used plt.scatter

2.  Since there is more than 1 series of data in the chart, a legend was added

a.  Adding a legend

Anytime you have more than 1 series of data, you should display a legend.  There are several different techniques you can use.

The easiest way to display a legend is to:

a)   add label="legend text"  to the arguments passed to scatter (or plot )

plt .scatter(pretest_scores, score_range, color='green',label="pretest scores")
plt .scatter(posttest_scores,score_range, color='darkblue', label="posttest scores")

b)  include plt.legend(loc="location for legend", ncol=columnsDisplayedInLegened, fontsize=#)

Location for legend can be: 

Example:

plt.legend(loc='lower center,  ncol=2, fontsize=8)

NOTE:  If you do not include labels in plt.scatter or plt.plot, then you need to include more information in the plt.legend arguments.

For more information on using the legend, see: https://Matplotlib.org/3.1.1/api/legend_api.html

b.  Size Argument

The size argument is used in scatter plots to increase the size of the dots

Syntax:  plt.scatter(values,range,s=#)

Example:  In the example below, the green dots have a size of 200 and the dark blue have a "normal" size.

 

 

2.  Scatter() function in 3D

To make the scatter plot 3D, you need to provide a set of Z values and you need to use plt.subplot to indicate you want a 3D projection.   The Z values will need to be included in the arguments you send to the scatter function

Syntax: 
ax = plt.subplot(project='3d')
ax.scatter(list#1,range,zvalue,arguments)
ax.scatter(list#2, range,z-value, arguments)
ax.scatter(list#n, range,z-value, arguments)

Example:   In the example below, Z-values are set to 0.   The variable ax contains plt.subplot with the 3D projection setting.  The ax variable was used to create both scatter plots and the Z values of zero were added after the scores.   The legend was adjusted to use the "upper center" location.

import matplotlib.pyplot as plt
pretest_scores = [55, 49, 30, 66, 25, 30, 45, 50, 60, 34]
posttest_scores = [99, 89, 88, 78, 100, 88, 68, 65, 99, 88]
score_range = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
pretestWeek=0
posttestWeek=0
ax = plt.subplot(projection='3d')
ax .scatter(pretest_scores, score_range, pretestWeek,color='green',label="pretest scores")
ax .scatter(posttest_scores, score_range, posttestWeek, color='darkblue', label="posttest scores")
plt.ylabel('Points')
plt.xlabel('Test Results')
plt.title('Pre and Post Test Scores')
plt.legend(loc='upper center', ncol=2, fontsize=8)
plt.show()

Sample Output:


Hands On #5 - Creating a Scatter Plot

1.  Download scatter.py into your Chapter15 folder

2.  Temperature data has been provided for 4 months along with the day the temps were recorded on. 

3.  You need to create scatter plots for each month.  You should include

a)  a color and label identifying what each plot represents

b)  xlabel

c)  ylabel

d)  legend

e)  title

Example of a completed scatter plot:


3.  Bar() function

Bar plots (or charts)  are typically used to make comparisons.  You can plot 1 series of data points (or bars) or multiple series.   In Matplotlib, plotting multiple series is a bit more complicated, so we will start with a single series first

A.  Plotting a single series

Syntax for a vertical chart: plt.bar(x_labels,data,arguments)

Where arguments are typically color and width of the bar.

Example:

import matplotlib.pyplot as plt  
    
revenue = [200,300,400,350]
periods=["Q1","Q2","Q3","Q4"]

plt.bar(periods, revenue, color ='darkgreen', width = 0.5) 
  
plt.xlabel("Current Year") 
plt.ylabel("Revenue (Thousands)") 
plt.title("Bob's Bakery Revenue Report") 
plt.show() 

Sample Output:

 

Syntax for a horizontal chart:  plt.barh(x_labels,data,arguments)

Example:

import matplotlib.pyplot as plt  
    
revenue = [200,300,400,350]
periods=["Q1","Q2","Q3","Q4"]

plt.barh(periods, revenue, color ='darkgreen') 
  
plt.xlabel("Revenue (Thousands)") 
plt.ylabel("Current Year") 
plt.title("Bob's Bakery Revenue Report") 
plt.show() 

 

Sample Output:

B.  Plotting Multiple Series (Stacked Bar)

When plotting multiple series, a stacked bar chart is pretty straightforward and only requires adding another plt.bar() statement for the second series.

Vertical Syntax:

plt.bar(x_labels,dataSet1,arguments)
plt.bar(x_labels,dataSet2,arguments)

Example

import matplotlib.pyplot as plt  
    
revenue = [200,300,400,350]
expenses = [110,100,90,90]
periods=["Q1","Q2","Q3","Q4"]

# creating the bar plot 
plt.bar(periods, revenue, color ='darkgreen', width = 0.5, label="Revenues") 
plt.bar(periods,expenses, color="maroon", width = 0.5, label="Expenses")
  
plt.xlabel("Current Year") 
plt.ylabel("Thousands") 
plt.title("Bob's Bakery Balance Report") 
plt.legend(loc="best")
plt.show() 

NOTE:  labels were added to each plt.bar command so they could display in the legend.  The legend was added using plt.legend(loc="best")

Sample Output

Horizontal Syntax:

plt.barh(x_labels,dataSet1,arguments)
plt.barh(x_labels,dataSet2,arguments)

Example:

import matplotlib.pyplot as plt  
    
revenue = [200,300,400,350]
expenses = [110,100,90,90]
periods=["Q1","Q2","Q3","Q4"]

# creating the bar plot 
plt.barh(periods, revenue, color ='darkgreen', label="Revenues") 
plt.barh(periods,expenses, color="maroon",  label="Expenses")
  
plt.xlabel("Thousands") 
plt.ylabel("Current Year") 
plt.title("Bob's Bakery Balance Report") 
plt.legend(loc="best")
plt.show() 

 

NOTE:  labels were added to each plt.bar command so they could display in the legend.  The legend was added using plt.legend(loc="best")

Sample Output:

C.  Plotting Multiple Series (Clustered Bar)

For a clustered bar chart, you need to provide positioning for each bar on the x axis. 

This is done by calculating the space between the bars and adding in the width of the bars.

The arrange function is part of the NumPy module.   NumPy is used to make array processing in Python more efficient.  You will see it included in many Matplotlib applications because the data is stored in arrays.   to use arrange(), we need to import NumPy

Syntax to import NumPy:

import numpy as np

NOTE: It is standard practice to give NumPy an alias of np

Syntax to use arrange:

barPos1 = np.arrange(len(dataSeries1))

To position additional bars, you use the position of the previous set of bars

barPos2 = [x + barWidth for x in barPos1]
barPos3 = [x + barWidth for x in barPos2]

To position the category-x labels, you need to use plt.xticks()

Syntax:  plt.xticks([r + barWidth for r in range(len(dataSeries1))],xSeriesVariable)

Example showing how to position bars on the x axis and display the category x labels in a clustered bar chart:

import matplotlib.pyplot as plt  
import numpy as np 
    
revenue = [200,300,400,350]
fixed_expenses = [110,100,90,90]
variable_expenses=[10,20,20,25]
periods=["Q1","Q2","Q3","Q4"]
barWidth=.25

#position of bar on x axis
br1 = np.arange(len(periods)) 
br2 = [x + barWidth for x in br1] 
br3 = [x + barWidth for x in br2] 

plt.xticks([r + barWidth for r in range(len(age_range))],periods) 

# creating the bar plot 
plt.bar(br1, revenue, color ='darkgreen', width=barWidth, label="Revenues") 
plt.bar(br2,fixed_expenses, color="maroon",  width=barWidth, label="Fixed Expenses")
plt.bar(br3,variable_expenses, color="grey", width=barWidth,  label="Variable Expenses")
  
plt.ylabel("Thousands") 
plt.xlabel("Current Year") 
plt.title("Bob's Bakery Revenues & Expenses") 
plt.legend(loc="best")
plt.show() 

Sample Output:


Hands On #6 - Creating a multi-bar plot

1.  Download bar_charts.py and save the file into your Chapter15 folder.

2.  The lists for men, women, total and range have been filled in for you. The values represent fast food consumption per day broken down by age and sex.

3.  You need to add the following to create the chart:

a)  bar width

b) position of each bar on the x axis

c)  xticks

d)  plt.bar statements  (include a color, the bar width and a label for the legend)

e)  ylabel

f)  xlabel

g)  title

h)  legend

Example of a completed chart:


4.  Pie

Pie charts are used to plot 1 series of data to show how each wedge contributes to the whole.

You need to provide the labels, values and which series you want exploded (separated) from the pie.

Syntax:

plt.pie(values,explode=List,labels=List, autopct=format, shadow=True or False, startangle=#)

values are what you are planning on charting (the values should be in a list

explode should have a list with a 0 or decimal for each pie wedge. 0 means do NOT explode.  a decimal means explode (or separate) from the pie

labels should have a list of labels for each wedge that correspond to the values provided

autopct can displays the percentage of the whole each wedge occupies. The format is:  '%#.#f%%'   Where the first # is the whole number and .# is the number of decimal places you want displayed int he value.    NOTE:  By default, the values you chart are automatically converted to percentages and the wedges represent the percentages

shadow set to True will put a shadow at the bottom of the pie

startangle=# will rotate the pie to the position indicated by the startangle

Example:

import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Freshwater Fish', 'Cats', 'Dogs', 'Birds'
numPets = [142, 88.3, 74.8, 16]
explode = (.1, 0, 0, 0)  # only "explode" the 1st wedge

fig1, ax1 = plt.subplots()
ax1.pie(numPets, explode=explode, labels=labels, autopct='%3.1f%%', shadow=True, startangle=0)
ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
plt.suptitle("Most popular pets in America")

plt.show()

 

Sample Output:

Showing the values instead of the percentages in a pie chart, is not an easy task.  It requires creating a function to convert the percentages back into whole numbers  (you will lose the decimal portion of the original number, even if you format the value as a float, the original decimal portion was lost when it converted to a percentage for the pie wedges)

def autopct_format(values):
    def my_format(pct):
        total = sum(values)
        val = int(round(pct*total/100.0))
        return '{v:d}'.format(v=val)
    return my_format

The function is calling the format() function which is used to format numbers.   The format() function returns a formatted string based on the formatting type provided.

Syntax:

variable = "{:type}".format(valueToFormat)

Some of the more useful formatting types include:

"{:,}" - inserts commas as a thousands operator

"{:%}" - percentage format

"{:.2f}" - float format with 2 decimal places

"{:,.2f}" - float format with commas and 2 decimal places

For more information, see:  https://www.geeksforgeeks.org/python-format-function/

Programming Example:


import matplotlib.pyplot as plt

def autopct_format(values):
    def my_format(pct):
        total = sum(values)
        val = int(round(pct*total/100.0))
        return '{v:d}'.format(v=val)
    return my_format

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Freshwater Fish', 'Cats', 'Dogs', 'Birds'
numPets = [142, 88.3, 74.8, 16]
explode = (.1, 0, 0, 0)  # only "explode" the 1st wedge

fig1, ax1 = plt.subplots()

ax1.pie(numPets, explode=explode, labels=labels, autopct=autopct_format(numPets), shadow=True, startangle=0)
ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
plt.suptitle("Most popular pets in America")

plt.show()

 

Sample Output:

To apply different colors for the wedges of the pie, you need to create a tuple with the colors specified and then set the color argument to your tuple variable

Example:

import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Freshwater Fish', 'Cats', 'Dogs', 'Birds'
numPets = [142, 88.3, 74.8, 16]
explode = (.1, 0, 0, 0)  # only "explode" the 1st wedge
wedgeColors=('lightgreen','lightblue','lavender','lemonchiffon')

fig1, ax1 = plt.subplots()
ax1.pie(numPets, explode=explode, labels=labels, autopct='%3.1f%%', shadow=True, startangle=0, colors=wedgeColors)
ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
plt.suptitle("Most popular pets in America")

plt.show()

Sample Output


Hands On #7 - Creating a pie plot

1. Create a pie chart using the data shown in the table below:

Image Format # used at sites
PNG 376
JPEG 348
SVG 153
GIF 104
Other 19

 

2.  Make sure each wedge of the pie displays a percentage with 1 decimal place. 

3.  The pie should have a shadow applied to it and the data labels (text) should display next to each wedge.

4.  Explode one of the wedges in the pie and rotate the pie to a starting angle that you like.

5.  Give the chart a title (you decide on the title).

6.  Add custom colors to each wedge of the pie

Example of what your pie could look like (your start angle, exploded wedge, title, colors etc may look different because you can pick your own settings!)

 


5.  Histograms

Histograms provide a visual interpretation of numerical data by indicating the number of data points that lie within a range of values. These ranges of values are called classes or bins. The frequency of the data that falls in each class is depicted by the use of a bar. Each bar in a histogram reprsents a range of data points.

For histograms, you should try to have between 5 to 20 data intervals (or bins).  This requires grouping some data together.  If you look at the data, you should see natural groupings.  You just need to tell pyplot how many groupings (or bins) you need to use.

Syntax to create a histogram:   plt.hist (value, bins=#, arguments)

Where value represents the list of values you want plotted

bins is the number of groupings you want the values placed into

arguments typically involve selecting the color of the bars.

In the example below, I am rolling the dice 500 times and storing each value in a list.  Since the values on the dice can be 1 through 6, that provides a natural way to group data for the bins.

import matplotlib.pyplot as plt
import random

dice=[]
x=0
while x < 500:
    dice.append(random.randint(1,6))
    x+=1
plt.hist(dice, bins=6, color="green")
plt.ylabel("Frequency")
plt.xlabel("Dice")
plt.suptitle("Frequency of Dice Rolls")

plt.show()

 

Sample Output: 

 

Example #2 Distribution of weight values collected over 30 days.  When analyzed, the values logically fit into 9 different groupings (or bins). 

import matplotlib.pyplot as plt

weight=[135,137,136,137,138,139,140,139,137,140,142,146,148,145,139,140,
142,143,144,143,141,139,137,138,139,136,133,134,132,132]
plt.hist(weight, bins=9, color="hotpink")
plt.ylabel("Frequency")
plt.xlabel("Recorded Weight")
plt.suptitle("Weight Distribution over 30 Days")

plt.show()

 

Sample Output


Hands On 8 - Creating a histogram

1.  Download histogram.py  to your CIT228/Chapter15 folder

2.  Determine how many bins you need based on the data in the list  (HINT:  To figure out the bins, I would print out the sorted fitness_test)

3.  Create a histogram to visually display the data.

4.  Give the plot a title and label the x and y axis (you decide the text for the titles)


H.  Enhancing charts with colormaps

Matplotlib includes color gradients called colormaps that let you emphasize a pattern within the data using color. The list below includes some of the color names available for use:

Perceptually Uniform Sequential = ['viridis', 'plasma', 'inferno', 'magma']


Sequential = ['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds', 'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu', 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']


Sequential (2)= ['binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink', 'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia', 'hot', 'afmhot', 'gist_heat', 'copper']


Diverging= ['PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']


Qualitative= ['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c']


Miscellaneous= ['flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern', 'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'hsv', 'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar']

Colormaps work best with scatter plots because they are designed to help visualize patterns.  You can apply then to other plots, but not all the colormaps are available and they don't always display the way you think they will.

To use a colormap with a scatter plot, you need to connect it to each y value of data in your data set and you need to specify which colormap you are going to use.

This can be done in the scatter function

Syntax:  plt.scatter(xValues,yValues,c=yValues, cmap=plt.cm.Name)

c=yValues connects each y value to the colormap and cmap=plt.cm.Name specifies which colormap you want to use

Example:

 


Hands On #9 - Modifying a scatter plot by adding a colormap

1.  Open scatter.py and add a colormap to the plot.

2.  Comment out the legend (it won't display the colors properly)

3.  Add a Title and Subtitle using plt.suptitle for the title and plt.title for the subtitle

Example (you can use any of the colormaps you want, in the example, the summer temps are using the YlOrRd colormap and the winter temps are using the GnBu colormap


III.  Plotting Data using Plotly

Plotly lets you share and manipulate your plots online. 

A.  Installing Plotly

Plotly is free and open source.  It contains everything you needd to write figures to standalone HTML files

Installation is done through the command prompt using pip

python -m pip install --user plotly

Plotly version 4 is for offline use only.  Once you create the web page, you can transfer it to your site using ftp.

After you create a chart using Plotly, it will open and display in your web browser.

We will cover how to create several types of charts, but there are over 30 chart types available that you can see at: https://plotly.com/python/basic-charts/

B.  Creating and Customizing Figures

There are many flavors of Plotly.  Plotly express has a lot of documentation, but requires downloading and installing pandas (which we aren't going to do).

To create figures using Plotly, you need to use a dictionary object or a graph object.  We will be using a graph object because there are more features and ways to customize the graphic.

1.  Import plotly

Syntax:  import plotly.graph_objects as go

2.  Create a figure object

The syntax varies depending upon what type of chart you are creating.  In general, these are the steps you need to follow:

a) use go.Figure to specify the type of chart and data

Pie Chart Example:

labels = 'Freshwater Fish', 'Cats', 'Dogs', 'Birds'
numPets = [142, 88.3, 74.8, 16]
colors=['lightgreen','lightblue','lavender','lemonchiffon']

fig = go.Figure(data=[go.Pie(labels=labels, values=numPets)])

b)  use fig.update_traces to control interactivity and display settings in graph

Pie Chart Example:

fig.update_traces(
    hoverinfo='label+percent',
    textinfo='value',
    textfont_size=20, 
    marker=dict(colors=colors,line=dict(color='black',width=2))
    )

fig.update_traces is used to customize the interactivity in the pie chart.   

Key points for the settings used above:

c)  use fig.update_layout to control the title and margins

Pie Chart Example:

fig.update_layout(
    title_text="Popular Pets in America",
    title_font_color="darkgreen", 
    title_font_size=30, 
    title_font_family="Raleway", 
    title_xref="paper", 
    title_yref="paper",
    margin_l=200,
    margin_r=200
    )

There are many settings you can use in update_layout.  To find additional arguments, see: https://plotly.com/python/reference/layout/

Key points for the settings used above:

d)  use fig.show() to display the chart in your default browser

For more information on using plotly.graph_objects, see: https://plotly.com/python-api-reference/generated/plotly.graph_objects.Figure.html

Example that creates a pie chart:

import plotly.graph_objects as go
import numpy as np

labels = 'Freshwater Fish', 'Cats', 'Dogs', 'Birds'
numPets = [142, 88.3, 74.8, 16]
colors=['lightgreen','lightblue','lavender','lemonchiffon']
fig = go.Figure(data=[go.Pie(labels=labels, values=numPets)])
fig.update_traces(
    hoverinfo='label+percent',
    textinfo='value',
    textfont_size=20, 
    marker=dict(colors=colors,line=dict(color='black',width=2))
    )
fig.update_layout(
    title_text="Popular Pets in America",
    title_font_color="darkgreen", 
    title_font_size=30, 
    title_font_family="Raleway", 
    title_xref="paper", 
    title_yref="paper",
    margin_l=200,
    margin_r=200
    )
fig.show()

Sample Output

 

Example that creates a two column bar chart:

import plotly.graph_objects as go
# Example adapted from https://plotly.com/python/bar-charts/#colored-and-styled-bar-chart, March 2021
years = [2000, 2001, 2002, 2003,2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012]
world = [180, 236, 207, 236, 263,350, 430, 474, 526, 488, 537, 500, 439]
chinaExport = [37, 43, 55, 56, 88, 105, 156, 270,299, 340, 403, 549, 499]

fig = go.Figure()
fig.add_trace(go.Bar(
    x=years,
    y=world,
    name='Rest of world',
    marker_color='rgb(247, 7, 127)'
))
fig.add_trace(go.Bar(
    x=years,
    y=chinaExport,
    name='China',
    marker_color='rgb(7, 87, 247)'
))

fig.update_layout(
    title='US Export of Plastic Scrap',
    title_font_color='darkblue',
    title_font_size=30,
    xaxis_tickfont_size=14,
    yaxis=dict(
        title='USD (millions)',
        titlefont_size=16,
        tickfont_size=14,
    ),
    legend=dict(
        x=0,
        y=1.0,
        bgcolor='rgba(255, 255, 255, 0)',
        bordercolor='rgba(255, 255, 255, 0)'
    ),
    barmode='group',
    bargap=0.15, # gap between bars of adjacent location coordinates.
    bargroupgap=0.1 # gap between bars of the same location coordinate.
)
fig.show()

To view the chart live, see:  bar.html

 

Example that creates a styled line plot

import plotly.graph_objects as go
# Retrieved from https://plotly.com/python/line-charts/#style-line-plots, March 2021

# Data
month = ['January', 'February', 'March', 'April', 'May', 'June', 'July','August', 'September', 'October', 'November', 'December']
high_2000 = [32.5, 37.6, 49.9, 53.0, 69.1, 75.4, 76.5, 76.6, 70.7, 60.6, 45.1, 29.3]
low_2000 = [13.8, 22.3, 32.5, 37.2, 49.9, 56.1, 57.7, 58.3, 51.2, 42.8, 31.6, 15.9]
high_2007 = [36.5, 26.6, 43.6, 52.3, 71.5, 81.4, 80.5, 82.2, 76.0, 67.3, 46.1, 35.0]
low_2007 = [23.6, 14.0, 27.0, 36.8, 47.6, 57.7, 58.9, 61.2, 53.3, 48.5, 31.0, 23.6]
high_2014 = [28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9]
low_2014 = [12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1]

fig = go.Figure()
# Create and style traces
fig.add_trace(go.Scatter(
    x=month, 
    y=high_2014, 
    name='High 2014',
    line=dict(color='firebrick', width=4)
))
fig.add_trace(go.Scatter(
    x=month, 
    y=low_2014, 
    name = 'Low 2014',
    line=dict(color='royalblue', width=4)
))
fig.add_trace(go.Scatter(
    x=month, 
    y=high_2007, 
    name='High 2007',
    line=dict(color='firebrick', width=4,dash='dash') # dash options include 'dash', 'dot', and 'dashdot'
))
fig.add_trace(go.Scatter(
    x=month, 
    y=low_2007, 
    name='Low 2007',
    line = dict(color='royalblue', width=4, dash='dash')
))
fig.add_trace(go.Scatter(
    x=month, 
    y=high_2000, 
    name='High 2000',
    line = dict(color='firebrick', width=4, dash='dot')
))
fig.add_trace(go.Scatter(
    x=month, 
    y=low_2000, 
    name='Low 2000',
    line=dict(color='royalblue', width=4, dash='dot')
))

# Edit the layout
fig.update_layout(
    title='Average High and Low Temperatures in New York',
    xaxis_title='Month',
    yaxis_title='Temperature (degrees F)'
)

fig.show()

To see the example live, view:  line.html

 

C. Saving the plot as a webpage

To save the plot as a webpage, you need to do 3 things:

1)  import plotly offline

2) store fig.update_layout in a variable

3)  Comment out fig.show() and add a line similar to the one below:

offline.plot({'data': fig, 'layout':layout}, filename='bar.html')

When you run the code, you will still see it display in the browser. BUT it will also create the specified html file in the directory you are running Python out of

Programming example:

import plotly.graph_objects as go
from plotly import offline
# Example adapted from https://plotly.com/python/bar-charts/#colored-and-styled-bar-chart, March 2021
years = [2000, 2001, 2002, 2003,2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012]
world = [180, 236, 207, 236, 263,350, 430, 474, 526, 488, 537, 500, 439]
chinaExport = [37, 43, 55, 56, 88, 105, 156, 270,299, 340, 403, 549, 499]

fig = go.Figure()
fig.add_trace(go.Bar(
    x=years,
    y=world,
    name='Rest of world',
    marker_color='rgb(247, 7, 127)'
))
fig.add_trace(go.Bar(
    x=years,
    y=chinaExport,
    name='China',
    marker_color='rgb(7, 87, 247)'
))

layout=fig.update_layout(
    title='US Export of Plastic Scrap',
    title_font_color='darkblue',
    title_font_size=30,
    xaxis_tickfont_size=14,
    yaxis=dict(
        title='USD (millions)',
        titlefont_size=16,
        tickfont_size=14,
    ),
    legend=dict(
        x=0,
        y=1.0,
        bgcolor='rgba(255, 255, 255, 0)',
        bordercolor='rgba(255, 255, 255, 0)'
    ),
    barmode='group',
    bargap=0.15, # gap between bars of adjacent location coordinates.
    bargroupgap=0.1 # gap between bars of the same location coordinate.
)
#fig.show()

offline.plot({'data': fig, 'layout':layout}, filename='bar.html')

NOTE:  I am running Python out of my CIT228 directory so that is where it saved bar.html to.


Hands On #10 - Using Plotly

1.  Create a new file named plotly chart.py

2.  Copy the code from the cubes, bars or scatter assignment files into plotly and modify it to display offline in the web browser.  Make sure you save the file created as plotly.html


Hands On #11 - Uploading to GitHub

1.  You should have the following files in your Chapter15 folder:

Exercise Assignment Files Points
Hands On 1,2,3 and 4 cubes.py 10 points
Hands On 5 and 9 scatter.py 10 points
Hands On 6 bars.py  10 points
Hands On 7 pie.py 10 points
Hands On 8 histogram.py 10 points
Hands On 10 plotly chart.py 10 points

2.  Upload Chapter 15 to GitHub.

a.  Click the ... next to CIT228 Git and select the Changes menu, Stage all Changes command.

b.  In the Message window below Source Control, enter Chapter15 and then click the ... next to CIT228 Git and select the Commit menu,  Commit Staged command

c.  Display the menu next to the CIT228 Git folder by selecting ...  and select the Push command

d.  This will put all files and folders inside your CIT228/Chapter15 directory into your GitHub repository

3.  Open a web browser and go to your CIT228 repository.  Make sure the Chapter15 directory and files are there, then copy the URL and paste it into the lab assignment dropbox (you are done with the first part of your lab assignment!)