#!/usr/bin/env python # coding: utf-8 # In[1]: # import import matplotlib.pyplot as plt import numpy as np get_ipython().run_line_magic('matplotlib', 'inline') # #### Figures # A “figure” in matplotlib means the whole window in the user interface. Within this figure there can be “sub-plots”. # A figure is the windows in the GUI that has “Figure #” as title. Figures are numbered starting from1 as opposed # to the normal Python way starting from 0. # # Argument | Default| Description # ----|---------|---------------------------- # num| 1| number of figure # figsize| figure.figsize |figure size in in inches (width, height) # dpi| figure.dpi| resolution in dots per inch # facecolor| figure.facecolor| color of the drawing background # edgecolor| figure.edgecolor| color of edge around the drawing background # frameon| True| draw figure frame or not # # # #### Subplots # With subplot you can arrange plots in a regular grid. You need to specify the number of rows and columns and # the number of the plot. # # # #### Axes # Axes are very similar to subplots but allow placement of plots at any location in the figure. # So if we want to put a smaller plot inside a bigger one we do so with axes. # # # #### Ticks # Well formatted ticks are an important part of publishing-ready figures. Matplotlib provides a totally configurable # system for ticks. There are tick locators to specify where ticks should appear and tick formatters to # give ticks the appearance you want. # In[2]: # generating some data points X = np.linspace(-np.pi, np.pi, 256, endpoint=True) C, S = np.cos(X), np.sin(X) # In[3]: # creating a figure fig = plt.figure(figsize=(4,3), dpi=120) #plotting plt.plot(X, C, linestyle='--') plt.plot(X, S) # plotting plt.show() # In[4]: # creating a figure fig = plt.figure(figsize=(4,3), dpi=120) #plotting plt.plot(X, C, linestyle='--') plt.plot(X, S, linewidth=2) # adding labels plt.xlabel("X data") plt.ylabel("Y data") plt.title("Plotting") # adding margins, grid and autoscale plt.margins(0.15) plt.autoscale(True) plt.grid(True) # plotting plt.show() # ** Adding one more sub plot ** # In[5]: # creating a figure fig = plt.figure(figsize=(12,6), dpi=120) # adding subplot plt.subplot(1,2,1) # 1 row, 2 columns, 1st plot plt.plot(X, C, linestyle='--') plt.xlabel("X data") plt.ylabel("Y data") plt.title("Cos Plotting") plt.grid(True) plt.margins(0.15) plt.legend(["Cos"], loc="upper left") plt.subplot(1,2,2) # 1 row, 2 columns, 2nd plot plt.plot(X, S, linewidth=2) plt.xlabel("X data") plt.ylabel("Y data") plt.title("Sin Plotting") plt.legend(["Sin"], loc="upper left") plt.margins(0.15) plt.autoscale(True) plt.grid(True) # plotting plt.show() # ** another way to add subplots ** # In[6]: # creating a figure fig = plt.figure(figsize=(12,6), dpi=120) # adding subplot ax1 = fig.add_subplot(1,2,1) # 1 row, 2 columns, 1st plot ax1.plot(X, C, linestyle='--') ax1.grid(True) ax1.margins(0.15) ax2 = fig.add_subplot(1,2,2) # 1 row, 2 columns, 2nd plot ax2.plot(X, S, linewidth=2) ax2.margins(0.2) ax2.autoscale(True) ax2.grid(True) # plotting plt.xlabel("X data") plt.ylabel("Y data") plt.title("Sin Plotting") plt.legend(["Sin"], loc="upper left") plt.show() # ** One more way to add subplots ** # In[7]: # creating a figure plt.figure(figsize=(12,6), dpi=120) f, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True) ax1.plot(X,S, color='green', linestyle='--', linewidth='2') ax1.grid(True) ax1.margins(0.2) ax2.plot(X,C, color='blue', linestyle='-', linewidth='2') ax2.grid(True) ax2.margins(0.2) # In[8]: # creating a figure plt.figure(figsize=(12,6), dpi=120) f, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True) ax1.plot(X,S, color='green', linestyle='--', linewidth='2', label="Sin wave") ax1.grid(True) ax1.margins(0.2) ax2.plot(X,C, color='blue', linestyle='-', linewidth='2', label="Cos wave") ax2.grid(True) ax2.margins(0.2) ax1.legend() ax2.legend() # In[9]: # creating a figure plt.figure(figsize=(12,6), dpi=120) f, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True) ax1.plot(X,S, color='green', linestyle='--', linewidth='2', label="Sin wave") ax1.grid(True) ax1.margins(0.2) ax2.plot(X,C, color='blue', linestyle='-', linewidth='2', label="Cos wave") ax2.grid(True) ax2.margins(0.2) ax1.legend() ax1.set_xlabel("X data") ax1.set_ylabel("Sin data") ax1.set_title("Sin Chart") ax2.legend() ax2.set_xlabel("X data") ax2.set_ylabel("Sin data") ax2.set_title("Cos Chart") # ** Adding some samples plots ** # In[10]: fig = plt.figure(figsize=(16,12)) plt.subplots(nrows=3, ncols=4) plt.show() # In[11]: fig = plt.figure(figsize=(8,6)) ax1 = plt.subplot(1,2,1) plt.subplot(3,2,2) plt.subplot(3,2,4) plt.subplot(3,2,6) ax1.plot([1,2,3], [0.5,4,1.5]) plt.show() # In[12]: X = [ (2,1,1), (2,3,4), (2,3,5), (2,3,6) ] for nrows, ncols, plot_number in X: plt.subplot(nrows, ncols, plot_number) # In[13]: # removing all ticks X = [ (2,1,1), (2,3,4), (2,3,5), (2,3,6) ] for nrows, ncols, plot_number in X: plt.subplot(nrows, ncols, plot_number) plt.xticks([]) plt.yticks([]) # In[14]: # removing all ticks X = [ (1,2,1), (3,2,2), (3,2,4), (3,2,6) ] for nrows, ncols, plot_number in X: plt.subplot(nrows, ncols, plot_number) plt.xticks([]) plt.yticks([]) # In[15]: # removing all ticks X = [ (4,2,1), (4,2,3), (4,2,5), (4,1,4), (4,2,2), (4,2,(4,6)) ] for nrows, ncols, plot_number in X: plt.subplot(nrows, ncols, plot_number) plt.xticks([]) plt.yticks([]) # ** Setting the Plot Range ** # In[16]: fig, axes = plt.subplots(1, 3, figsize=(10, 4)) x = np.arange(0, 5, 0.25) axes[0].plot(x, x**2, x, x**3) axes[0].set_title("default axes ranges") axes[1].plot(x, x**2, x, x**3) axes[1].axis('tight') axes[1].set_title("tight axes") axes[2].plot(x, x**2, x, x**3) axes[2].set_ylim([0, 60]) axes[2].set_xlim([2, 5]) axes[2].set_title("custom axes range"); # In[20]: fig, ax1 = plt.subplots() x = np.arange(1,7,0.1) ax1.plot(x, 2 * np.pi * x, lw=2, color="blue") ax1.set_ylabel(r"Circumference $(cm)$", fontsize=16, color="blue") for label in ax1.get_yticklabels(): label.set_color("blue") ax2 = ax1.twinx() ax2.plot(x, np.pi * x ** 2, lw=2, color="darkgreen") ax2.set_ylabel(r"area $(cm^2)$", fontsize=16, color="darkgreen") for label in ax2.get_yticklabels(): label.set_color("darkgreen") #plt.grid(color='b', alpha=1.5, linestyle='dashed', linewidth=0.5) # In[18]: fig.savefig("filename.png", dpi=200) # In[24]: def f(t): return np.exp(-t) * np.cos(2*np.pi*t) def fp(t): return -2*np.pi * np.exp(-t) * np.sin(2*np.pi*t) - np.e**(-t)*np.cos(2*np.pi*t) def g(t): return np.sin(t) * np.cos(1/(t+0.1)) def g(t): return np.sin(t) * np.cos(1/(t)) python_course_green = "#476042" fig = plt.figure(figsize=(6, 4)) t = np.arange(-5.0, 1.0, 0.1) sub1 = fig.add_subplot(221) # instead of plt.subplot(2, 2, 1) sub1.set_title('The function f') # non OOP: plt.title('The function f') sub1.plot(t, f(t)) sub2 = fig.add_subplot(222, axisbg="lightgrey") sub2.set_title('fp, the derivation of f') sub2.plot(t, fp(t)) t = np.arange(-3.0, 2.0, 0.02) sub3 = fig.add_subplot(223) sub3.set_title('The function g') sub3.plot(t, g(t)) t = np.arange(-0.2, 0.2, 0.001) sub4 = fig.add_subplot(224, axisbg="lightgrey") sub4.set_title('A closer look at g') sub4.set_xticks([-0.2, -0.1, 0, 0.1, 0.2]) sub4.set_yticks([-0.15, -0.1, 0, 0.1, 0.15]) sub4.plot(t, g(t)) plt.plot(t, g(t)) plt.tight_layout() plt.show() # In[ ]: