{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# [Fundamental Python Data Science Libraries: A Cheatsheet (Part 3/4)](https://hackernoon.com/fundamental-python-data-science-libraries-a-cheatsheet-part-3-4-6c2aecc697a4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "by [Lauren Glass](https://www.linkedin.com/in/laurenjglass/), [Hackernoon](https://hackernoon.com/), Aug. 7, 2018\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matplotlib\n", "\n", "This library is the go-to Python visualization package! It allows you to create rich images displaying your data with Python code.\n", "\n", "This library is extensive, but this article will focus on two objects: the Figure and the Axes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load libraries\n", "\n", "import matplotlib.pyplot as plt\n", "#will lead to static images of your plot embedded in the notebook\n", "%matplotlib inline \n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create data:\n", "\n", "x = np.array([1,2,3,4,5,6])\n", "y = np.array([1,4,9,16,25,36])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create figure\n", "\n", "# Figure is a blank canvas\n", "fig = plt.figure(figsize=(8,5), dpi=100) # 800x500 pixel image\n", "\n", "# Add axes at specific position (fractions of fig width and height)\n", "position = [0.1, 0.1, 0.8, 0.8] # left, bottom, width, height\n", "axes = fig.add_axes(position)\n", "\n", "# Plot a line\n", "axes.plot(x, y, label=\"growth\") # label keyword used later!\n", "axes.set_xlabel('X Axis')\n", "axes.set_ylabel('Y Axis')\n", "axes.set_title(\"Simple Line\")\n", "\n", "# Save the image\n", "fig.savefig(\"file1.jpg\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Legends\n", "\n", "# Figure is a blank canvas\n", "fig = plt.figure(figsize=(8,5), dpi=100) # 800x500 pixel image\n", "\n", "# Add axes at specific position (fractions of fig width and height)\n", "position = [0.1, 0.1, 0.8, 0.8] # left, bottom, width, height\n", "axes = fig.add_axes(position)\n", "\n", "# Plot a line\n", "axes.plot(x, y, label=\"growth\") # label keyword used later!\n", "axes.set_xlabel('X Axis')\n", "axes.set_ylabel('Y Axis')\n", "axes.set_title(\"Simple Line\")\n", "\n", "\n", "# Location options: 0 = Auto Best Fit, 1 = Upper Right, 2 = Lower Right, \n", "# 3 = Lower Left, 4 = Lower Right\n", "axes.legend(loc=0)\n", "\n", "# Save the image\n", "fig.savefig(\"file2.jpg\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Colors & Lines\n", "# Figure is a blank canvas\n", "fig = plt.figure(figsize=(8,5), dpi=100) # 800x500 pixel image\n", "\n", "# Add axes at specific position (fractions of fig width and height)\n", "position = [0.1, 0.1, 0.8, 0.8] # left, bottom, width, height\n", "axes = fig.add_axes(position)\n", "\n", "# Plot a line\n", "axes.plot(x, y, label=\"growth\") # label keyword used later!\n", "axes.set_xlabel('X Axis')\n", "axes.set_ylabel('Y Axis')\n", "axes.set_title(\"Simple Line\")\n", "\n", "# Use the keywords in the plot method\n", "benchmark_data = [5,5,5,5,5,5]\n", "axes.plot(x, benchmark_data, label=\"benchmark\", color=\"r\", alpha=.5, linewidth=1, linestyle ='-', marker='+', markersize=4)\n", "axes.legend(loc=0)\n", "# Save the image\n", "fig.savefig(\"file3.jpg\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Axes Range & Tick Marks\n", "\n", "# Figure is a blank canvas\n", "fig = plt.figure(figsize=(8,5), dpi=100) # 800x500 pixel image\n", "\n", "# Add axes at specific position (fractions of fig width and height)\n", "position = [0.1, 0.1, 0.8, 0.8] # left, bottom, width, height\n", "axes = fig.add_axes(position)\n", "\n", "# Plot a line\n", "axes.plot(x, y, label=\"growth\") # label keyword used later!\n", "axes.set_xlabel('X Axis')\n", "axes.set_ylabel('Y Axis')\n", "axes.set_title(\"Simple Line\")\n", "\n", "# Control the range of the axes\n", "axes.set_xlim([1, 6])\n", "axes.set_ylim([1, 50]) # increasing y axis maximum to 50, instead of 35\n", "#axes.axis(\"tight\") # to get auto tight fitted axes, do this\n", "\n", "# Control the tick lines\n", "axes.set_xticks([1, 2, 3, 4, 5, 6])\n", "axes.set_yticks([0, 25, 50])\n", "\n", "# Control the labels of the tick lines\n", "axes.set_xticklabels([\"2018-07-0{0}\".format(d) for d in range(1,7)])\n", "axes.set_yticklabels([0, 25, 50])\n", "axes.legend(loc=0)\n", "fig.savefig(\"file4.jpg\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Subplots\n", "\n", "# 2 graphs side by side\n", "fig1, axes1 = plt.subplots(nrows=1, ncols=2, figsize=(8,5), dpi=100)\n", "\n", "# Set up first graph\n", "axes1[0].plot(x, x**2, color='r')\n", "axes1[0].set_xlabel(\"x\")\n", "axes1[0].set_ylabel(\"y\")\n", "axes1[0].set_title(\"Squared\")\n", "\n", "# Set up second graph\n", "axes1[1].plot(x, x**3, color='b')\n", "axes1[1].set_xlabel(\"x\")\n", "axes1[1].set_ylabel(\"y\")\n", "axes1[1].set_title(\"Cubed\")\n", "\n", "# Automatically adjust the positions of the axes so there is no overlap\n", "fig1.tight_layout()\n", "fig1.savefig(\"file5.jpg\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }