App: Visualization#

Matplotlib#

Matplotlib, a Python library, supports the creation of various types of visualizations in Python. It is commonly imported as:

import matplotlib.pyplot as plt

Scatterplot#

A scatter plot shows points on a graph using dots. Each dot represents a point’s location, given by its x and y coordinates.

  • These coordinates are like labels for the variables being compared.

  • For example, if we’re comparing a mouse’s weight and height, weight would go on the x-axis and height on the y-axis.

To create a scatetter plot:

  • Use plt.scatterplot()

  • (x_coordinate, y-coordinate) should be given for a single point

  • (x_coordinate sequence, y-coordinate sequence) should be given for multiple points

    • sequence can be a tuple, list, series

  • Using a semicolon prevents the return value from being printed.

    • In plotting, using a semicolon stops the resulting plot object from being printed.

  • For more information on plt.scatterplot() and its parameters, visit the following link.

# single point
plt.scatter(5,10);   # x=5, y=10
_images/b07ff145917dadf44d7cbace90c412eb6ceac22863cc532e88e5c11bbe39b89b.png
  • In the following code the points are: \((2,10), (4,3), (5,6)\).

# multiple points
plt.scatter((2,4,5),(10,3,6));  
_images/60d2676716567b94337680d98e3b706d3008e219cb955b7d8f5752643bf2b42f.png

Color#

The c or color parameter is used to change the colors of the markers.

  • ‘r’ can be used to represent ‘red’

  • ‘g’ can be used to represent ‘green’

  • ‘k’ can be used to represent ‘black’

plt.scatter((2,4,5),(10,3,6), c='red');    
_images/a207c607e545fde9c20860f5b383b93f1865fcf4f70adac2e9386f04dde5f9be.png
plt.scatter((2,4,5),(10,3,6), color='r');    
_images/a207c607e545fde9c20860f5b383b93f1865fcf4f70adac2e9386f04dde5f9be.png
  • A tuple of colors can be provided to assign each marker a different color.

plt.scatter((2,4,5),(10,3,6), color=('navy','k','r'));
_images/8e0ac64c22f05b8835ed360ad92d4fa9000c227fd2d84977352c8f461357140b.png

Size#

  • The s parameter is used to change the size of the markers.

plt.scatter((2,4,5),(10,3,6), s=300);  
_images/e738ec4dac0cdca0bb48adf24b0f0b0b21c474f1d0145045a117a7ce82933d07.png
plt.scatter((2,4,5),(10,3,6), s=300, c='r');  
_images/40d4de2ccb84a737693d2bb2e81d6ed1d78df6301608895a0686aa65c8873f0a.png
  • A tuple of sizes can be provided to assign each marker a different size.

plt.scatter((2,4,5),(10,3,6), s=(10, 100, 1000));
_images/113880734521587b48a45392da51145670ef18da4acb75c30ba09687403f6834.png

Marker#

The marker parameter is used to change the style of the markers. For more marker styles visit the following link.

plt.scatter((2,4,5),(10,3,6), marker='+');
_images/292c10c07e5b0cccf95877b78dd85493902594c7513edc7fee02a450e81243cf.png
# H: hexagon
plt.scatter((2,4,5),(10,3,6), marker='H', s=200);
_images/b4897cc822029fcc4b9470d9f3321fb70c1b9cf1a170db9554202d6135c9cedd.png

Transparancy#

The alpha parameter, ranging from 0 (transparent) to 1 (opaque), is used to adjust the transparency of the markers.

# H: hexagon
plt.scatter((2,4,5),(10,3,6), alpha=0.2, s=200);
_images/1c2dd73c787e83a1eea22f9411aaff50541ea3f5d7e121492cc7bcb3a490bbf2.png
plt.scatter((2,4,5),(10,3,6), alpha=(0.1, 0.4, 0.8), s=200);
_images/221e8dcae5dc55ae52763f8ea4fbe68851bad554cdaef79f3401e83af5d2455f.png

Chart Elements#

Title#

plt.title() is used to include a title in a figure.

plt.title('Three Red Points')
plt.scatter((2,4,5),(10,3,6), c='r');
_images/fb52e3eb705477991007c2fd9986fbbb8a8973680e67e4f46d552d125f594e6b.png
  • The parameters fontsize, c, family, and loc are used to adjust the properties of the title.

    • c: color

    • loc: location (‘center’, ‘left’, ‘right’)

    • family: font

plt.title('Three Red Points', fontsize=30, c='navy', family='fantasy', loc='left')
plt.scatter((2,4,5),(10,3,6), c='r');
_images/d2ed7b359fa6d6e24d1e8abc3b24fa6cb57fbdccda3c9ce1bd174cd0b6f51b89.png

Axis Labels#

plt.xlabel() and plt.ylabel() are used to add axis labels to the figure.

plt.xlabel('Weight')
plt.ylabel('Height')
plt.scatter((2,4,5),(10,3,6), c='red');    
_images/35262f1e594bb7c2b09edfd607cdafa8180bbea1e2c2f9e6ce2080e582ed173d.png

Axis Ticks#

The tick locations and labels can be modified using plt.xticks() and plt.yticks().

plt.scatter([2,4,5],[10,3,6])
plt.xticks([2,4,5], ['Washington', 'Alabama','Virginia']);
_images/bbc7486fd9cbeec818f532db875dcfaf0cec63cc3e1e839ac51fe1406a5c7c33.png
# rotation
plt.scatter([2,4,5],[10,3,6])
plt.xticks([2,4,5], ['Washington', 'Alabama','Virginia'], rotation=90);
_images/ae5fcdb15a30a9dfea14e195fad0006e50d68932a48a2df19775f6eb6b3a54b1.png

Remove Axis#

plt.axis(‘off’) is used to remove axis.

plt.scatter((2,3,4,5,6),(1,2,5,2,3), c='red', s=100)
plt.axis('off');    
_images/d79c0f1d70edddf5e3f0d858a9a74ba965f39a294e69ef6eb33cdb3f3bcd8712.png

Grids#

plt.grid() is used to add horizontal and vertical gridlines to the plot.

  • axis parameter is used to achoose the gridline types.

    • Only vertical gridlines: axis = ‘x’.

    • Only horizontal gridlines: axis = ‘y’.

plt.scatter([2,4,5],[10,3,6])
plt.grid()
_images/ac5b28a2e27e0a35120cd0bb855d06c4142452c9f2601ef785f731bdcccc1395.png
plt.scatter([2,4,5],[10,3,6])
plt.grid(axis='x')
_images/3331b97ea5a250cf83ccc20e2cde0930b3576fc11ed01b816466719ce5420f6a.png
plt.scatter([2,4,5],[10,3,6])
plt.grid(axis='y')
_images/dc073f308662b12ead104869cab5b471e2afbf1b12fb2edb3388593a22464a2a.png

Text#

plt.text() is used to insert a text into a figure.

  • It is used in the form of plt.text(x, y, text).

  • (x, y) denotes the starting point of the text.

plt.scatter([2,4,5],[10,3,6])
plt.text(4.05,3,'Alabama');   # starts from the point (4.05,3)
_images/925df683e418cb910020d4d5531f8f3265ad4ba57f29d1681d42bb1c6675991f.png

Subplot#

plt.subplot() is used to create multiple plots within a single figure. Before the code for each subplot, plt.subplot() is added with the total number of rows and columns, as well as the order of the subplot. The order of the subplot starts from 1.

  • In the code below the figure has 1 row and 3 columns so in total there are 3 horizontal plots.

    • 1st plot: plt.subplot(1,3,1)

    • 2nd plot: plt.subplot(1,3,2)

    • 3rd plot: plt.subplot(1,3,3)

# number of rows: 1
# number of columns: 3

plt.figure(figsize=(20,5))
plt.subplot(1,3,1)                   # 1st plot
plt.scatter([2,4],[8,7],c='r')
plt.subplot(1,3,2)                   # 2nd plot
plt.scatter([1,2],[5,12],c='b')
plt.subplot(1,3,3)                   # 3rd plot
plt.scatter([6,10],[1,2],c='g');
_images/4fd813ff368fa17fab7069bf9398480246d7d31ef9ac062d7c82b5f8d3192c9d.png

Colormap#

You can choose a marker color by using a scalar or sequence of numbers, which will be mapped to colors using cmap. The following link provides access to various colormaps.

The default color map is viridis which ranges between blue and yellow.

  • The largest number, 10, corresponds to yellow.

  • 5 correspons to green.

  • The smallest number, 1, corresponds to (dark) blue

plt.scatter([1,2,3], [3,5,7], c=[1,5,10], s=500);
_images/984e83e2d89675a3d347f4d9a0bc3f03d65319529c28eee27a5349f4715cac1b.png

cmap parameter is used to change the colormap.For the gray colormap:

  • The largest number, 10, corresponds to white.

  • 5 corresponds to a gray color.

  • The smallest number, 1, corresponds to black

plt.scatter([1,2,3], [3,5,7], c=[1,5,10], s=500, cmap='gray');
_images/f2dec09425a5dbbf80f8a65ecbc6e247fcf4c41f65900c59437ddde2440ee554.png

Lineplot#

plt.plot() is used to generate line plots.

  • The x and y coordinates of the points are provided similarly to plt.scatter().

  • The points are connected by line segments.

  • If x coordinates are not specified, plt.plot() uses default x values: 0, 1, 2, and so on.

  • However, for plt.scatter(), x coordinates must be explicitly provided.

The following lineplot connects (1,7) and (3,10).

plt.plot((1,3),(7,10)); 
_images/b91af389c0efaf354118a8a70be4d8eefdefb468fb884636fbf15a08dca0d64c.png

The following lineplot connects (1,7), (2,5) and (3,10).

plt.plot((1,2,3),(7,5,10)); 
_images/ba4d8cf1a25b61cc6e24ebc598574a8ddd6ea25a8ac3606ed6a9d12262585357.png

Marker#

The marker parameter is used to specify the style of the markers.

plt.plot((1,2,3),(7,5,10), marker='o'); 
_images/37e082184ab198cbc03ce9ed1a04e25dae01ed33dabcab85d68b69f1bcc65911.png

Markersize#

The markersize parameter is used to adjust the size of the marker.

plt.plot((1,2,3,4,5,6),(7,5,10,2,7,1), marker='*', markersize='20'); 
_images/f5b7c66678d4f21c754c12773c5da00c39574413aa5a68c6fac6a82011587cd9.png

Linewidth#

The linewidth parameter is used to adjust the thickness of the line segments.

plt.plot((1,2,3,4,5,6),(7,5,10,2,7,1), linewidth=5); 
_images/0c1baa303470fec8c2650b245d96f9d9822ce09bc9a2d76c83313346cfb75e3a.png

Linestyle#

The linestyle parameter is used to adjust the style of the line segments.

  • Choices: ‘-’, ‘–’, ‘-.’, ‘:’, ‘None’, ‘ ‘, ‘’, ‘solid’, ‘dashed’, ‘dashdot’, ‘dotted’

plt.plot((1,2,3,4,5,6),(7,5,10,2,7,1), linestyle='dashed'); 
_images/660b406392e60beace955cf9fc6837fb3e1a22261019439b9c88f5e001908f0f.png
plt.plot((1,2,3,4,5,6),(7,5,10,2,7,1), linestyle='dotted'); 
_images/40d40dc8772f37f45d3a4d470aefde0315352110eaabeb7cf310fe544e0f3345.png
plt.plot((1,2,3,4,5,6),(7,5,10,2,7,1), linestyle='dashdot'); 
_images/c9e8b54aed570e52a8fe4c81d15000136419ff3f55fd733d35c9b2422487e503.png

Color#

The c or color parameter is used to adjust the color of the line segments and markers.

plt.plot((1,2,3,4,5,6),(7,5,10,2,7,1), c='r'); 
_images/2201ca46d897b234d389e4608834a2f528c92cc92f7fba7ce7821f371b7179c2.png
plt.plot((1,2,3,4,5,6),(7,5,10,2,7,1), marker='*', color='r'); 
_images/19d45d4742bb2684d631da0b830fd4db4f93aeb09e8531b12deaa358937df95c.png

Barplot#

plt.bar() is used to generate bar plots.

  • A bar plot represents values corresponding to categories as vertical bars.

  • The categories and their values are specified by the first and second arguments of the plt.bar() function, respectively.

In the following code, categories are represented by the expenses tuple, and the corresponding values are represented by the cost tuple.

  • The color parameter is used to change the colors of the bars.

expenses = ('housing', 'tuition', 'transportation', 'supplies', 'food')
cost = (700, 1000, 200, 100, 500)
color_set = ('y', 'r', 'g', 'orange', 'navy')

plt.bar(expenses, cost, color=color_set);
_images/6569f08b4eda35c95dfdd6ea7277edf1e3846fca7dad6233a5e0e1316d2ad5a5.png

Functions#

You can plot a function by generating x coordinates using np.linspace() and calculating corresponding y coordinates using functions from the numpy module.

  • np.linspace(start, stop, num) returns num evenly spaced numbers between start and end.

Square#

The following code generates 10 evenly spaced numbers between -1 and 1 and calculates their square values.

import numpy as np

x = np.linspace(-1, 1, 5) 
y = x**2

plt.plot(x,y);
_images/0adc409f56f96d94a8e793eaa33c40b9cde324a5ece530d96668a9e27aa5f3bc.png

In the graph above, we can recognize the individual line segments. Now, let’s increase the number of points.

import numpy as np

x = np.linspace(-1, 1, 100) 
y = x**2

plt.plot(x,y);
_images/43a3375c5199ac17c1f9f987cb9237ee592ddb10bd4516e6af2c221b60899c70.png
  • The graph appears as a curve, although it’s actually composed of line segments.

  • However, these segments are small, so when combined, they resemble a curve.

  • As evident from the plot below, the line segments are quite small.

import numpy as np

x = np.linspace(-1, 1, 100) 
y = x**2

plt.plot(x,y, marker='o');
_images/ea23176586758e969dd220703c1219bd3ec0e345480a8880306159815c812f15.png

sin(x)#

The following code generates 100 evenly spaced numbers between 0 and 10 and calculates their corresponding sine values.

import math
import numpy as np

x = np.linspace(0, 30, 100) 
y = np.sin(x)

plt.plot(x,y);
_images/cd1f61c98e6851996eafe737e3e324d2952693e81b150bcf4ba68780e285f188.png

Polynomials#

The following code generates 100 evenly spaced numbers between -4 and 5 and calculates their corresponding polynomial values.

  • The polynomial is \(x^3-2x^2-3x\)

import numpy as np

x = np.linspace(-4, 5, 100) 
y = x**3-2*x**2-3*x

plt.plot(x,y);
_images/f53152cbe5d1eeeea0ff3933775669bfd38689811ba59641eb5f894c3db7ff38.png

Squareroot#

import numpy as np

x = np.linspace(0, 10, 100) 
y = np.sqrt(x)

plt.plot(x,y);
_images/9c33a8c6790786c3d14ce6534d0029791941cd24be124220f86e3b31a21c4015.png

Exponential#

import numpy as np

x = np.linspace(-2, 10, 100) 
y = np.exp(x)

plt.plot(x,y);
_images/47a98089d6aa60be72c91702804724605d8af3acddaa66228955cd71ceffda89.png